2017-12-12 4 views
0

나는이 문제에 대한 해결책을 1 주 이상 찾고 있었고 같은 문제로 다른 사람을 찾을 수 없었습니다.WordPress 페이지 매기기가 페이지를 전환하지 않습니다

나는 다른 사람이 만든 사용자 지정 WP 테마로 작업 중입니다. 보조 루프 중 하나에 대해 페이징을 구현해야하는 단일 페이지 템플릿이 있습니다. 나는 내장 된 paginate_links() 기능과 다른 방법을 사용하려고 시도했다. 페이지 매김 링크가 나타나지만, 페이지 매김 링크를 클릭하면 페이지 매김의 해당 페이지로 이동하지 않습니다. 대신 원본 페이지가 다시로드됩니다 (예 : thewebsite.com/my-page/page/2/로 이동하지 않고 thewebsite.com/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(); ?> 

페이지 매김 자습서와 스레드가 있지만이 특정 문제를 해결하는 아직 찾지 못했습니다.

답변

0

나는 주로 코드가 작동해야한다고 생각합니다. $wp_query의 저장과 스위칭에 대해 걱정할 필요가 없습니다.이 간단한 코드를 사용해 보셨습니까? 그것은 내 환경에서 괜찮 았어 (비록 내가 어떤 결과를 반환하기 위해 쿼리 args를 약간 수정해야만 함).

작동하지 않는 경우 생성 된 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

아하나. 문제가되는 것은 페이징 링크가 아니며 페이지를 클릭 할 때 페이지가 리디렉션되는 방식입니다. 먼저 영구 링크를 비우십시오 (설정 -> 영구 링크로 이동하여 저장을 클릭하십시오). 작동하지 않으면 아이디어 링크를 클릭하십시오. https://wordpress.org/support/topic/pagination-on-singlephp-causing-301-redirect/?replies=10 –

0

고마워, 조! 그 기사는 나를 올바른 방향으로 가리켰다. 나는 dev 도구를 조사했고 실제로 301을 얻었습니다. 당신이 지적한 기사에서 코드 스 니펫을 시도했으나 제대로 작동하지 않았기 때문에 "redirect_canonical로 페이지 번호 매김 수정"및 this was the first article that popped up을 봤습니다. 나는 거기에서 함수를 가져 와서 functions.php에 던져 넣었다. et voilà! 그것은 같은 방법이지만, 사용자 정의 게시 유형을 조건부로 전달하지 않는 것이 좋습니다. 나는 이것이 미래에 누군가를 도울 수 있기를 바랍니다. 그것은 정말 고통 스러웠습니다. 그것은 또한 내가 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; 
} 
관련 문제