2

WordPress의 개발을 배우기 위해, 저는 처음부터 WordPress 테마를 만들고 있습니다. 이제 내 카테고리 페이지에 페이지 매김을 추가하고 싶지만 문제는 다음과 같습니다. 이전 게시물 링크를 클릭하면 "http://localhost/wordpress4/category/bloc1/"에서 "http://localhost/wordpress4/category/bloc1/page/2/"로 변경됩니다. 그러나 다른 페이지를 표시하는 대신 빈 페이지로 이동합니다 게시물. 2 페이지의 WordPress 매김 페이지 빈 페이지 (처음부터 테마 만들기)

내가 눈치 category.php

<?php get_header(); ?> 

    <div class="container"> 
    <?php 
    $counter = 1; //start counter 

    $grids = 3; //Grids per row 

    global $query_string; //Need this to make pagination work 


    /*Setting up our custom query (In here we are setting it to show 12 posts per page and eliminate all sticky posts) */ 
    query_posts($query_string . '&caller_get_posts=1&posts_per_page=4'); 


    if(have_posts()) : while(have_posts()) : the_post(); 
    ?> 
    <?php 
    //Show the left hand side column 
    if($counter == 1) : 
    ?> 
    <div class="row"> 
      <div class="col-md-4"> 
      <div class="center"> 
       <div class="postimage"> 
       <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_custom_logo(); ?></a> 
       </div> 
        <h2><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2> 
        <h4><?php the_category(); ?></h4> 
      </div> 
      </div> 
    <?php 

    elseif($counter == 2) : 
    ?> 
    <div class="col-md-4 border2"> 
    <div class="center"> 
       <div class="postimage"> 
       <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_custom_logo(); ?></a> 
       </div> 
        <h2><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2> 
        <h4><?php the_category(); ?></h4> 
      </div> 
      </div> 

    <?php 
    elseif($counter == $grids) : 
    ?> 
    <div class="col-md-4"> 
    <div class="center"> 
       <div class="postimage"> 
       <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_custom_logo(); ?></a> 
       </div> 
        <h2><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2> 
        <h4><?php the_category(); ?></h4> 
      </div> 
      </div> 
    </div> 
    <div class="clear"></div> 
    <?php 
    $counter = 0; 
    endif; 
    $counter++; 
    endwhile; 
    ?> 


     <div class="row"> 
     <div class="col-xs-6 text-left"> 
     <?php next_posts_link('<< Older post'); ?> 
     </div> 
     <div class="col-xs-6 text-right"> 
     <?php previous_posts_link('Newer post >>'); ?> 
     </div> 


    <?php 
    endif; 
    ?> 

    </div> 
    </div> 

    <?php get_footer(); ?> 

의 코드입니다 내가 카테고리 페이지도 내 index.php를 아래의 페이지 매김 작업을 코드를 추가합니다. 두 번째 카테고리 페이지 ("http://localhost/wordpress4/category/bloc1/page/2/")는 index.php의 마크 업을 사용하므로 게시물은 첫 번째 카테고리 페이지와 같은 그리드 형식이 아니게됩니다.

카테고리 페이지에도 이전 링크가 페이지 하단에 표시되는 대신 행 사이에 표시됩니다. enter image description here

마침내이

<?php get_header(); ?> 
<div class="container"> 
    <div class="row"> 
     <div class="col-xs-12 col-sm-8"> 
     <?php 



     if(have_posts()): 
       while(have_posts()): the_post(); ?> 



        <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"> 

         <h3><?php the_title(); ?></h3> 
        <small><?php the_category(); ?></small> 

        </a>    

        <p><?php the_content(); ?></p> 
        <hr/> 


      <?php endwhile; 
      endif; 

      ?> 
     </div> 

     <div class="col-xs-12 col-sm-4"> 
     <?php get_sidebar(); ?> 
     </div> 

    </div> 
</div> 



<?php get_footer(); ?> 

이 감사 제의 index.php에 코드입니다.

답변

0

을 확인합니다 수 있습니다. 문제는 "블로그 페이지가 최대로 표시됩니다"라는 단어 설정을 읽는 설정에서 "posts_per_page = 4"로 인해 query_posts()를 통해 선언 된 것이 었습니다. 솔루션 :

WP_Query() 또는 pre_get_posts 필터를 사용하는 것이 가장 좋기 때문에 "query_posts()"가 삭제되었습니다. wp_query를 사용하여조차도 사용할 수있는 페이지 매김을 얻지 못했습니다. 따라서 pre_get_posts 필터를 사용해 보았는데 효과적이었습니다. category.php에서 나는 query_posts를 삭제하고 정상적인 루프 만 사용했습니다.

add_action('pre_get_posts', function ($q) 
{ 
    if (  !is_admin() // Very important, otherwise back end queries will be affected as well 
      && $q->is_main_query() // Very important, we just need to modify the main query 
      && $q->is_category() // Only target category pages 
    ) { 

        $q->set('posts_per_page', 2); 


    } 
}); 

내 대답은 것입니다 희망 :

내가 내 function.php 이에 pre_get_posts 동작을 추가 한 다음

<?php get_header(); ?> 

    <div class="container"> 
    <?php 
    $counter = 1; //start counter 

    $grids = 3; //Grids per row 



    if(have_posts()) : 
    while(have_posts()) : the_post(); 


    ?> 
    <?php 
    //Show the left hand side column 
    if($counter == 1) : 
    ?> 
    <div class="row"> 
      <div class="col-md-4"> 
      <div class="center"> 
       <div class="postimage"> 
       <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_custom_logo(); ?></a> 
       </div> 
        <h2><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2> 
        <h4><?php the_category(' '); ?></h4> 
      </div> 
      </div> 
    <?php 

    elseif($counter == 2) : 
    ?> 
    <div class="col-md-4 border2"> 
    <div class="center"> 
       <div class="postimage"> 
       <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_custom_logo(); ?></a> 
       </div> 
        <h2><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2> 
        <h4><?php the_category(' '); ?></h4> 
      </div> 
      </div> 

    <?php 
    elseif($counter == $grids) : 
    ?> 
    <div class="col-md-4"> 
    <div class="center"> 
       <div class="postimage"> 
       <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_custom_logo(); ?></a> 
       </div> 
        <h2><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2> 
        <h4><?php the_category(' '); ?></h4> 
      </div> 
      </div> 
    </div> 
    <div class="clear"></div> 
    <?php 
    $counter = 0; 
    endif; 
    ?> 

    <?php 
    $counter++; 
    endwhile; 
    ?> 

       <div class="row"> 

        <div class="col-xs-12 text-left "> 
        <?php next_posts_link('<< Older post'); ?> 
        </div> 
        <div class="col-xs-12 text-right "> 
        <?php previous_posts_link('Newer post >>'); ?> 
        </div> 

       </div> 



    <?php 
    endif; 
    ?> 
    </div> 

    <?php get_footer(); ?> 

category.php 내 새로운 코드는 코드이됩니다 내 대답이 그렇게 잘 설명되어 있지 않아도 같은 문제가있는 사람을 도우십시오. 자세한 내용은 다음을 참조하십시오. wp_query로 페이지 매김 사용 pre_get_posts를 사용하여 사용자 정의 페이지의 페이지 매김을 설정합니다.

개발자가 내 솔루션을 자세히 설명하고 pre_get_posts를 사용하여 사용자 정의 페이지의 페이지 매김을 설정하는 방법에 대한 자세한 정보를 제공하는 것이 좋을 것입니다.

+1

pre_get_posts 쿼리 변수 개체가 만들어진 후 실제 쿼리가 실행되기 전에 호출 된 후크입니다. 당신은 사용 참조 때문에 무언가를 돌려 줄 필요가 없습니다. pre_get_posts hook은 백엔드 do_action_ref_array ('pre_get_posts', WP_Query) 함수를 실행합니다. 자세한 내용을 알고 싶다면 https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts 및 https://developer.wordpress.org/reference/hooks/pre_get_posts/ –

+0

감사합니다. @NahidHasan –

1

사용이 코드는, 자세한 내용은 문제

<?php 
// the query to set the posts per page to 3 
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
$args = array('posts_per_page' => 3, 'paged' => $paged); 
query_posts($args); ?> 
<!-- the loop --> 
<?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
     <!-- rest of the loop --> 
     <!-- the title, the content etc.. --> 
<?php endwhile; ?> 
<!-- pagination --> 

<div class="row"> 
    <div class="col-xs-12>" 
    <div class="col-xs-6 text-left"><?php next_posts_link(); ?></div> 
    <div class="col-xs-6 text-right"><?php previous_posts_link(); ?></div> 
    </div> 
</div> 
<?php else : ?> 
<!-- No posts found --> 
<?php endif; wp_reset_postdata(); ?> 

를 해결 내가 해결책을 찾을 수 있었다 검색을 많이하면이 링크 https://codex.wordpress.org/Pagination

+0

안녕하세요. 도움을 주셔서 감사합니다. 사실 저는 방금 문제를 발견했습니다.wordpress 읽기 설정에서 "Blog pages show at most"는 query_posts()를 통해 선언 한 posts_per_page = 4를 방해합니다. 나는이 문제에 대한 해결책을 게시 할 것이다. –

+0

방금 ​​문제를 설명 할 수 있다면 해결 방법을 게시하여 동일한 문제를 가진 사람들이 더 잘 이해할 수 있도록하십시오. –

관련 문제