WordPress.org

Ready to get started?Download WordPress

Forums

Displaying names of categories after multiple category query (5 posts)

  1. Kreittis
    Member
    Posted 1 year ago #

    Hi,

    when I link to category like this: http://website/?cat=1 the single_cat_title() works just like it should and displays the name of the category. But if I link to multiple categories like this: http://website/?cat=1,3 then it just shows the name of one category.

    Is there any way to display both category names when linking like that? I found this support topic but I don't understand if the code in that topic is supposed to work as-is or where it's meant to be placed: http://wordpress.org/support/topic/dislaying-title-of-two-category-after-search

  2. David Gard
    Member
    Posted 1 year ago #

    You'd need to grab the categories, turn them in to an array, and then output the title for each using the get_terms(); function.

    $cats = explode(',', $_REQUEST['cat']);
    $args = array(
        'include' => $cats,
        'field' => 'name'
    );
    $term_names = get_terms('category', $args)
    
    if(!empty($term_names)) : foreach($term_names as $term_name) :
    
            echo $term_name;
    
        endforeach;
    endif;

    Code is untested, so may contain a typo or two.

  3. Kreittis
    Member
    Posted 1 year ago #

    I'm very bad at PHP and hacking WordPress so please bear with me.

    I assume that the code you provided goes after The Loop and then I just replace single_cat_title(); with get_terms( $term_name );, am I correct?

  4. Kreittis
    Member
    Posted 1 year ago #

    I hate to ask again but I really need help.

    Is the above code provided by duck__boy meant to be replacement for single_cat_title();? I tried to replace it with that but it didn't do anything, multi category query still shows just one title.

  5. Kreittis
    Member
    Posted 1 year ago #

    I still haven't resolved this problem so let's try this again.

    Here is my full category.php code: http://pastebin.com/LDwrDaxh

    Now then, if I replace single_cat_title() with

    $cats = explode(',', $_REQUEST['cat']);
    $args = array(
        'include' => $cats,
        'field' => 'name'
    );
    $term_names = get_terms('category', $args);
    
    if(!empty($term_names)) : foreach($term_names as $term_name) :
    
            echo $term_name;
    
       endforeach;
    endif;

    The content section goes blank. How would I resolve this?

Topic Closed

This topic has been closed to new replies.

About this Topic