1

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?

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

4

1 に答える 1

1

私が思いつく限り、$posts配列には各カテゴリの最初の投稿へのリンクが含まれるようになりました。このコードを 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;
}

リンクを表示するだけのテンプレートファイルでは、次を使用します。

<?php first_categories( true ) ?>

または、HTML 内でリンクをラップする場合は、次のようなものを使用します。

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

それが役に立てば幸い。

于 2013-05-11T20:27:26.010 に答える