我正在开发一个WordPress上的库存照片页面,我想显示每页一定数量的图像,然后在底部显示导航。我可以正确地显示图像,没有任何问题,但导航根本不会显示。下面是我的模板页面:
$args = array(
'showposts' => 12,
'post_type' => 'attachment',
'paged' => $paged
);
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo '<li>';
echo '<a class="th" title="';
echo apply_filters( 'the_title', $attachment->post_title );
echo '" href="';
echo apply_filters( 'the_permalink', '?attachment_id='.$attachment->ID );
echo '">';
echo wp_get_attachment_image( $attachment->ID, 'thumbnail' );
echo '</a>';
echo '</li>';
}
}
?>
</ul>
<nav>
<?php previous_posts_link('« Newer') ?>
<?php next_posts_link('Older »') ?>
</nav>
添加$page变量:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 24,
'post_type' => 'attachment',
'paged' => $paged
);
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo '<li>';
echo '<a class="th" title="';
echo apply_filters( 'the_title', $attachment->post_title );
echo '" href="';
echo apply_filters( 'the_permalink', '?attachment_id='.$attachment->ID );
echo '">';
echo wp_get_attachment_image( $attachment->ID, 'thumbnail' );
echo '</a>';
echo '</li>';
}
}
?>
</ul>
<nav>
<?php previous_posts_link('« Newer') ?>
<?php next_posts_link('Older »') ?>
</nav>