admin管理员组

文章数量:1415119

I'm trying to make two "before" and "after" buttons on each wordpress entry, but with related articles by tags. I have this code, but it doesn't work because it doesn't show me the related articles by tags

<?php if (strlen(get_previous_post()->post_title) > 0) { ?>
 <div class="alignleft"><?php previous_post_link('&laquo; %link') ?></div>
 <?php }?>
 <?php if (strlen(get_next_post()->post_title) > 0) { ?>
 <div class="alignright"><?php next_post_link('%link &laquo;') ?></div>
 <?php }?>

Any idea to put related pages (before and after) in each entry? Thank you

I'm trying to make two "before" and "after" buttons on each wordpress entry, but with related articles by tags. I have this code, but it doesn't work because it doesn't show me the related articles by tags

<?php if (strlen(get_previous_post()->post_title) > 0) { ?>
 <div class="alignleft"><?php previous_post_link('&laquo; %link') ?></div>
 <?php }?>
 <?php if (strlen(get_next_post()->post_title) > 0) { ?>
 <div class="alignright"><?php next_post_link('%link &laquo;') ?></div>
 <?php }?>

Any idea to put related pages (before and after) in each entry? Thank you

Share Improve this question asked Aug 19, 2019 at 15:40 juanjuan 12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You can use get_adjacent_post() to get the nexy/prev post by tag. I think this should work,

// get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy )
$next_post = get_adjacent_post( true, '', false, 'post_tag' );
$prev_post = get_adjacent_post( true, '', true, 'post_tag' );

if ( $prev_post instanceof WP_Post ) {
  printf(
    '<div class="alignleft"><a href="%s">&laquo; %s</a></div>',
    esc_url( get_permalink( $prev_post->ID ) ),
    esc_html( $prev_post->post_title )
  );
}

if ( $next_post instanceof WP_Post ) {
  printf(
    '<div class="alignright"><a href="%s">%s &laquo;</a></div>',
    esc_url( get_permalink( $next_post->ID ) ),
    esc_html( $next_post->post_title )
  );
}

本文标签: phpHow to put a before and after with tags in a wordpress entry