• Resolved Fery Rohrer

    (@ferylee)


    Hi,
    I try to displaying category descriptions using custom taxonomy and CPT and since I use the Divi visual builder, my idea is to retrieve it by using a shortcode inside the Code Divi Module and defining the function inside functions.php

    The problem is, I can’t make it work within the CPT environment. It just works with the normal post types and default category.

    Here the code I use:

    function wpb_catlist_desc()
    {
        $catID = get_the_category();
        return category_description($catID[0]);
    }
    add_shortcode('cat_desc', 'wpb_catlist_desc');

    …and the shordcode for this is: [cat_desc]

    i would be grateful if someone can help me with this as i dont know what i am doing wrong and i am not PHP expert either, thanks!

    Fery

    The page I need help with: [log in to see the link]

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Couple things to note, if you’re in the archive for a custom taxonomy term, then you’re using the wrong functions, since those are specific for the category taxonomy.

    Perhaps something like:

    function wpb_catlist_desc()
    {
        $catID = get_queried_object_id();
        return term_description( $catID );
    }
    add_shortcode('cat_desc', 'wpb_catlist_desc');
    

    This should get the current ID of the queried taxonomy term, and then pass it into the more appropriate term_description() function to return that value.

    https://developer.www.ads-software.com/reference/functions/get_queried_object_id/
    https://developer.www.ads-software.com/reference/functions/term_description/

    Thread Starter Fery Rohrer

    (@ferylee)

    Hi Michael,

    Thanks for your input! Yes, sounds logic and I see that it has to go in the direction you indicate, but still its strange that also with your code example, in the front-end, the box that contains the short-code is blank and I do not understand why the description does not render…

    https://wordpress-640374-2120378.cloudwaysapps.com/art_gallery_item/nach-oben-und-hinaus-01/

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Are you doing <?php echo do_shortcode('[cat_desc]'); ?> for that portion of it?

    Otherwise, I’d probably end up checking and confirming that get_queried_object_id() is returning the term ID you’re expecting and also extra confirming that that term has a description available. Just for some debugging intents.

    Thread Starter Fery Rohrer

    (@ferylee)

    I thought that adding in functions.php at the end:

    add_shortcode('cat_desc', 'wpb_catlist_desc');

    and inside the custom post type only adding [cat_desc] ( see image: https://prnt.sc/1w4m25k ) should be enough? At least it worked for the default posts and categories.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Ok, this is a bit different. My apologies.

    You were using almost the right function, to fetch the terms from the post being displayed, I unintentionally switched to code that would/could be used when viewing the term archive.

    Because of that, you’d want to try something like

    function wpb_catlist_desc()
    {
        $terms = get_the_terms( get_the_ID(), 'artists' );
        if ( $terms ) {
            $term = $terms[0]; // Grab the first one.
            return term_description( $term->term_id );
        }
        return '';
    }
    add_shortcode('cat_desc', 'wpb_catlist_desc');
    

    Make sure to change ‘artists’ to your actual taxonomy slug.

    Thread Starter Fery Rohrer

    (@ferylee)

    Hello Michael,
    Please don’t apologize, you just helped me a lot with this code, now it is working as expected ??????

    Thanks again so so much,
    Fery

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Woohoo! working code! ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Displaying category descriptions using custom taxonomy and cpt’ is closed to new replies.