• Resolved paa1605

    (@paa1605)


    Hi guys,

    Im trying to display the category name from the slug as a title on my taxonomy page. Im using the excellent Query Multiple Taxonomies plug-in that allows you to filter posts in a category by the taxonomy terms associated with them.

    For example, if i had a category of clothes, and some taxonomies of colours and sizes, you could then use the plug-in to show all posts that were in the category of clothes, the colour of red, and the size of small. The url would then display something like this:

    mysite.com/?category_name=clothes&colour=reds&size=small

    On the taxonomy page i would like a heading that shows the category_name based on the slug. So in this case it would echo ‘clothes’. Does anyone know how to do this?

    At the moment i have tried two pieces of code, one that unfortunately shows all the queries:

    <?php if (is_multitax()) {
    	foreach ( qmt_get_query() as $tax => $slug ) {
    		$terms[] = get_term_by( 'slug', $slug, $tax )->name;
    	} ?>
    	<h1><?php echo implode( ", ", $terms ); ?></h1>
    <?php } ?>

    and the other that shows only the last selected term:

    <?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); echo $term->name; ?>

    neither of these are what im looking for. Any help would be much appreciated. Thanks

Viewing 6 replies - 1 through 6 (of 6 total)
  • I think all you need to do is get the ‘category_name’ query_var:

    <?php $name = (get_query_var('category_name')) ? get_query_var('category_name') : 'default name here';
    echo $name; ?>
    Thread Starter paa1605

    (@paa1605)

    Thanks vtxyzzy for taking the time to reply, much appreciated.

    I put in the code you provided but all it did was echo ‘default name here’, not the category_name variable. Should i have replaced this text with something else? If so what, as i couldn’t find anything in the codex that explains this in much detail. Thanks.

    You may need to use a different approach. Please try this:

    <?php $name = (isset($_GET['category_name'])) ? $_GET['category_name'] : 'default name here';
    echo $name; ?>
    Thread Starter paa1605

    (@paa1605)

    Fantastic vtxyzzy! That solved it.

    Its not massively important but do you know if its possible to have this code ignore the ‘dash’ that is used in categories with more than one word? For example, if the category name was ‘new cars’ and the slug was ‘new-cars’, can the code you provided be tweaked so that it echoes the category name with a space rather than a dash?

    Thanks ever so much for your help

    Instead of this:

    echo $name;

    you can use this:

    echo str_replace('-',' ',$name);

    If you would like to capitalize all the words in the name, use this:

    echo ucwords(str_replace('-',' ',$name));

    Thread Starter paa1605

    (@paa1605)

    Wonderful. You saved me hours of head scratching! Many thanks for your kind work.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Get the category name from the slug? Help needed please’ is closed to new replies.