2017-12-12 11 views
0

私はこの問題の解決策を1週間以上探しており、同じ問題を抱えている誰かを見つけることができませんでした。WordPressページネーションはページを切り替えることはありません

私は他の誰かが作成したカスタムWPテーマに取り組んでいます。セカンダリループの1つに対してページングを実装する必要がある単一ページのテンプレートがあります。私は組み込みのpaginate_links()関数と他のメソッドを使用しようとしています。ページネーションリンクが表示されますが、ページネーションリンクをクリックすると、ページネーションのそのページには移動しません。代わりに元のページがリロードされます(つまり、website.com/my-page/page/2/に行く代わりに、website/my- page /をリロードします)。

以前devが正しいテンプレートをロードするためのfunctions.phpにこのフィルタを使用:

add_filter('single_template', create_function('$t', 'foreach((array) get_the_category() as $cat) { if (file_exists(TEMPLATEPATH . "/single-{$cat->slug}.php")) return TEMPLATEPATH . "/single-{$cat->slug}.php"; } return $t;')); 

そして、ここでは私のテンプレートファイルです:

<?php 
/** 
* Template Name: Project Template 
*/ 
get_header('news'); ?> 

<article role="main" class="projectpage"> 
<div class="container"> 
    <section class="pagecontent"> 


     <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
     <section class="overview"> 
      <h1><?php the_title(); ?></h1> 
      <div> 
       <?php the_content(); ?> 
      </div> 
      <div> 
       <?php if(get_post_meta($post->ID, 'pagelink', true)): ?> 
        <a href ="<?php echo get_post_meta($post->ID, 'pagelink', true); ?>" class="ctabutton2"> Read the Overview </a> 
       <?php endif; ?> 
      </div> 

</div><!--end row--> 
    </section><!--end overview--> 
    <?php endwhile ?> 
    <?php wp_reset_postdata() ?> 
    <? endif ?> 


    <section class="related"> 
     <div> 
      <h1> Related Resources </h1> 
      <h2> Explore our library of articles and resources </h2> 
     </div> 
     <div class="row"> 
      <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 relatedlinks"> 
       <section class="projectcategories"> 
        <h3> Categories </h3> 
        <ul> 
         <?php wp_list_categories(array(
          'orderby'   => 'id', 
          'show_count'   => true, 
          'use_desc_for_title' => false, 
          'child_of'   => 93, 
          'title_li' => ' ' 
         )); ?> 
        </ul> 
       </section> 

       <section class="project-search" role="search"> 

        <form method="get" action="<?php echo esc_url(home_url('/')); ?>"> 
         <input type="hidden" name="cat" id="cat" value="93" /> 
         <input type="text" size="16" name="s" placeholder="search keywords" class="search-box" /> 
         <input type="submit" value="Go" class="go"/> 
        </form> 

       </section> 

       <section class="otherprojects"> 
        <h3> Other Projects </h3> 

        <?php 
        $args = array(
         'category__in' => 91, 
         'post__not_in' => array($post->ID) 
        ); 
        // the query 
        $query = new WP_Query($args); 
        $temp_query = $wp_query; 
        $wp_query = NULL; 
        $wp_query = $query; 

        // The Loop 
        if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?> 
         <a href="<?php the_permalink();?>"><?php the_title(); ?></a> 

        <? endwhile ; 
         /* Restore original Post Data */ 
         wp_reset_postdata(); 
        endif; 


        $wp_query = NULL; 
        $wp_query = $temp_query; 
        ?> 
       </section> 
      </div><!--end col 1--> 

      <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9"> 
      <section class="articles"> 

       <?php 
// THIS IS THE SECTION WHERE I NEED THE PAGINATION 
       $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 

       $args = [ 
       'posts_per_page' => 3, 
       'paged' => $paged, 
       'post_type' => 'post', 
       'order' => 'DESC', 
       'post__not_in' => array($post->ID), 
       'tax_query' => [ 
       [ 
        'taxonomy' => 'category', 
        'field' => 'term_id', 
        'terms' => '93', 
       ], 
       ], 
      ]; 

      $custom_query = new WP_Query($args); 
       $temp_query = $wp_query; 
       $wp_query = NULL; 
       $wp_query = $custom_query; 
           if ($custom_query->have_posts()) { 
            while ($custom_query->have_posts()) { 
             $custom_query->the_post(); ?> 
            <div class="row"> 
             <div class="col-md-2 col-sm-2 col-xs-2 divider"> 
               <p class="date"><?php the_time('M j') ?></p> 
              </div><!--end col--> 
              <div class="col-md-4 col-sm-4 col-xs-4"> 
               <div class="articleimg"> 
               <?php if (has_post_thumbnail()) {?> 
                <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('blog-thumb'); ?></a> 
               <?php } ?> 
               </div><!--end blogimg--> 
              </div><!--end col--> 
              <div class="col-md-6 col-sm-6 col-xs-6"> 
               <div class="blogcontent"> 
                <h3><?php the_title();?></h3> 
                <p><?php the_excerpt(); ?></p> 
                <a href="<?php the_permalink(); ?>" class="readmore"> // read more </a> 
               </div><!--end blogcontent--> 
              </div><!--end col--> 
            </div><!--end row--> 
      <?php } 
       } 

      echo paginate_links(array(
       'total' => $wp_query->max_num_pages 
     )); 


       $wp_query = NULL; 
       $wp_query = $temp_query; 
       wp_reset_postdata(); ?> 
      </section><!--end articles--> 
      </div><!--end col 2--> 
     </div> <!--end row--> 
     </section><!--end related--> 


<!-- ANNOUNCEMENTS --> 
    <!--ANNOUNCEMENT SECTION --> 
    <!-- dynamic content --filters posts by category and only shows 'member' posts with a limit of six posts being 
    displayed--> 
    <section id="announcement-front" class="clearfix"> 
     <div class="container"> 
      <div> 
       <?php $query = new WP_Query('posts_per_page=1&category_name=advertisement'); 
       if ($query->have_posts()) : 
        while ($query->have_posts()) : $query->the_post(); ?> 
         <a href="<?php the_permalink()?>" <?php the_content();?> </a> 
        <?php endwhile ?> 
       <? endif ?> 
       <?php wp_reset_postdata() ?> 
      </div><!--end row--> 
     </div><!--container--> 
    </section><!--end announcement--> 


    </section> <!--end page content --> 
</div><!--end container--> 
     </article> 
<?php get_footer(); ?> 

私はWordPressの全体の銀河があり実現しますページ区切りのチュートリアルやスレッドがありますが、私はこの特定の問題を解決するものはまだ見つかりませんでした。

答えて

0

私はあなたのコードが主に機能するはずだと思います。 $wp_queryのすべての保存と切り替えについて心配する必要はありません。この簡単なスニペットを試しましたか?私の環境ではうまくいきました(ただし、結果を返すためにクエリの引数を少し修正しなくてはなりませんでした)。

これが機能しない場合は、生成するHTMLを投稿できますか?

 <section class="articles"> 
      <?php 
// THIS IS THE SECTION WHERE I NEED THE PAGINATION 
      $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 

      $args = [ 
      'posts_per_page' => 3, 
      'paged' => $paged, 
      'post_type' => 'post', 
      'order' => 'DESC', 
      'post__not_in' => array($post->ID), 
      'tax_query' => [ 
      [ 
       'taxonomy' => 'category', 
       'field' => 'term_id', 
       'terms' => '93', 
      ], 
      ], 
     ]; 

     $custom_query = new WP_Query($args); 
          if ($custom_query->have_posts()) { 
           while ($custom_query->have_posts()) { 
            $custom_query->the_post(); ?> 
           <div class="row"> 
            <div class="col-md-2 col-sm-2 col-xs-2 divider"> 
              <p class="date"><?php the_time('M j') ?></p> 
             </div><!--end col--> 
             <div class="col-md-4 col-sm-4 col-xs-4"> 
              <div class="articleimg"> 
              <?php if (has_post_thumbnail()) {?> 
               <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('blog-thumb'); ?></a> 
              <?php } ?> 
              </div><!--end blogimg--> 
             </div><!--end col--> 
             <div class="col-md-6 col-sm-6 col-xs-6"> 
              <div class="blogcontent"> 
               <h3><?php the_title();?></h3> 
               <p><?php the_excerpt(); ?></p> 
               <a href="<?php the_permalink(); ?>" class="readmore"> // read more </a> 
              </div><!--end blogcontent--> 
             </div><!--end col--> 
           </div><!--end row--> 
     <?php } 
      } 

     echo paginate_links(array(
      'total' => $custom_query->max_num_pages 
    )); 

      wp_reset_postdata(); ?> 
     </section><!--end articles--> 
     </div><!--end col 2--> 
    </div> <!--end row--> 
    </section><!--end related--> 
+0

ありがとう:

は、ここでは、コードです。これは実際に私が元々使っていたコードですし、 '$ wp_query'を保存して切り替える方法のチュートリアルを読んで試してみました。生成されたページネーションリンクは右のように表示されます。 '<スパンクラス=「ページ番号現在の」>3 ' 2 1しかし、私はそれだけで元のページをロードし、それらをクリックしたとき。 – Devon

+0

ああ、私は参照してください。それは問題であるページングリンクではなく、ページをクリックするとページがリダイレクトされる方法です。最初にパーマリンクを消去してみてください(設定 - > Permalinksに行き、Saveをクリックしてください)。それでも問題が解決しない場合は、このリンクをお試しください。 https://wordpress.org/support/topic/pagination-on-singlephp-causing-301-redirect/?replies=10 –

0

ありがとう、ジョー!その記事は正しい方向に私を指摘した。私は開発ツールをチェックして、実際に301を取得していました。私が指摘した記事のコードスニペットを試しましたが、うまくいかなかったので、 "redirect_canonicalでページ番号を修正"とthis was the first article that popped upを検索しました。私はそこから関数を取り出し、functions.phpに投げ込んだ。etvoilà!これは同じ方法ですが、カスタムポストタイプを条件に渡さないと思います。私はこれが将来誰かを助けることができることを願っています。それは本当の痛みでした。それはまた、私がWPについて本当に知っていることがほんの少ししかなく、どれくらい私がそれについて学びたいのかを思い出させるものでした。再度、感謝します。提案、ジョーのための

add_filter('redirect_canonical','custom_disable_redirect_canonical'); 
function custom_disable_redirect_canonical($redirect_url) { 
    if (is_paged() && is_singular()) $redirect_url = false; 
    return $redirect_url; 
} 
関連する問題