2017-08-10 49 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'); 

希望,可能會對你有所幫助。

如果您有任何疑問,請隨時留言。謝謝

相關問題