Support » Fixing WordPress » Help getting get_category_by_slug to work please!

  • Resolved ransom1337



    I’m trying to assign a little image icon to a post to represent the category it’s in, rather than using the category name. I figured the best way to do this would be to include the slug name in the filename of the image and call the slug in dynamically in category.php:

    <img class="valign-middle" src="images/icon-<?php get_category_by_slug( $slug ) ?>-small.png" />

    However, in Firebug all it’s showing is icon–small.png.

    Can anyone please explain to me where I’m going wrong? I’ve tried searching through the other related threads for this but most of them seem outdated or slightly different to what I’m after.


Viewing 4 replies - 1 through 4 (of 4 total)
  • vtxyzzy


    If the actual slug name is in the file name, you probably don’t need to call get_category_by_slug, just echo the value of $slug.

    <img class="valign-middle" src="images/icon-<?php echo $slug; ?>-small.png" />


    Hey vtxyzzy,

    Thanks for the help. Unfortunately it’s producing the same result. Is there any other code I need to put before that in order to pull the slug in?




    Forum Moderator

    a little image icon to a post to represent the category it’s in

    get_category_by_slug( $slug ) is per-se not producing any output;

    how did you define $slug ?

    if not at all, you will need to get the post’s category (or categories as this could be more than one) first and get $slug from there;


    $post_cats = get_the_category();
    $slug = $post_cats[0]->slug; //the slug of the 'first' category of the post//

    then work with $slug as for instance suggested by @vtxyzzy



    That example worked a treat. Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Help getting get_category_by_slug to work please!’ is closed to new replies.