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('« %link') ?></div>
<?php }?>
<?php if (strlen(get_next_post()->post_title) > 0) { ?>
<div class="alignright"><?php next_post_link('%link «') ?></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('« %link') ?></div>
<?php }?>
<?php if (strlen(get_next_post()->post_title) > 0) { ?>
<div class="alignright"><?php next_post_link('%link «') ?></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 badges1 Answer
Reset to default 0You 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">« %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 «</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
版权声明:本文标题:php - How to put a before and after with tags in a wordpress entry? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745225078a2648569.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论