6

カスタム ページ テンプレート (testimonials-page.php) を作成し、そのテンプレートで、次のループを使用してカスタム投稿タイプ 'testimonials' を読み込んでいます。

<?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 Paging プラグインをインストールしましたが、そのプラグインは、次を使用してページネーションを category.php に呼び出すとうまく機能します。

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

同じものを testimonial-page.php に挿入すると、フォーマットが壊れ、リンクが 404 になります。

4

2 に答える 2

16

まず、Wordpress のデフォルトのループを変更するつもりがない限り、決して query_posts を使用しないでください。

代わりに、 WP Queryに切り替えます。

これは、組み込みのすべてのWordpress機能を使用して、クライアント用に行ったテーマ用に書いたものです. これまでのところかなりうまく機能しているので、できる限りコードに統合します。

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月編集:

paginate_linksも Wordpress に組み込まれており、より堅牢なオプションと機能を備えているため、paginate_linksの使用も検討してください。

于 2012-07-24T21:29:14.200 に答える