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
Add a comment  | 

1 Answer 1

Reset to default 0

Hope, 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