简体   繁体   English

WordPress 自定义帖子类型的类别内的“循环”分页

[英]WordPress 'circular' pagination within category for custom post type

I'm trying to build a post slider in WordPress, showing custom posts from a specific category, and adding pagination on the posts.我正在尝试在 WordPress 中构建帖子 slider,显示来自特定类别的自定义帖子,并在帖子上添加分页。 I want this pagination to be circular, so that the last post links to the first post and vice versa.我希望这个分页是循环的,以便最后一篇文章链接到第一篇文章,反之亦然。

It's almost working perfectly, but for some reason the most recent post 'previous' link is linking to itself rather than the previous post (the 'next' link is correctly going to the oldest post in that category) and I'm stumped as to why.它几乎可以完美运行,但由于某种原因,最近发布的“上一个”链接链接到自身而不是上一个帖子(“下一个”链接正确地指向该类别中最旧的帖子),我很难过为什么。

If I just use如果我只是使用

<?php next_post_link('%link', '%title', TRUE); ?>
<?php previous_post_link('%link', '%title', TRUE); ?>

it works correctly but without being circular.它工作正常,但不是圆形的。 But if I add in the If statements to show the first/last links if there isn't a next/prev item, the most recent post stops thinking it's got a previous post to link to even though it has.但是,如果我添加 If 语句以在没有下一个/上一个项目的情况下显示第一个/最后一个链接,那么最近的帖子停止认为它有一个以前的帖子可以链接到,即使它有。

This is my full code:这是我的完整代码:

<?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(); ?>

Really interesting question.真的很有趣的问题。

As per your comments.根据您的评论。 In a single.php / singular.php context.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; ?>

Based on the code by amarinediary I made some slight changes to include the category filter and got it working: Full code below:根据 amarinediary 的代码,我做了一些细微的更改以包含类别过滤器并使其正常工作:完整代码如下:

<?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">
    <div class="prev">
        <?php if( ! empty( get_next_post(true) ) ):
        echo '<a href="' . get_the_permalink( get_next_post(true)->ID ) . '">' . get_the_title( get_next_post(true)->ID ) . '</a>';
        else:
        echo '<a href="' . get_the_permalink( get_posts( "post_type=pub&numberposts=1&order=ASC&cat=17")[0]->ID ) . '">' . get_the_title( get_posts( "post_type=pub&numberposts=1&order=ASC&cat=17")[0]->ID ) . '</a>';
        endif; ?>
    </div>
    <div class="next">
        <?php   if( ! empty( get_previous_post(true) ) ):
        echo '<a href="' . get_the_permalink( get_previous_post(true)->ID ) . '">' . get_the_title( get_previous_post(true)->ID ) . '</a>';
        else:
        echo '<a href="' . get_the_permalink( get_posts ("post_type=pub&numberposts=1&order=DESC&cat=17")[0]->ID ) . '">' . get_the_title( get_posts( "post_type=pub&numberposts=1&order=DESC&cat=17")[0]->ID ) . '</a>';
        endif; ?>
    </div>
</div>
<?php endwhile; ?>
</div> <!-- end slide-->
</div> <!-- end MySlider-->
</section>
<?php  endif;  wp_reset_postdata(); ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM