0

I have this URL:
http://example.com/?s=&cp_state=0&scat=16&sa=search&refine_search=yes

and this code on search.php:

<?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($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 ); 
    }
}else{
    $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?

2

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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