Wordpress get category link

前端 未结 2 1488
猫巷女王i
猫巷女王i 2021-01-14 10:04

I want to ask on how to get the link of each category that have related post on it. I only got some code that will only display parent category. Any help would be much appre

相关标签:
2条回答
  • 2021-01-14 10:13

    According to Function Reference/get category link

    <?php get_category_link( $category_id ); ?> 
    

    Example:

    <?php
    // Get the ID of a given category
    $category_id = get_cat_ID( 'Category Name' );
    
    // Get the URL of this category
    $category_link = get_category_link( $category_id );
    ?>
    
    <!-- Print a link to this category -->
    <a href="<?php echo esc_url( $category_link ); ?>" title="Category Name">Category Name</a>
    
    0 讨论(0)
  • 2021-01-14 10:37

    Sounds like you may want get_category_link -- something like

    $categories = get_categories();
    foreach ($categories as $cat) {
       $category_link = get_category_link($cat->cat_ID);
       echo '<a href="'.esc_url( $category_link ).'" title="'.esc_attr($cat->name).'">'.$cat->name.'</a>';
    }
    

    should print out the links to the categories for you.

    0 讨论(0)
提交回复
热议问题