2017-01-18 6 views
2

私はWordPressテーマを構築しています。コメント欄に問題があります。コメントを表示すると、投稿IDがランダムに表示されます。ランダム投稿IDを付与する理由

私は最初に2つの異なる場所に同じコードを置いていますが、TOPではそのコードは機能しません。誰もがなぜこれが底で働いていないのかを教えて助けてくれますか?ここで

は、あなたがループにあるので、どこでも、あなたが使用した、だから、最初のオフ、私 single.phpファイル

<?php get_template_part('/template-parts/standard-post-header'); ?> 
    <main role="main"> 
     <!-- section --> 
     <section> 
      <div class="container background-color"> 
       <?php if (have_posts()): while (have_posts()) : the_post(); ?> 

       <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>     

        <?php if (has_post_video(isset($post_id)) != null) { 
        // Featured VIDEO --> 
         get_template_part('/template-parts/featured-video-post'); 
        // END Featured VIDEO --> 
        } else { ?> 

        <!-- Featured Image --> 
        <?php $header_type = get_post_meta(get_the_id(), 'meta-box-dropdown', true); ?> 

        <?php if ($header_type == 'Slider') { 
        // SLIDER Header 
         get_template_part('/template-parts/featured-slider-post'); 
        ?> 

        <?php } else { 
        // SLIDER Header 
         get_template_part('/template-parts/featured-normal-post'); 
        } ?>       
        <!-- END Featured Image --> 
        <?php } ?> 

        <div class="container container-post-color"> 
         <div class="content"> 
          <?php the_content(); ?> 
          <?php edit_post_link(); ?> 
         </div> 
        </div> 

        <?php 
        global $post; 
        echo $post->ID; 
        ?> 

        <ol class="commentlist"> 
         <?php 
          //THIS WORKS!!! 
          $comments = get_comments(array(
           'post_id' => $post->ID, 
           'status' => 'approve' 
          )); 


          wp_list_comments(array(
           'per_page' => 10, 
           'reverse_top_level' => false 
          ), $comments); 
         ?> 
        </ol> 

        <!-- Post Navigation --> 
        <div class="container container-post-color top-space" style="padding-left: 0px; padding-right: 0px;"> 
         <div id="left-side"><?php previous_post_link(); ?></div> 
         <div id="right-side"><?php next_post_link(); ?></div> 
         <?php echo wp_link_pages(); ?> 
        </div> 

        <!-- Tags --> 
        <div class="tags-area"> 
         <?php echo the_tags(); ?> 
        </div> 

        <!-- Related Articles --> 
        <?php get_template_part('/template-parts/related-articles'); ?> 

        <!-- Coments Part -->    
        <?php //get_template_part('/template-parts/comments'); ?> 

        <?php 
        global $post; 
        echo $post->ID; 
        ?> 

        <ol class="commentlist"> 
         <?php 
          //THIS DOES NOT WORKS!!! WHY?! 
          $comments = get_comments(array(
           'post_id' => $post->ID, 
           'status' => 'approve' 
          )); 


          wp_list_comments(array(
           'per_page' => 10, 
           'reverse_top_level' => false 
          ), $comments); 
         ?> 
        </ol>     

       </article> 
       <!-- /article --> 
      </div> 
      <!-- END of Container-Fluid --> 

      <?php endwhile; ?> 
      <?php else: ?> 

      <!-- article --> 
      <article> 
       <div class="container background-color"> 
        <h1 class="black mastheading-post"><?php _e('Sorry, nothing to display.', 'html5blank'); ?></h1> 
       </div> 
      </article> 
      <!-- /article --> 

      <?php endif; ?> 
     </section> 
     <!-- /section --> 
    </main> 


    <!-- INSTAGRAM --> 
    <?php get_template_part('/template-parts/instagram'); ?> 


<?php get_footer(); ?> 

関連-articles.php

<div class="container container-post-color" style="padding-left: 0px; padding-right: 0px;"> 
    <div class="random">     
      <ul> 
       <?php $posts = get_posts('orderby=rand&numberposts=4'); foreach($posts as $post) { ?> 
       <div class="col-md-3 padding-zero"> 
        <li> 
         <div class="random-thumb"> 
          <?php the_post_thumbnail('full'); ?> 
         </div> 
         <div class="random-title"> 
          <h1 class="black mastheading"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1> 
         </div> 

         <div class="black iltalica"><?php echo excerpt(25); ?></div> 

         <div class="category"> 
          <div class="random-category"> 
           <h5><?php echo the_category();?></h5> 
          </div> 
         </div> 

        </li> 
       </div> 
       <?php } ?> 
      </ul> 
    </div> 
</div> 

答えて

2

ですglobal $post; ... $post->ID代わりにget_the_ID()を使用できるはずです。

第2に、私はこの問題があなたのテンプレート部分であると強く思っています/template-parts/related-articlesはおそらくmain loopを駄目にします。私は、あなたがそのファイルを見て、それが自分自身が投稿の選択肢にループしているかどうかを確認することをお勧めします - チャンスはメインループの内部で再利用できる方法で、きちんとしていないということです。

ファイルのコードをわかりやすくするために、そのファイルのコードを質問に追加することができます。

UPDATE

オクラホマので、確かに、あなたは、関連-記事のループの後ループデータをリセットする必要があります。

... 
<?php 
    } 
    wp_reset_postdata(); // <----- add this after your loop 
?> 
</ul> 
... 

・ホープ、このことができます!

+0

ありがとうございます。私は最初の質問にrelated-articles.phpを入れます。 – Barlet

+0

素晴らしいです、ありがとうございます。私は私の答えを変更しました。 – MacPrawn

+0

はい、あなたは正しかったです。私はこれをコメントした後<?php get_template_part( '/ template-parts/related-articles'); ?>それは仕事を始めた... あなたの助けをありがとう! – Barlet

関連する問題