2017-03-04 122 views
1

去几个谷歌搜索后,我完成这个代码(在PHP不太好)WordPress的帖子类别名称的链接

 <div> 

    <?php 

    $args = array(
     'post_type' => 'post' 
    ); 

    $categories = get_categories($args); 

    $catlinks = get_category_link($categories); 


    foreach ($categories as $category) { 

     echo '<a href=" '.$catlink->link . '"> <h2>' . $category->name .'</h2></a>'; 


     $args['category'] = $category->term_id; 
    } ?> 

</div> 

这个代码显示的WordPress发布分类的循环,即时通讯试图让每个类别链接,但我仍然没有得到正确的链接。

任何提前的帮助将是伟大的。

感谢罗德里戈

回答

1

您已经非常接近了。

您需要在您的foreach循环中针对$category的ID运行get_category_link()

,看起来像这样:

<?php 
foreach ($categories as $category) { 
    echo '<a href="' . get_category_link($category->term_id) . '"> <h2>' . $category->name . '</h2></a>'; 
} 
?> 

所以,都在一起,你的整个代码应为:

<div> 
    <?php 
    $args = array(
     'post_type' => 'post' 
    ); 

    $categories = get_categories($args); 
    foreach ($categories as $category) { 
     echo '<a href="' . get_category_link($category->term_id) . '"> <h2>' . $category->name . '</h2></a>'; 
    } 
    ?> 
</div> 
+0

TYVM曾任职完美! –

相关问题