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

categories - Linking to the most recent post in my podcast category

programmeradmin8浏览0评论

I am attempting to pulloff what the author did here: How can I link to the most recent post in a category?

However, after updating my functions.php file in my child theme I can not get it to load the latest post from the podcast category. When I try:

/podcast/?latest

it returns with a list off all the podcast episodes in the podcast category. When I attempt to goto /category/podcast/?latest

I get a Nothing Found page. I am sure there has got to be something simple I am missing I just dont know what it is. Any help would be greatly appreciated!

The code currently in my functions.php file is:

function wpa_latest_in_category_redirect( $request ){
    if( isset( $_GET['latest'] )
        && isset( $request->query_vars['category_name'] ) ){

        $latest = new WP_Query( array(
            'category_name' => $request->query_vars['category_name'],
            'posts_per_page' => 1
        ) );
        if( $latest->have_posts() ){
            wp_redirect( get_permalink( $latest->post->ID ) );
            exit;
        }

    }
}
add_action( 'parse_request', 'wpa_latest_in_category_redirect' );
发布评论

评论列表(0)

  1. 暂无评论