个性化阅读
专注于IT技术分析

WordPress-覆盖短代码

我有一个主题, 可以通过首页上的滑块扩展Visual Composer插件。滑块将显示来自五个不同客户的五个推荐。我想将每个推荐的特色图像添加为滑块中的缩略图。

这是父主题的简化代码:

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’);解决了。


#1


你需要调用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');

#2


你必须在子主题的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;
}
赞(0) 打赏
未经允许不得转载:srcmini » WordPress-覆盖短代码
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

觉得文章有用就打赏一下文章作者

微信扫一扫打赏