也许有更好的方法可以做到这一点,但到目前为止,我必须做到这一点。我很高兴任何一个更了解WP的人能告诉我怎么做会更好!
“我的库”页面包含以下短代码:
[gallery media_category="943" orderby="post_date" order="DESC"]
我把它限制在学期
943
在名为
media_category
. 如果使用这样的东西,还需要在
$args
在下面
请注意orderby
和order
影响我们如何在下面的代码中获取下一个/上一个图像。
在我的image.php
模板一开始就有:
// Initial query args used for both next and previous:
$args = array(
\'post_type\' => \'attachment\',
\'post_mime_type\' => \'image/jpeg,image/gif,image/jpg,image/png\',
\'posts_per_page\' => 1,
\'post_status\' => \'inherit\',
// The same orderby as in our gallery shortcode:
\'orderby\' => \'post_date\',
// The same taxonomy as in our gallery shortcode:
\'tax_query\' => array(
\'taxonomy\' => \'media_category\',
\'field\' => \'term_id\',
\'terms\' => 943,
),
\'date_query\' => array()
);
// Add query args for finding previous image:
$args[\'order\'] = \'DESC\';
$args[\'date_query\'] = array(\'before\' => $post->post_date);
// Get previous image:
$prev_query = new WP_Query( $args );
while ($prev_query->have_posts()) {
$prev_query->the_post();
// Set previous image:
$prev_image = get_post();
}
wp_reset_postdata();
// Add query args for finding next image:
$args[\'order\'] = \'ASC\';
$args[\'date_query\'] = array(\'after\' => $post->post_date);
// Get next image:
$next_query = new WP_Query( $args );
while ($next_query->have_posts()) {
$next_query->the_post();
// Set next image:
$next_image = get_post();
}
wp_reset_postdata();
然后在模板中我想要链接的位置:
<?php
if ($prev_image) {
?>
<a href="<?php the_permalink($prev_image); ?>" title="<?php echo get_the_title($prev_image); ?>">Previous image</a>
<?php
}
if ($next_image) {
?>
<a href="<?php the_permalink($next_image); ?>" title="<?php echo get_the_title($next_image); ?>">Next image</a>
<?php
}
?>
我从不同的例子中把它拼凑在一起,似乎很有效。。。如果你知道改进,一定要让我知道!