WordPress的 - 覆盖一个简码

问题描述 投票:12回答:2

我有一个延伸与头版滑块可视化编辑器插件的一个主题。滑块将展示来自五个不同的客户5个褒奖。我想每一个告别赛的特色图像添加为滑块缩略图。

下面是从父主题缩短的代码:

function jo_customers_testimonials_slider( $atts ) {
    extract( shortcode_atts( array( 'limit' => 5, "widget_title" => __('What Are People Saying', 'jo'), 'text_color' => "#000" ), $atts ) );
    $content = "";
    $loopArgs = array( "post_type" => "customers", "posts_per_page" => $limit, 'ignore_sticky_posts' => 1 );

    $postsLoop = new WP_Query( $loopArgs );
    $content = "";

    $content .= '...';
    $content .= '...';
    $content .= '...';

    wp_reset_query();
    return $content;
}
add_shortcode( 'jo_customers_testimonials_slider', 'jo_customers_testimonials_slider' ); 

我functions.php文件:

function jo_customers_testimonials_slider_with_thumbnail( $atts ) {
    extract( shortcode_atts( array( 'limit' => 5, "widget_title" => __('What Are People Saying', 'jo'), 'text_color' => "#000" ), $atts ) );
    $content = "";
    $loopArgs = array( "post_type" => "customers", "posts_per_page" => $limit, 'ignore_sticky_posts' => 1 );

    $postsLoop = new WP_Query( $loopArgs );
    $content = "";

    $content .= '...';
    $content .= get_the_post_thumbnail( get_the_ID(), 'thumbnail' );
    $content .= '...';
    $content .= '...';

    wp_reset_query();
    return $content;
}
add_shortcode( 'jo_customers_testimonials_slider', 'jo_customers_testimonials_slider_with_thumbnail' );

从理论上讲,从我functions.php文件的功能应该覆盖从父主题简码。但似乎没有,当我使用此代码的情况发生。我究竟做错了什么?

编辑: 尝试这种代码,但它仍然是行不通的。

function wpa_add_child_shortcodes(){
remove_shortcode('jo_customers_testimonials_slider');
    add_shortcode( 'jo_customers_testimonials_slider', 'jo_customers_testimonials_slider_with_thumbnail' );
}
add_action( 'after_setup_theme', 'wpa_add_child_shortcodes' );

也改变 add_action( 'after_setup_theme', 'wpa_add_child_shortcodes' );add_action( 'init', 'wpa_add_child_shortcodes' ); ,但结果没有差异。

编辑2(用溶液):

更改add_action( 'after_setup_theme', 'wpa_add_child_shortcodes' );add_action( 'wp_loaded', 'wpa_add_child_shortcodes' );解决了这个问题。

php wordpress-theming wordpress
2个回答
30
投票

你需要调用remove_shortcode();是这样的:

remove_shortcode('jo_customers_testimonials_slider');` 

在您使用相同的名称添加新简码为“覆盖”了。

您还需要调用它的父主题运行后,所以我们开枪称为wp_loaded行动挂钩。

function overwrite_shortcode() {

    function jo_customers_testimonials_slider_with_thumbnail($atts) {
        extract(shortcode_atts(array('limit' => 5, "widget_title" => __('What Are People Saying', 'jo'), 'text_color' => "#000"), $atts));
        $content = "";
        $loopArgs = array("post_type" => "customers", "posts_per_page" => $limit, 'ignore_sticky_posts' => 1);

        $postsLoop = new WP_Query($loopArgs);
        $content = "";

        $content .= '...';
        $content .= get_the_post_thumbnail(get_the_ID(), 'thumbnail');
        $content .= '...';
        $content .= '...';

        wp_reset_query();
        return $content;
    }

    remove_shortcode('jo_customers_testimonials_slider');
    add_shortcode('jo_customers_testimonials_slider', 'jo_customers_testimonials_slider_with_thumbnail');
}

add_action('wp_loaded', 'overwrite_shortcode');

4
投票

你在你的子主题的functions.php中编写的代码

add_action( 'after_setup_theme', 'calling_child_theme_setup' );

function calling_child_theme_setup() {
   remove_shortcode( 'parent_shortcode_function' );
   add_shortcode( 'shortcode_name', 'child_shortcode_function' );
}

function child_shortcode_function( $atts) {
    $atts = shortcode_atts( array(
        'img'  => '',
        'cat'  => '',
        'capt' => '',
        'link' => ''
    ), $atts );

//YOUR OWN CODE HERE

    $imgSrc = wp_get_attachment_image_src( $atts['img'], 'delicious-gallery' );

    $imgFull = wp_get_attachment_image_src( $atts['img'], 'full' );



    $b = '<div class="screen-item" data-groups=\'["'.strtolower(str_replace(' ', '_', $atts["cat"])).'", "all"]\'>'.

        '<a href="'.$atts["link"].'" data-title="'.$atts["capt"].'" target="_blank"><img src="'.$imgSrc[0].'" alt="SCREEN" class="screenImg" /></a>'.

        '<span>'.$atts["capt"].'</span>'.

    '</div>';

//YOUR OWN CODE HERE

    return $b;
}
© www.soinside.com 2019 - 2024. All rights reserved.