Last Updated: February 25, 2016
·
2.042K
· dscotts3

Get permalink for category by category name

This function will allow you to pass in a name of a category as the first paramater which will then echo out the link. If you set the second paramater to false, it will return the permalink to the category.

function get_category_link_by_name( $category, $echo = true ) {
    // Get the ID of a given category
    $category_id = get_cat_ID( $category );

    // Get the URL of this category
    $category_link = get_category_link( $category_id );

    if ( $echo ) : 
        echo $category_link;
    else :
        return $category_link;
    endif;  
}

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Related Tags
#php
#wordpress
Did you know?

PHP applications still send a browser full of JavaScript. Hardening the server does not inventory the payment, analytics, and plugin scripts that run after the HTML is delivered.

Security insights from Cside