Tagged: metadata
-
AuthorPosts
-
November 8, 2021 at 4:10 pm #1328238
Hi,
I want to use the Metadata content element to present the categories of a page. However I don’t want it to show all categories, but only the categories within a certain category. So I would like to be able to choose a sub category, and the output will be that it only shows the child items within that category.
I added this screenshot to hopefully make it more clear what I mean:
https://drive.google.com/file/d/1BT8vHdyZpEJyX18q0IieFVy0PsH8hVrl/view?usp=sharingIs this something that could be done?
Hope to hear from you. Thanks in advance.
ArnoutNovember 9, 2021 at 7:58 am #1328306Hey arnoutc,
Thank you for the inquiry.
The option is not available by default but you could create a custom shortcode that displays the child categories of the selected parent category using the get_term_children function.
// https://codex.wordpress.org/Shortcode_API
// https://developer.wordpress.org/reference/functions/get_term_children/Best regards,
IsmaelNovember 15, 2021 at 1:24 pm #1329089Hi Ismael,
Thanks for your quick reply!
Sorry for asking, since I’m quite a rookie in PHP. But in which file should I add this shortcode? To the file taxonomy.php? or something like metadata.php?I’m not yet sure if this would bring the solution I’m looking for. I would indeed like to “displays the child categories of the selected parent category” as you described. So I would like to be able to select the taxonomy “Categories” (through the Metadata component) and then “Category X” for example. Then I would like to display the childe items/categories within that parent category. However at the moment in the Metadata component I’m only able to select “Categories”, period.
Hope this clears it up.Looking forward to your reply. Already appreciating the help!
Best regards,
ArnoutNovember 15, 2021 at 1:46 pm #1329090Hi,
Thank you for the update.
We created a custom shortcode that should render the child categories of a given parent category. You have to add this code in the functions.php file.
// get parent category children function avs_get_term_children( $atts ) { $a = shortcode_atts( array( 'parent_id' => null, ), $atts ); $term_output = ''; $term_id = $a['parent_id']; $taxonomy_name = 'category'; $termchildren = get_term_children( $term_id, $taxonomy_name ); if($termchildren) { ob_start(); echo '<ul>'; foreach ( $termchildren as $child ) { $term = get_term_by( 'id', $child, $taxonomy_name ); echo ' <li><a href="' . get_term_link( $child, $taxonomy_name ) . '">' . $term->name . '</a></li>'; } echo '</ul>'; $term_output = ob_get_clean(); } return $term_output; } add_shortcode( 'avs_term_children', 'avs_get_term_children' );
You can use the shortcode in a text or code block like this.
[avs_term_children parent_id=2]
Make sure to place the actual ID of the parent category as the value of the parent_id parameter.
Best regards,
IsmaelNovember 15, 2021 at 2:15 pm #1329102Hi Ismael, thanks a lot for your quick reply again. I see the code working and showing me all the child categories of a parent category. However, I only want it to display the categories as selected for a particular page.
As the metadata component does. It’s just that the metadata component shows all the categories that are selected for a particular page. And I want it to only display the selected child items of a selected parent category.Sorry if this wasn’t clear yet, hope it is now. If not, please let me know!
ArnoutNovember 19, 2021 at 5:56 am #1329612Hi,
So I would like to be able to choose a sub category, and the output will be that it only shows the child items within that category.
This was in your initial reply and the custom shortcode does exactly that. It shows the child categories of the selected or specified parent category.
Unfortunately, extending the custom shortcode above will require modifications that is beyond the scope of support and is considered custom work. You may need to hire a freelance developer or contact our partner (Codeable) to modify and extend the shortcode according to your specifications.
Thank you for your understanding.
Best regards,
IsmaelNovember 22, 2021 at 9:14 am #1329899Hi Ismael,
Sorry for the confusion, but as I also explained in my initial reply I was using the Metadata component. So I don’t just want to show (sub)categories, I only want to display the categories as selected for a particular page.And the Metadata component does that, but i shows them all. And because there are multiple levels of categories for this website, I want to be able to choose a specific (sub)category as a parent and only show the categories within that, as selected for that particular post or page (therefore metadata).
Perhaps this will explain it better.
A page has these categories selected:
cat Regions
– cat Amsterdam, Rotterdam
cat Discipline
– cat Architecture, Theatre, WritingI want to display the sub items, by creating several Metadata components like this
1. Regions (title)
Amsterdam, Rotterdam2. Discipline (title)
Architecture, Theatre, WritingI hope this will explain it best. Please let me know if you have any questions and thanks in advance for looking into this.
Best regards,
ArnoutNovember 24, 2021 at 3:43 am #1330208Hi,
Thank you for the info.
We understand what you are trying to do but unfortunately, this kind of customization is beyond the scope of support. You will have to hire a freelance developer or find an agency that will help you extend the Metadata shortcode.
Best regards,
Ismael -
AuthorPosts
- You must be logged in to reply to this topic.