【问题标题】:WordPress 'circular' pagination within category for custom post type自定义帖子类型的类别内的WordPress“循环”分页
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      非常有趣的问题。

      根据您的 cmets。在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; ?>
      

      【讨论】:

      • 嗨,谢谢你,但我认为我们在谈论不同的目的 - 看起来你编写的代码是用于帖子页面之间的分页,对吧?而我说的是对个别帖子本身的分页 - next_post_link() 而不是 next_posts_link()。除非我误解了你的评论?
      • 同样的,你可以适应它。
      • 但它并没有链接到帖子本身,是吗?看起来它正在链接到该帖子类型的搜索结果的第一页(或最后一页) - 我正在尝试获取实际帖子的链接(和标题)。
      • 好的,明白你的意思。我已对答案添加了编辑。
      • 啊,太棒了,谢谢,这让我到达了我需要的地方! :-)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-15
      • 2011-03-20
      • 2014-05-21
      • 1970-01-01
      • 2022-07-13
      • 2011-06-24
      相关资源
      最近更新 更多