php – WordPress – 覆盖一个短码

前端之家收集整理的这篇文章主要介绍了php – WordPress – 覆盖一个短码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个主题,扩展Visual Composer插件首页上的滑块.该滑块将显示来自五个不同客户的五个推荐.我想将每个推荐的特色图片添加为滑块中的缩略图.

以下是父主题缩短的代码

  1. function jo_customers_testimonials_slider( $atts ) {
  2. extract( shortcode_atts( array( 'limit' => 5,"widget_title" => __('What Are People Saying','jo'),'text_color' => "#000" ),$atts ) );
  3. $content = "";
  4. $loopArgs = array( "post_type" => "customers","posts_per_page" => $limit,'ignore_sticky_posts' => 1 );
  5.  
  6. $postsLoop = new WP_Query( $loopArgs );
  7. $content = "";
  8.  
  9. $content .= '...';
  10. $content .= '...';
  11. $content .= '...';
  12.  
  13. wp_reset_query();
  14. return $content;
  15. }
  16. add_shortcode( 'jo_customers_testimonials_slider','jo_customers_testimonials_slider' );

我的functions.PHP文件

  1. function jo_customers_testimonials_slider_with_thumbnail( $atts ) {
  2. extract( shortcode_atts( array( 'limit' => 5,'ignore_sticky_posts' => 1 );
  3.  
  4. $postsLoop = new WP_Query( $loopArgs );
  5. $content = "";
  6.  
  7. $content .= '...';
  8. $content .= get_the_post_thumbnail( get_the_ID(),'thumbnail' );
  9. $content .= '...';
  10. $content .= '...';
  11.  
  12. wp_reset_query();
  13. return $content;
  14. }
  15. add_shortcode( 'jo_customers_testimonials_slider','jo_customers_testimonials_slider_with_thumbnail' );

在理论上,我的functions.PHP文件中的函数应该覆盖父主题的短代码.但是当我使用这个代码时,似乎没有发生任何事情.我究竟做错了什么?

编辑:

试过这段代码,但仍然不行.

  1. function wpa_add_child_shortcodes(){
  2. remove_shortcode('jo_customers_testimonials_slider');
  3. add_shortcode( 'jo_customers_testimonials_slider','jo_customers_testimonials_slider_with_thumbnail' );
  4. }
  5. 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的动作钩上触发

  1. function overwrite_shortcode()
  2. {
  3. function jo_customers_testimonials_slider_with_thumbnail( $atts ) {
  4. extract( shortcode_atts( array( 'limit' => 5,'thumbnail' );
  5. $content .= '...';
  6. $content .= '...';
  7.  
  8. wp_reset_query();
  9. return $content;
  10. }
  11. remove_shortcode('jo_customers_testimonials_slider');
  12. add_shortcode( 'jo_customers_testimonials_slider','jo_customers_testimonials_slider_with_thumbnail' );
  13. }
  14. add_action( 'wp_loaded','overwrite_shortcode' );

猜你在找的PHP相关文章