admin管理员组文章数量:1334826
Title says it all. I'm trying to link to the most recent post of a custom post type, but only within the same term. Currently my code successfully echos the correct term ID but doesn't show a link on the screen.
<?php
// Get the ID of the current posts's term
$terms = get_the_terms( get_the_ID(), 'comic-series' );
// Only get the parent term and ignore child terms
foreach ( $terms as $term ){
if ( $term->parent == 0 ) {
// Echo the term (this is only for debugging purposes)
echo $term->term_id;
// Take only the most recent post of same term
$args = array( 'numberposts' => '1', 'category' => $term->term_id );
$recent_posts = wp_get_recent_posts( $args );
// Link to most recent post of same term
foreach( $recent_posts as $recent ){ ?>
<a href="<?php get_the_permalink( $recent->ID ); ?>">
>>
</a> <?php
}
}
}
?>
Title says it all. I'm trying to link to the most recent post of a custom post type, but only within the same term. Currently my code successfully echos the correct term ID but doesn't show a link on the screen.
<?php
// Get the ID of the current posts's term
$terms = get_the_terms( get_the_ID(), 'comic-series' );
// Only get the parent term and ignore child terms
foreach ( $terms as $term ){
if ( $term->parent == 0 ) {
// Echo the term (this is only for debugging purposes)
echo $term->term_id;
// Take only the most recent post of same term
$args = array( 'numberposts' => '1', 'category' => $term->term_id );
$recent_posts = wp_get_recent_posts( $args );
// Link to most recent post of same term
foreach( $recent_posts as $recent ){ ?>
<a href="<?php get_the_permalink( $recent->ID ); ?>">
>>
</a> <?php
}
}
}
?>
Share
Improve this question
asked Jul 27, 2020 at 16:26
ZackAkaiZackAkai
434 bronze badges
2 Answers
Reset to default 1I ended up using wp_query instead because I couldn't make wp_get_recent_posts work no matter what I tried. For anyone who needs this functionality in the future, here's the code I used:
// Get the ID of the current posts's term
$terms = get_the_terms( $post->ID, 'comic-series' );
// Only get the parent term and ignore child terms
foreach ( $terms as $term ){
if ( $term->parent == 0 ) {
// Query Options
$query_options = array(
'posts_per_page' => 1,
'orderby' => 'title', // I've ordered all my posts by title but change this for your needs
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'comic-series',
'field' => 'term_id',
'terms' => $term->term_id,
),
),
);
//Query
$the_query = new WP_Query( $query_options );
while ($the_query -> have_posts()) : $the_query -> the_post();
// Link to the latest page
?> <a href="<?php the_permalink(); ?>">LINK TEXT</a> <?php
endwhile;
wp_reset_postdata();
}
}
Not sure if this is the only error, but watch out for WP functions that have get_
on the front, as they return the value in PHP without echoing it to the screen. The ones without will echo it.
So probably you want:
foreach( $recent_posts as $recent ){ ?>
<a href="<?php the_permalink( $recent->ID ); ?>">
>>
</a> <?php
}
Note removal of get_
from the function
本文标签: categoriesHow to Link to Most Recent Custom Post of Same Term
版权声明:本文标题:categories - How to Link to Most Recent Custom Post of Same Term 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742228725a2436817.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论