Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have this URL:

and this code on search.php:

$searchTxt = trim( strip_tags( esc_attr( get_search_query() ) ) );
$state_append = '';
$state = $_GET['cp_state'];
if($state != '0') 
    $state_append = __('', APP_TD).' ' . $state;

$cat_id =  $_GET['scat'];
if ( $searchTxt ==  __('What are you looking for? ', APP_TD) || $searchTxt == __('[All]', APP_TD) ) 
    $searchTxt = '*';   

    if($searchTxt == '*') {
        if($cat = get_term_by('id', $cat_id, 'ad_cat')) {
            printf( __("Ads %s %s ", APP_TD), $cat->name, $state_append, $wp_query->found_posts ); 
        } else {
            printf( __("Ads %s ", APP_TD), $state_append, $wp_query->found_posts ); 
    } else {
        printf( __("What are you looking for? '%s' %s ", APP_TD), $searchTxt, $state_append,      $wp_query->found_posts ); 
    $searchTxt = trim( strip_tags( esc_attr( get_search_query() ) ) );
    printf( __("What are you looking for? '%s' %s ", APP_TD), $searchTxt, $state_append,      $wp_query->found_posts );
echo $cat_id;

I would like to get the category name of the URL. I try to use echo $cat_id, but it gives me the number and not the name of the category.

How can I "echo" the name of category instead the number?

share|improve this question
add comment

1 Answer

You are already using the get_term_by() method in your code. That's the way to get information about your category, i.e.:

$myterm = get_term_by('id', $cat_id, 'category');
if ( $myterm ) {
    echo $myterm->name;

PS. I used 'category' as taxonomy, you might have a different term (i.e. ad_cat).

share|improve this answer
Thank you works 100% –  RAN Mar 6 '13 at 8:09
@RAN, check this: Accepting Answers: How does it work? –  brasofilo Mar 6 '13 at 14:08
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.