2017-08-10 3 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がありますが、ループを閉じていません。
  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'); 
    

    希望、それはあなたに役立つかもしれない:

したがって、私はあなたのコードを変更した、以下の更新されたコードを見つけてください。

お気軽にお問い合わせください。ありがとうございました

関連する問題