2017-08-10 53 views
1

我想创建一个短代码,它将显示CPT =感言的循环。我准备了这样的代码:短代码内的Wordpress自定义文章类型循环

function testimonials_loop_shortcode() { 
     $args = array(
      'post_type' => 'testimonials', 
      'post_status' => 'publish', 
     ); 

     $my_query = null; 
     $my_query = new WP_query($args); 
     while ($my_query->have_posts()) : $my_query->the_post(); 

     $custom = get_post_custom(get_the_ID()); 

     ?><p><?php the_title();?></p><?php 
     ?><p>the_content();</p><?php 

    wp_reset_postdata(); 
    else : 
    _e('Sorry, no posts matched your criteria.'); 
    endif; 
} 

add_shortcode('testimonials_loop', 'testimonials_loop_shortcode'); 

它准备粘贴在functions.php中。但代码打破了网站/错误500.我做错了什么?

回答

0

我回顾了你的代码,有很多修补程序必须完成。以下是修正的列表:

  1. 您尚未打开if条件,但已用endif关闭它。
  2. 你有opend while循环,但没有关闭它。
  3. 您尚未将<?php ?>标签放在the_content()功能的附近。

因此,我已修改了代码,请在下面找到更新的代码:

function testimonials_loop_shortcode() { 
    $args = array(
     'post_type' => 'testimonials', 
     'post_status' => 'publish', 
    ); 

    $my_query = null; 
    $my_query = new WP_query($args); 
    if($my_query->have_posts()): 
     while($my_query->have_posts()) : $my_query->the_post(); 
      $custom = get_post_custom(get_the_ID()); 
      echo "<p>".get_the_title()."</p>"; 
      echo "<p>".get_the_content()."</p>"; 
     endwhile; 
     wp_reset_postdata(); 
    else : 
    _e('Sorry, no posts matched your criteria.'); 
    endif; 
} 

add_shortcode('testimonials_loop', 'testimonials_loop_shortcode'); 

希望,可能会对你有所帮助。

如果您有任何疑问,请随时留言。谢谢

相关问题