[Resolved] Template tags for parent/child category separated

Home Forums Support [Resolved] Template tags for parent/child category separated

Home Forums Support Template tags for parent/child category separated

Viewing 15 posts - 1 through 15 (of 34 total)
  • Author
    Posts
  • #1305447
    William

    Hi there,
    Love Generate Press – however, I’ve found an issue that I hope you can provide some code to get around.

    For the elements, I want to have the parent category of the post appear and below it the child category – this is always the case

    What can I do to achieve this with template tags? I know custom fields can do this, but it requires updating every post with custom fields which is not a long term solution.

    Kind regards,

    Will

    #1305605
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    You would likely need to build a shortcode for this.

    Is there always only one parent category? What if there is more than one category for the post?

    #1305614
    William

    Hi there,

    Shortcode or custom field?

    There will always be one parent category, and multiple child categories (book author is parent category and the book titles/s the child categories)

    Kind regards,

    Will

    #1305635
    Tom
    Lead Developer
    Lead Developer

    A shortcode would be best.

    For example:

    add_shortcode( 'child_categories', function() {
        ob_start();
    
        $current_category = get_the_category();
    
        if ( isset( $current_category[0]->term_id ) ) {
            $categories = get_categories(
                array( 'cat_is_ancestor_of' => $current_category[0]->term_id )
            );
    
            var_dump($categories);
    
            if ( $categories ) {
                echo '<ul class="child-categories">';
                foreach( $categories as $category ) {
                    echo '<li>' . $category->cat_name . '</li>';
                }
                echo '</ul>';
            }
        }
    
        return ob_get_clean();
    } );
    #1308517
    William

    Hi there,

    I’ve added the above code, but it does not seem to work?

    I would really appreciate having code that works above to display just the parent category of a post, as well as the child category of the post.

    Many thanks,

    Will

    #1309020
    Tom
    Lead Developer
    Lead Developer

    The code above should display the child categories of the current category. It won’t display the parent category.

    Did the above code output anything at all? Can you link me to a page where it should be displaying?

    #1309033
    William

    I’ve added the code you sent me as a snippet to run on all pages, and then added the shortcode [child_categories] to an element that appears next to the h1 title as a test on posts such as:

    https://bookanalysis.com/albert-camus/the-stranger/analysis/ <– however, nothing appears for this as of yet.

    As well as this, I added the [child-categories] on a parent category page, but that does not seem to show anything too:

    https://bookanalysis.com/albert-camus/

    I only need the child_categories shortcode for post pages, so that it is [Book title] (child category) by [Book author] (parent category) on all posts such as:

    https://bookanalysis.com/albert-camus/the-stranger/analysis/

    Where currently, it displays the categories as [Book title], [Book author]

    Many thanks for your help up to now,

    #1309145
    Tom
    Lead Developer
    Lead Developer

    Got it. Can you share the HTML you’re adding into the Page Hero on “The Stranger” post as of right now? I should be able to tweak it.

    Let me know πŸ™‚

    #1309188
    William

    Sure thing! It’s here (ignore the last bit – was going to stick buttons for affiliate links but commented out for the moment) :

    <table style=”width: 100%;” border=”0″>
    <tbody>
    <tr>
    <td style=”width: 100%;”>

    <h2 style=”font-size:42px; font-weight:bold;”>{{post_terms.category}}</h2>
    <h1 style=”font-size:30px;”>{{post_title}} [child_categories]</h1>

    </td>
    <td style=”width: 0%;”>

    <! –– [wps_double_button left_btn_text=”Buy the book” left_url=”http://bookanalysis.com/purchase/book/{{custom_field.book_title}}” left_btn_target=”blank” left_icon=”book” left_background=”#1abc9c” left_color=”#ffffff” right_btn_text=”Listen on Audible” right_url=”http://bookanalysis.com/purchase/audio/{{custom_field.book_title}}” right_btn_target=”blank” right_icon=”file-audio-o” right_background=”#02a4ef” right_color=”#ffffff” size=”27″ radius=”0″] ––>
    </td>
    </tr>
    </tbody>
    </table>

    #1309189
    William

    I stuck the child categories in at any point to see if it works, which unfortunately it doesn’t seem to show up as of yet.

    #1310059
    Tom
    Lead Developer
    Lead Developer

    Thanks.

    So, The Stranger, Albert Camus are both parent categories (there are two).

    Analysis is the post title, and the child categories are supposed to show up next to it.

    Can you try the updated code?: https://generatepress.com/forums/topic/template-tags-for-parent-child-category-separated/#post-1305635

    #1312251
    William

    Hi there,
    ‘Albert Camus’ is a parent category – ‘The Stranger’ is currently set as the child category to ‘Albert Camus’.

    ‘Analysis’ is the post title – I currently have the child category shortcode next to it to see if it works.

    Unfortunately, the code still does not spit out anything, parent/child categories are still joined by the single category shortcode.

    #1312903
    Tom
    Lead Developer
    Lead Developer

    Let’s debug a little. Does anything output with the updated code?: https://generatepress.com/forums/topic/template-tags-for-parent-child-category-separated/#post-1305635

    #1313117
    William

    Unfortunately not it seems

    #1313118
    William

    I’m happy to provide access the wp-admin if that would help you out?

Viewing 15 posts - 1 through 15 (of 34 total)
  • You must be logged in to reply to this topic.