WordPress Category List Link to First Post

I am loading a category list in wordpress by using this function:

<?php wp_list_cats($args = array( 'current_category'   => 0, 'hide_empty'  => 1) );?>

Can anyone tell me how I can make the category links link to the category first post?

Read More

ie:

At the moment Category A links to url…/category/category-a

I would like Category A to link to its first post instead so url…/category-a/first-post

I tried chaning the taxonomy template which worked witht he following:

<?php
/*
Redirect To First Child
*/
if (have_posts()) {
 while (have_posts()) {
   the_post();
   $pagekids = get_pages("child_of=".$post->ID."&sort_column=menu_order");
   $firstchild = $pagekids[0];
   wp_redirect(get_permalink($firstchild->ID));
 }
}
?>

I just need a neater solution where i dont need to modify the actual wordpress files.

Thanks

Related posts

Leave a Reply

1 comment

  1. Best that i can come up with, $posts array now contains links to first post in each category. Put this code in functions.php:

    function first_categories( $echo = false ) {
        $categories = get_categories();
    
        // array to hold links
        $posts = array();
        foreach ( $categories as $category ) {
            $post = get_posts( array( 'posts_per_page' => 1, 'post_type' => 'post', 'category' => $category->cat_ID ) );
            $posts[] = '<a href="'.get_permalink( $post[0]->ID ).'">'.$category->name.'</a>';
        }
    
        if ( $echo ) echo implode( '', $posts );
        else return $posts;
    }
    

    In template file to just show the links use:

    <?php first_categories( true ) ?>
    

    Or, if you want to wrap links within HTML use something like:

    <ul>
    <?php foreach( first_categories() as $category ) : ?>
        <li><?php echo $category; ?></li>
    <?php endforeach; ?>
    </ul>
    

    Hope it helps.