【发布时间】:2021-04-19 10:59:42
【问题描述】:
我正在尝试在 WordPress 中构建一个帖子滑块,显示来自特定类别的自定义帖子,并在帖子上添加分页。我希望这个分页是循环的,以便最后一个帖子链接到第一个帖子,反之亦然。
它几乎可以完美运行,但由于某种原因,最近发布的“上一个”链接链接到自身而不是上一个帖子(“下一个”链接正确地指向该类别中最旧的帖子),我不知道为什么。
如果我只是使用
<?php next_post_link('%link', '%title', TRUE); ?>
<?php previous_post_link('%link', '%title', TRUE); ?>
它可以正常工作,但不是循环的。但是,如果我添加 If 语句以在没有下一个/上一个项目的情况下显示第一个/最后一个链接,则最近的帖子不再认为它有以前的帖子可以链接到,即使它有。
这是我的完整代码:
<?php $args = array(
'post_type' => 'pub',
'post_status' => 'publish',
'posts_per_page' => 99,
'category__in' => 17,
'order' => 'DESC',
);
$arr_posts = new WP_Query( $args );
$post = $posts[0]; $c=0; ?>
<?php
if ( $arr_posts->have_posts() ) :
?>
<section class="above-fold-splash">
<div class="mySlider">
<?php
while ( $arr_posts->have_posts() ) :
$arr_posts->the_post();
$image = get_field('photo');
$image_size_big = 'full';
$image_size_small = 'large-square';
$image_array_big = wp_get_attachment_image_src($image, $image_size_big);
$image_array_small = wp_get_attachment_image_src($image, $image_size_small);
$image_url = $image_array_big[0];
$image_url_small = $image_array_small[0];
$logo = get_field('logo');
if( $logo ):
// Image variables.
$logourl = $logo['url'];
$logotitle = $logo['title'];
$logoalt = $logo['alt'];
endif;
$website = get_field('website_address');
?>
<div class="slide">
<div class="background-image desktop" style="background-image: url(<?php echo $image_url; ?>);"></div>
<div class="background-image mobile" style="background-image: url(<?php echo $image_url_small; ?>);"></div>
<div class="overlay"></div>
<div class="row">
<div class="col full">
<div class="image" data-aos="fade" data-aos-offset="0" data-aos-duration="2000" data-aos-delay="1000"><?php echo wp_get_attachment_image( $logo ); ?></div>
<div data-aos="fade-up" data-aos-offset="0" data-aos-duration="2000" data-aos-delay="1000" class="buttons">
<a class="button icon-none left" href="<?php echo $website; ?>"><span class="icon left"><span class="nothover"></span><span class="hover"></span></span><span class="text">Visit Website</span><span class="icon right"><span class="nothover"></span><span class="hover"></span></span></a>
<a class="button icon-none right" href="/our-pubs"><span class="icon left"><span class="nothover"></span><span class="hover"></span></span><span class="text">See All Pubs</span><span class="icon right"><span class="nothover"></span><span class="hover"></span></span></a>
</div>
</div>
</div>
<div class="slider-pagination">
<?php
$next_post = get_next_post(true);
if ( ! empty( $next_post ) ): ?>
<a href="<?php echo get_permalink( $next_post->ID ); ?>">
<?php echo apply_filters( 'the_title', $next_post->post_title ); ?>
</a>
<?php else:
$args = array(
'post_type' => 'pub',
'post_status' => 'publish',
'posts_per_page' => 1,
'category__in' => 17,
'order' => 'ASC',
);
$last = new WP_Query($args); $last->the_post();
echo '<a href="' . get_permalink() . '">' . get_the_title() . '</a>';
wp_reset_postdata();
endif;
$prev_post = get_previous_post(true);
if ( ! empty( $prev_post ) ): ?>
<a href="<?php echo get_permalink( $prev_post->ID ); ?>">
<?php echo apply_filters( 'the_title', $prev_post->post_title ); ?>
</a>
<?php else:
$args = array(
'post_type' => 'pub',
'post_status' => 'publish',
'posts_per_page' => 1,
'category__in' => 17,
'order' => 'DESC',
);
$first = new WP_Query($args); $first->the_post();
echo '<a href="' . get_permalink() . '">' . get_the_title() . '</a>';
wp_reset_postdata();
endif; ?>
</div>
</div>
<?php endwhile; else : endif; ?>
</div>
</section>
<?php wp_reset_postdata(); ?>
【问题讨论】:
标签: php wordpress if-statement pagination