Php WordPress';圆形';自定义帖子类型的类别内分页
我试图在WordPress中构建一个帖子滑块,显示特定类别的自定义帖子,并在帖子上添加分页。我希望此分页是循环的,以便最后一篇文章链接到第一篇文章,反之亦然 它几乎工作得很完美,但出于某种原因,最近的帖子“上一篇”链接是链接到它自己,而不是上一篇帖子(“下一篇”链接正确地指向该类别中最早的帖子),对此我感到困惑 如果我用Php WordPress';圆形';自定义帖子类型的类别内分页,php,wordpress,if-statement,pagination,Php,Wordpress,If Statement,Pagination,我试图在WordPress中构建一个帖子滑块,显示特定类别的自定义帖子,并在帖子上添加分页。我希望此分页是循环的,以便最后一篇文章链接到第一篇文章,反之亦然 它几乎工作得很完美,但出于某种原因,最近的帖子“上一篇”链接是链接到它自己,而不是上一篇帖子(“下一篇”链接正确地指向该类别中最早的帖子),对此我感到困惑 如果我用 <?php next_post_link('%link', '%title', TRUE); ?> <?php previous_post_link('%li
<?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(); ?>
真是个有趣的问题
根据你的评论。在single.php
/single.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; ?>
基于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(); ?>
你好,谢谢你这么说,但我认为我们的讨论是有分歧的——看起来你写的代码是用于在文章页面之间分页的,对吗?而我说的是对单个帖子本身的分页——next_post_link()而不是next_posts_link()。除非我误解了你的评论?这是一样的,你可以修改它。但是它没有链接到帖子本身,是吗?它看起来像是链接到该帖子类型的搜索结果的第一页(或最后一页)-我正在尝试获取实际帖子的链接(和标题)。好的,明白你的意思了。我在答案中添加了一个编辑。啊,太棒了,谢谢,这让我达到了我需要的位置!:-)