2013-05-11 45 views
0

我加載在WordPress的類別列表使用此功能:WordPress的分類列表鏈接的帖子發表

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

誰能告訴我怎樣才能讓類別鏈接鏈接到第一類張貼?

即:

目前A類的鏈接網址... /分類/類別的一個

我想A類鏈接到它的第一個帖子這樣反而網址... /類別-a /第一後

我試圖chaning其工作witht他的分類模板如下:

<?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)); 
} 
} 
?> 

我只是需要一個更簡潔的解決方案,我不需要修改實際wordp雷斯文件。

感謝

+0

似乎'wp_list_cats()'被棄用,使用'wp_list_categories()' – Danijel 2013-05-11 19:50:42

回答

0

最好的,我可以拿出,$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> 

希望它有幫助。

相關問題