WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. ransom1337
    Member
    Posted 1 year ago #

    Hello,

    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.

    Thanks.

  2. vtxyzzy
    Member
    Posted 1 year 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
    Member
    Posted 1 year 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?

    Cheers.

  4. alchymyth
    The Sweeper & Moderator
    Posted 1 year 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;

    http://codex.wordpress.org/Function_Reference/get_category_by_slug

    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;
    http://codex.wordpress.org/Function_Reference/get_the_category

    example:

    $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
    Member
    Posted 1 year ago #

    That example worked a treat. Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.