我正在尝试为自定义帖子类型创建无限的上一个/下一个按钮。它完美无缺,直到你到达最后一篇文章。
正如您在我的代码中所看到的,我检查是否有先前的帖子,否则将执行其他帖子。在这个循环中,如果我目前在自定义帖子类型的最后一个帖子上,我希望得到第一篇文章,反之亦然。
我没有在这种情况下显示第一篇文章,而是看到当前帖子的数据。
$first = new WP_Query('post_type=success-stories&posts_per_page=1&order=DESC'); $first->the_post();
我认为此代码不正确,以获得第一篇文章。老实说,我认为DESC和ASC将确定第一篇和最后一篇文章。
<?php $prevPost = get_previous_post(); ?>
<?php if($prevPost) {
$args = array(
'post_type' => 'success-stories',
'posts_per_page' => 1,
'include' => $prevPost->ID
);
$prevPost = get_posts($args);
foreach ($prevPost as $post) {
setup_postdata($post); ?>
<div class="hide-medium blogpost-link">
<a href="<?php the_permalink(); ?>"></a>
<?php $thumbnail = get_field('sv_thumbnail_image'); ?>
<div class="bg-image" style="background-image: url(<?php echo $thumbnail['url']; ?>)"></div>
<div class="overlay">
<div class="inner">
<div class="icon">
<?php
$client_image = get_field('blog_client_logo');
if( ! empty($client_image) ):
echo '<img src="' . $client_image['url'] . '" alt="">';
endif;
?>
</div>
<div class="vertical-centered text-center text">
<h3> <?php the_title(); ?> </h3>
<a class="btn inline-block solid-btn" href="<?php the_permalink(); ?>">Lees verder</a>
</div>
</div>
</div>
</div>
<div class="show-medium small-6 prev previous-link">
<a href="<?php the_permalink(); ?>">← Vorige</a>
</div>
<?php wp_reset_postdata();
} //end foreach
} else {
$first = new WP_Query('post_type=success-stories&posts_per_page=1&order=DESC'); $first->the_post(); ?>
<div class="hide-large blogpost-link">
<a href="<?php the_permalink(); ?>"></a>
<?php $thumbnail = get_field('sv_thumbnail_image'); ?>
<div class="bg-image" style="background-image: url(<?php echo $thumbnail['url']; ?>)"></div>
<div class="overlay">
<div class="inner">
<div class="icon">
<?php
$client_image = get_field('blog_client_logo');
if( ! empty($client_image) ):
echo '<img src="' . $client_image['url'] . '" alt="">';
endif;
?>
</div>
<div class="vertical-centered text-center text">
<h3> <?php the_title(); ?> </h3>
<a class="btn inline-block solid-btn" href="<?php the_permalink(); ?>">Lees verder</a>
</div>
</div>
</div>
</div>
<div class="show-medium small-6 prev previous-link">
<a href="<?php the_permalink(); ?>">← Vorige</a>
</div>
<?php wp_reset_query();
} ?>
<?php $nextPost = get_next_post(); ?>
<?php if( $nextPost) {
$args = array(
'post_type' => 'success-stories',
'posts_per_page' => 1,
'include' => $nextPost->ID
);
$nextPost = get_posts($args);
foreach ($nextPost as $post) {
setup_postdata($post); ?>
<div class="hide-medium blogpost-link next">
<a href="<?php the_permalink(); ?>"></a>
<?php $thumbnail = get_field('sv_thumbnail_image'); ?>
<div class="bg-image" style="background-image: url(<?php echo $thumbnail['url']; ?>)"></div>
<div class="overlay">
<div class="inner">
<div class="icon">
<?php
$client_image = get_field('blog_client_logo');
if( ! empty($client_image) ):
echo '<img src="' . $client_image['url'] . '" alt="">';
endif;
?>
</div>
<div class="vertical-centered text-center text">
<h3> <?php the_title(); ?> </h3>
<a class="btn inline-block solid-btn" href="<?php the_permalink(); ?>">Lees verder</a>
</div>
</div>
</div>
</div>
<div class="show-medium small-6 text-right next next-link">
<a href="<?php the_permalink(); ?>">Volgende → </a>
</div>
<?php wp_reset_postdata();
} //end foreach
} else {
$last = new WP_Query('post_type=success-stories&posts_per_page=1&order=DESC'); $last->the_post(); ?>
<div class="hide-medium blogpost-link">
<a href="<?php the_permalink(); ?>"></a>
<?php $thumbnail = get_field('sv_thumbnail_image'); ?>
<div class="bg-image" style="background-image: url(<?php echo $thumbnail['url']; ?>)"></div>
<div class="overlay">
<div class="inner">
<div class="icon">
<?php
$client_image = get_field('blog_client_logo');
if( ! empty($client_image) ):
echo '<img src="' . $client_image['url'] . '" alt="">';
endif;
?>
</div>
<div class="vertical-centered text-center text">
<h3> <?php the_title(); ?> </h3>
<a class="btn inline-block solid-btn" href="<?php the_permalink(); ?>">Lees verder</a>
</div>
</div>
</div>
<div class="show-medium small-6 text-right next next-link">
<a href="<?php the_permalink(); ?>">Volgende → </a>
</div>
</div> <?php
wp_reset_query();
} ?>
我在@kaddath的帮助下找到了解决方案
我的代码有两个问题。 首先,我在订单上犯了一个错误。在我的第一个循环中,我写了以下
$first = new WP_Query('post_type=success-stories&posts_per_page=1&order=DESC'); $first->the_post();
在这种情况下,DESC应该是ASC。
这让我想到了下一个问题,我忘记在此时设置postdata,我需要添加setup_postdata($ first)和setup_postdata($ last)以获取最后和第一篇文章的详细信息。