programing

페이지의 사용자 지정 포스트 루프에 페이지 추가

oldcodes 2023. 11. 5. 14:57
반응형

페이지의 사용자 지정 포스트 루프에 페이지 추가

사용자 지정 페이지 템플릿(testimonals-page.php)을 작성했으며 해당 템플릿에서 다음 루프를 사용하여 사용자 지정 게시 유형 'testimonals'를 로드하고 있습니다.

<?php query_posts(array(
'posts_per_page' => 5,
'post_type' => 'testimonials',
    'orderby' => 'post_date',
    'paged' => $paged
 )
 ); ?>

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

    <div id="post-<?php the_ID(); ?>" class="quote">
    <?php echo get_the_post_thumbnail($id, array($image_width,$image_height)); ?>
    <?php the_content(); ?>
    </div>
    <?php endwhile; ?>
    <?php wp_reset_query(); ?>

거기에 페이지를 추가하려면 어떻게 해야 합니까?WP 페이징 플러그인을 설치했는데, 이 플러그인은 다음을 사용하여 페이지를 category.php로 호출하면 잘 작동합니다.

<p><?php wp_paging(); ?></p>

추천 페이지에 같은 것을 삽입합니다.php는 포맷이 깨지고 나에게 404를 링크합니다.

첫째, 기본 Wordpress Loop을 수정하려는 경우가 아니라면 query_posts를 절대 사용하지 마십시오.

대신 WP 쿼리로 전환합니다.

여기 제가 모든 워드프레스 기능이 내장된 고객을 위해 작성한 주제가 있습니다.지금까지 잘 작동했으므로 최대한 코드에 통합하겠습니다.

global $paged;
$curpage = $paged ? $paged : 1;
$args = array(
    'post_type' => 'testimonials',
    'orderby' => 'post_date',
    'posts_per_page' => 5,
    'paged' => $paged
);
$query = new WP_Query($args);
if($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
?>
<div id="post-<?php the_ID(); ?>" class="quote">
<?php
echo get_the_post_thumbnail($post->ID, array($image_width,$image_height));
the_content();
?>
</div>
<?php
endwhile;
    echo '
    <div id="wp_pagination">
        <a class="first page button" href="'.get_pagenum_link(1).'">&laquo;</a>
        <a class="previous page button" href="'.get_pagenum_link(($curpage-1 > 0 ? $curpage-1 : 1)).'">&lsaquo;</a>';
        for($i=1;$i<=$query->max_num_pages;$i++)
            echo '<a class="'.($i == $curpage ? 'active ' : '').'page button" href="'.get_pagenum_link($i).'">'.$i.'</a>';
        echo '
        <a class="next page button" href="'.get_pagenum_link(($curpage+1 <= $query->max_num_pages ? $curpage+1 : $query->max_num_pages)).'">&rsaquo;</a>
        <a class="last page button" href="'.get_pagenum_link($query->max_num_pages).'">&raquo;</a>
    </div>
    ';
    wp_reset_postdata();
endif;
?>

2018년 1월 편집:

또한 Wordpress에 내장되어 있고 보다 강력한 옵션과 기능을 갖추고 있으므로 paginate_links를 사용하는 것도 고려해 보십시오.

페이지 지정과 함께 사용자 지정 루프에 대해 이 코드를 사용해 보십시오.

<?php
if ( get_query_var('paged') ) {
    $paged = get_query_var('paged');
} elseif ( get_query_var('page') ) { // 'page' is used instead of 'paged' on Static Front Page
    $paged = get_query_var('page');
} else {
    $paged = 1;
}

$custom_query_args = array(
    'post_type' => 'post', 
    'posts_per_page' => get_option('posts_per_page'),
    'paged' => $paged,
    'post_status' => 'publish',
    'ignore_sticky_posts' => true,
    //'category_name' => 'custom-cat',
    'order' => 'DESC', // 'ASC'
    'orderby' => 'date' // modified | title | name | ID | rand
);
$custom_query = new WP_Query( $custom_query_args );

if ( $custom_query->have_posts() ) :
    while( $custom_query->have_posts() ) : $custom_query->the_post(); ?>

        <article <?php post_class(); ?>>
            <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
            <small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>
            <div><?php the_excerpt(); ?></div>
        </article>

    <?php
    endwhile;
    ?>

    <?php if ($custom_query->max_num_pages > 1) : // custom pagination  ?>
        <?php
        $orig_query = $wp_query; // fix for pagination to work
        $wp_query = $custom_query;
        ?>
        <nav class="prev-next-posts">
            <div class="prev-posts-link">
                <?php echo get_next_posts_link( 'Older Entries', $custom_query->max_num_pages ); ?>
            </div>
            <div class="next-posts-link">
                <?php echo get_previous_posts_link( 'Newer Entries' ); ?>
            </div>
        </nav>
        <?php
        $wp_query = $orig_query; // fix for pagination to work
        ?>
    <?php endif; ?>

<?php
    wp_reset_postdata(); // reset the query 
else:
    echo '<p>'.__('Sorry, no posts matched your criteria.').'</p>';
endif;
?>

출처:

언급URL : https://stackoverflow.com/questions/11638909/adding-pagination-to-custom-post-loop-in-page

반응형