your programing

워드프레스 루프 표시 제한 게시물

lovepro 2023. 4. 2. 12:15
반응형

워드프레스 루프 표시 제한 게시물

이것은 기본 루프입니다.

<?php while (have_posts()) : the_post(); ?>

검색 결과 페이지에 20개의 게시물을 표시하고 싶습니다.관리 패널 옵션의 값을 변경할 수 있지만 인덱스 페이지와 아카이브 페이지 등 모든 값이 변경됩니다.난 다른 방식으로 그들을 가질 필요가 있어.

참고 자료: http://codex.wordpress.org/The_Loop

while 스테이트먼트를 호출하기 직전에 투고를 조회해야 합니다.그래서:

  <?php query_posts('posts_per_page=20'); ?>

  <?php while (have_posts()) : the_post(); ?>
    <!-- Do stuff... -->
  <?php endwhile;?>

편집: 페이지 번호 지정에 대해 죄송합니다. 다음을 시도해 보십시오.

    <?php 
        global $query_string;
        query_posts ('posts_per_page=20');
        if (have_posts()) : while (have_posts()) : the_post();
    ?>
    <!-- Do stuff -->
    <?php endwhile; ?>

    <!-- pagination links go here -->

    <? endif; ?>

나는 이 해결책을 찾았고 그것은 나에게 효과가 있었다.

 global $wp_query;
 $args = array_merge( $wp_query->query_vars, ['posts_per_page' => 20 ] );
 query_posts( $args );

 if(have_posts()){
   while(have_posts()) {
     the_post();
     //Your code here ...
   }
 }

$wp_query 개체를 통해 루프당 게시 수를 제한할 수 있습니다.예를 들어 다음과 같은 여러 파라미터를 사용합니다.

<?php 
$args = array('posts_per_page' => 2, 'post_type' => 'type of post goes here');
$query = new WP_Query( $args );
while( $query->have_posts()) : $query->the_post();
<!-- DO stuff here-->
?>

wp_query 객체에 대한 자세한 내용은 이쪽>

'pagination' 추가 => $pagination이 작동합니다!

<?php 
$args = array('posts_per_page' => 2, 'paged' => $paged);
$query = new WP_Query( $args );
while( $query->have_posts()) : $query->the_post();
<!-- DO stuff here-->
?>

템플릿 내부에 새 쿼리가 있는 응답은 사용자 지정 게시 유형에서 올바르게 작동하지 않습니다.

그러나 문서에서는 쿼리에 후크하여 메인 쿼리인지 확인하고 실행 전에 수정할 수 있습니다.이는 템플릿 함수 내에서 수행할 수 있습니다.

function my_post_queries( $query ) {
  // do not alter the query on wp-admin pages and only alter it if it's the main query
  if (!is_admin() && $query->is_main_query()) {
    // alter the query for the home and category pages 
    if(is_home()){
      $query->set('posts_per_page', 3);
    }

    if(is_category()){
      $query->set('posts_per_page', 3);
    }
  }
}
add_action( 'pre_get_posts', 'my_post_queries' );

if 스테이트먼트를 호출하기 직전에 투고를 조회해야 합니다.

이것은 아카이브, 커스텀 투고 타입 등에 대응합니다.

global $query_string;
query_posts( $query_string . '&posts_per_page=12' );

if ( have_posts() ) : 

언급URL : https://stackoverflow.com/questions/3875895/wordpress-loop-show-limit-posts

반응형