admin管理员组文章数量:1197157
I have a custom post type ('stories') that uses a custom template (single-stories.php). I'd like to add pagination to each single-stories template that will link to previous and next posts. I'm using the wp_link_pages function, but nothing is displaying. Is this because it's a custom post type? How can I achieve this?
<?php
get_header();
?>
<div id="content" class="site-content pt-5 mt-5">
<div class="container-fluid">
<?php $backgroundImg = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full'); ?>
<section id="page_header" class="text-center post_header mb-5" style="background: url('<?php echo $backgroundImg[0]; ?>') no-repeat center center; background-size: cover">
<!-- <h1 class="mb-2"><?php the_title(); ?></h1> -->
<p class="text-light mb-0"><?php the_field('page_description'); ?></p>
<div class="overlay"></div>
</section>
</div>
<div class="container" <div id="primary" class="content-area">
<!-- Hook to add something nice -->
<?php bs_after_primary(); ?>
<main id="main" class="site-main">
<header class="entry-header">
<?php the_post(); ?>
<!-- Title -->
<!-- Featured Image-->
<!-- .entry-header -->
</header>
<div class="entry-content">
<h1 class="mb-2"><?php the_title(); ?></h1>
<div class="date-wrapper">
<h5><?php echo get_the_date(); ?></h5>
</div>
<!-- Content -->
<?php the_content(); ?>
<!-- .entry-content -->
<?php
wp_link_pages(array(
'before' => '<div class="page-links">' . esc_html__('Pages:', 'bootscore'),
'after' => '</div>',
));
?>
</div>
<footer class="entry-footer">
</footer>
</div><!-- #content -->
<?php
get_footer();
I have a custom post type ('stories') that uses a custom template (single-stories.php). I'd like to add pagination to each single-stories template that will link to previous and next posts. I'm using the wp_link_pages function, but nothing is displaying. Is this because it's a custom post type? How can I achieve this?
<?php
get_header();
?>
<div id="content" class="site-content pt-5 mt-5">
<div class="container-fluid">
<?php $backgroundImg = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full'); ?>
<section id="page_header" class="text-center post_header mb-5" style="background: url('<?php echo $backgroundImg[0]; ?>') no-repeat center center; background-size: cover">
<!-- <h1 class="mb-2"><?php the_title(); ?></h1> -->
<p class="text-light mb-0"><?php the_field('page_description'); ?></p>
<div class="overlay"></div>
</section>
</div>
<div class="container" <div id="primary" class="content-area">
<!-- Hook to add something nice -->
<?php bs_after_primary(); ?>
<main id="main" class="site-main">
<header class="entry-header">
<?php the_post(); ?>
<!-- Title -->
<!-- Featured Image-->
<!-- .entry-header -->
</header>
<div class="entry-content">
<h1 class="mb-2"><?php the_title(); ?></h1>
<div class="date-wrapper">
<h5><?php echo get_the_date(); ?></h5>
</div>
<!-- Content -->
<?php the_content(); ?>
<!-- .entry-content -->
<?php
wp_link_pages(array(
'before' => '<div class="page-links">' . esc_html__('Pages:', 'bootscore'),
'after' => '</div>',
));
?>
</div>
<footer class="entry-footer">
</footer>
</div><!-- #content -->
<?php
get_footer();
Share
Improve this question
asked Jul 19, 2022 at 16:25
RRhodesRRhodes
1053 bronze badges
1 Answer
Reset to default 0Hope, the below code is helpful for you to write a custom pagination link.
$post_id = $post->ID; // current post ID
$args = array(
'post_type' => 'stories',
'orderby' => 'post_date',
'order' => 'DESC'
);
$posts = get_posts( $args );
// get IDs of posts retrieved from get_posts
$ids = array();
if(isset($posts) && !empty($posts)) {
foreach ( $posts as $thepost ) {
$ids[] = $thepost->ID;
}
// get and echo previous and next post in the same category
$thisindex = array_search( $post_id, $ids );
$previd = isset( $ids[ $thisindex - 1 ] ) ? $ids[ $thisindex - 1 ] : false;
$nextid = isset( $ids[ $thisindex + 1 ] ) ? $ids[ $thisindex + 1 ] : false;
if (false !== $previd ) {
?><a rel="prev" href="<?php echo get_the_permalink($previd) ?>">Previous</a><?php
}
if (false !== $nextid ) {
?><a rel="next" href="<?php echo get_the_permalink($nextid) ?>">Next</a><?php
}
}
本文标签: Add pagination to custom single post template
版权声明:本文标题:Add pagination to custom single post template 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738495517a2089962.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论