加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – WordPress – 覆盖一个短码

发布时间:2020-12-13 16:23:44 所属栏目:PHP教程 来源:网络整理
导读:我有一个主题,扩展Visual Composer插件与首页上的滑块.该滑块将显示来自五个不同客户的五个推荐.我想将每个推荐的特色图片添加为滑块中的缩略图. 以下是父主题缩短的代码: function jo_customers_testimonials_slider( $atts ) { extract( shortcode_atts(
我有一个主题,扩展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,'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’); to add_action(‘wp_loaded’,’wpa_add_child_shortcodes’);解决了

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读