首页 > 解决方案 > 自定义帖子类型的类别内的 WordPress“循环”分页

问题描述

我正在尝试在 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(); ?>

标签: phpwordpressif-statementpagination

解决方案


真的很有趣的问题。

根据您的评论。在single.php/singular.php上下文中。

<?php
if ( have_posts() ) :
while ( have_posts() ) : the_post();

  the_title( '<h1>', '</h1>' );
  the_content();

endwhile;

  if( ! empty( get_previous_post() ) ):
    echo '<a href="' . get_the_permalink( get_previous_post()->ID ) . '"> << </a>';
  else:
    echo '<a href="' . get_the_permalink( get_posts ("post_type=" . get_query_var( 'post_type' ) . "&numberposts=1&order=DESC")[0]->ID ) . '"> << </a>';
  endif;
  if( ! empty( get_next_post() ) ):
    echo '<a href="' . get_the_permalink( get_next_post()->ID ) . '"> >> </a>';
  else:
    echo '<a href="' . get_the_permalink( get_posts( "post_type=" . get_query_var( 'post_type' ) . "&numberposts=1&order=ASC")[0]->ID ) . '"> >> </a>';
  endif;

endif; ?>

推荐阅读