- This topic has 33 replies, 2 voices, and was last updated 3 years, 8 months ago by Tom.
-
AuthorPosts
-
May 28, 2020 at 11:41 am #1305447William
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
May 28, 2020 at 2:18 pm #1305605TomLead DeveloperLead DeveloperHi 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?
May 28, 2020 at 2:27 pm #1305614WilliamHi 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
May 28, 2020 at 2:54 pm #1305635TomLead DeveloperLead DeveloperA 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(); } );
May 31, 2020 at 5:13 am #1308517WilliamHi 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
May 31, 2020 at 11:34 am #1309020TomLead DeveloperLead DeveloperThe 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?
May 31, 2020 at 11:49 am #1309033WilliamI’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,
May 31, 2020 at 2:33 pm #1309145TomLead DeveloperLead DeveloperGot 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 π
May 31, 2020 at 3:44 pm #1309188WilliamSure 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>May 31, 2020 at 3:44 pm #1309189WilliamI 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.
June 1, 2020 at 9:25 am #1310059TomLead DeveloperLead DeveloperThanks.
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
June 3, 2020 at 3:44 am #1312251WilliamHi 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.
June 3, 2020 at 9:42 am #1312903TomLead DeveloperLead DeveloperLet’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
June 3, 2020 at 12:24 pm #1313117WilliamUnfortunately not it seems
June 3, 2020 at 12:24 pm #1313118WilliamI’m happy to provide access the wp-admin if that would help you out?
-
AuthorPosts
- You must be logged in to reply to this topic.