最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

categories - How to Link to Most Recent Custom Post of Same Term

programmeradmin4浏览0评论

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

2 Answers 2

Reset to default 1

I 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

发布评论

评论列表(0)

  1. 暂无评论