[resolved] Help getting get_category_by_slug to work please! (5 posts)

  1. ransom1337
    Posted 3 years ago #


    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.


  2. vtxyzzy
    Posted 3 years ago #

    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" />
  3. ransom1337
    Posted 3 years ago #

    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?


  4. Michael
    Part-Time Forum Moderator
    Posted 3 years ago #

    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

  5. ransom1337
    Posted 3 years ago #

    That example worked a treat. Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.