• I’ve been searching high and low and can’t seem to find how I can accomplish this.

    I would like a shortcode that I can put into my post where it will display all the categories that post is listed under.

    Any ideas? I’ve been searching for about 1/2 day now and have come up short.

Viewing 14 replies - 1 through 14 (of 14 total)
  • Hi

    Important note: Backup your site and use child themes instead of modifying the parent theme’s code ??

    Now that I’ve got that out of the way , here’s a solution:

    Add this to your functions.php

    function get_cats(){
    $categories = get_the_category();
    $separator = ' ';
    $output = '';
    if ( ! empty( $categories ) ) {
        foreach( $categories as $category ) {
            $output .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" alt="' . esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ) . '">' . esc_html( $category->name ) . '</a>' . $separator;
        }
        echo trim( $output, $separator );
    }}
    
    add_shortcode('getcats', 'get_cats');

    Then use [getcats] in your posts to display a list of categories the post is affiliated to.

    All the best & happy coding!

    Thread Starter katrah

    (@katrah)

    Thank you for the help. First, I do have a child theme installed. However, am yes, very new to Genesis, so any tips are much appreciated. I’ve decided to plunge ahead and start learning at least some basic PHP as well, I think it will be needed now that I’ve actually seen how this works. I’m starting out with CodeAcademy and I’ll see where I should go from there I guess.

    I did put the code you provided into my functions.php, but it didn’t work. Where I put the shortcode in my post it was simply blank. (I did double check the post I tested this on and I do have it listed under a category).

    Hi

    The code I provided works well with on the twenty fifteen theme and a handful of others I tested.

    Perhaps you can contact support for the Genesis framework and ask them to point you in the right direction?

    Hi

    Just a thought, it might also be worth while to try to change the function name and shortcode name to something more unique as part of your bug fixing ??

    Thread Starter katrah

    (@katrah)

    I do appreciate the help. I still cannot seem to get it working. I also managed to get the white screen of death messing around with it last night… lol After that I did some research into how to test these things without the risk and came across a great article on making these additions via your own plugins instead. I turned my other “custom function code” into a plugin instead of placing it into the functions.php and it worked great. (I’m mostly telling this for anyone in the future who comes across this post and encounters a similar problem so they have some solution to go to). Unfortunately the above function is still not working for me.

    my test page: https://allthatfreewallpaper.com/2016/05/17/head-in-the-clouds-wallpaper/

    Thread Starter katrah

    (@katrah)

    okay… I played around with it some and noticed that what you’re code is doing is placing the categories at the top of the post when I use the shortcode rather than placing the categories in the location where I’ve written [getcats]. So you’re right, it does work, it’s just not placing them where I need them.

    Hi Katrah

    Add a class to the output and code some custom css to override the default placement. ??

    Moderator bcworkz

    (@bcworkz)

    Sorry, but CSS is not a good solution in this case. The suggested shortcode handler contains a common error – it is echoing content instead of returning it. This is the cause of misplaced content. All content intended for output by the shortcode should be collected into a single variable that is returned by the handler. The returned value is then output where it belongs by an internal WP operation.

    Thread Starter katrah

    (@katrah)

    Thank you so much bcworkz, that worked beautifully! I was just about to go research how to change the CSS when I decided to check on this post first. I simply changed “echo trim” to “return” and it’s now placing the categories within the post where I’ve placed the shortcode.

    Thank you both so, so much for your help! It’s very appreciated!

    Moderator bcworkz

    (@bcworkz)

    That’s great! You’re welcome. I’m glad you saw my post before going down the less desirable path. I was afraid I was too late to chime in.

    lyonbeton

    (@lyonbeton)

    Hello,
    I’ve test it on a regular blog post and it works fine.
    I wanted to use it on a custom post type of my theme which is a “project” and it doesn’t work.
    Look likes $categories is always empty.
    Any idea on how to make it works?
    thanks.

    Moderator bcworkz

    (@bcworkz)

    @lyonbeton – Once the shortcode handler is corrected by changing “echo” to “return”, it works fine on my custom post type. Are you sure the categories associated with your post type are true categories and not something like “project-cats” that uses the same category labels?

    A simple test. When you add a category term while editing a project, does that term also show up in the categories for regular posts? If so, it’s a true category.

    Is the place you are viewing project content inside a true WP loop that calls the_post()? The project post must be inside “The Loop” for this shortcode to function properly. Check the underlying template to confirm this.

    What theme are you using?

    lyonbeton

    (@lyonbeton)

    Hello,

    thank you for your answer.
    You’re right, my custom post use false categories.
    I make your snippet work by using this:
    $categories =get_the_terms($post->ID, 'project_category' );

    And by the way, I’m not sure how to write code about the “echo” VS “return” thing.
    I’ve replaced this :
    echo trim( $output, $separator );
    By this :
    return $output;
    Was that the right thing to do?

    Thanks again.

    Moderator bcworkz

    (@bcworkz)

    I had in mind
    return trim( $output, $separator );

    The trim function strips the terminal space from the output. If you don’t mind the extra space, what you have is fine.

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Shortcode to display categories the post is listed under on the current post’ is closed to new replies.