I would like that each category link to the last post of each category.
- category 1 ----> link to the last post of category 1
- category 2 ----> link to the last post of category 2
- category 3 ----> link to the last post of category 3
How can I do it?
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Sign up to join this communityYou can filter category_link
and replace the URL here. I have used the newest post in the following example, because first could also mean the oldest and that sounds … strange. :)
add_filter( 'category_link', 'wpse_96677_cat_link_to_first_post', 10, 2 );
function wpse_96677_cat_link_to_first_post( $url, $term_id )
{
$term = get_term( $term_id, 'category' );
// sub-terms only
if ( ! isset ( $term->parent ) or 0 == $term->parent )
return $url;
$post = get_posts(
array(
'numberposts' => 1,
'category' => $term_id
)
);
if ( ! $post )
return $url;
return get_permalink( $post[0]->ID );
}
http://example.com/category/my-cat/
etc? This is not enough information.