Archived topic
How use dynamic data as menu items
15 replies · Started by Sebastian on August 24, 2022
Hey guys
I'm trying to insert links to a custom taxonomy by using the dynamic text block function, to use as a menu in my footer (element site footer). Problem is, it's only showing up when I'm on the actual archive page. I've tried both "list of terms" as well as "term meta". None is working as I want it to. Is this even possible?
Big thanks
Hi there,
just to be clear, are you wanting a footer menu that is a list of all your custom taxonomy terms ?
Well actually I would like to know how to do both. I would like to insert a menu concisting of links to my custom categories, styled in the same way as my other links in the columns next to it.
And I would also like to know how to insert single links (one per line) pointing to the same thing.
In both cases preferably using the block editor dynamic field.
GP and GB Dynamic data both require a content source and to be inside the loop.
So you can use them to display the tax terms for current post.
But they cannot be used ( today ) to list ALL terms from a specific taxonomy.
You could create a shortcode to display them with this PHP Snippet:
add_shortcode( 'custom_tax', function(){
$tax_args = array(
'orderby' => 'name',
'order' => 'asc',
'hide_empty' => true,
);
$tax_terms = get_terms( 'category', $tax_args );
$html = '';
if ( ! empty( $tax_terms ) ) {
$html = '<ul class="tax-cat-nav">';
foreach ( $tax_terms as $key => $term ) {
$html .= sprintf(
'<li>
<a href="%1$s">
%2$s
</a>
</li>',
get_term_link( $term ),
$term->name
);
}
$html .= '</ul>';
}
return $html;
});
In this line: $tax_terms = get_terms( 'category', $tax_args ); change the category to your taxonomy name.
And then display the list using [custom_tax] shortcode.
It will need some CSS to style it - and i can help with that if you require.
Hi
I changed $tax_terms = get_terms( 'category', $tax_args ); to $tax_terms = get_terms( 'agent_types', $tax_args ); but still only [custom_tax] showing up on the frontend?
When I'm on the admin screen for my categories I have the following up in the address bar: /wp-admin/edit-tags.php?taxonomy=agent_types&post_type=agent. Don't I somehow have to add what post_type I'm referring to in the params?
Where did you add the [custom_tax] shortcode ? Does it work if you add it in the post/page content ?
I put it inside the site footer (elements). Same thing if I put right in the main content (with block editor).
If its showing [custom_tax] then it means that the shortcode is not being recongised.
When you added it to the post content did you use a Shortcode block ?
Yes!
There is no post_type arg for the get_terms function.
The above code should show ALL the terms in the custom taxonomy even if it was attached to many post types.
How was the snippet added to your site ?
You're right, I'm very sorry but I didn't want to type the actual code in the open forum and I totally forgot to mention this in the Private info area. If it's ok I'll give you the details this time.
My issue is:
If the PHP Snippet here is added correctly then you will see either:
a. A list of terms
OR
b. Nothing as the shortcode will return the empty $html.
If you changed the $tax_terms = get_terms( 'agent_types', $tax_args ); and the Custom Taxonomy is not recognised then it will most probably generate an Error.
As that is not happening then it means WordPress does not recognise the [custom_tax] shortcode or it is not allowing shortcodes.
To test can you add this snippet to your site:
add_shortcode( 'hello_shortcode', function() {
return '<p>Hello this shortcode works</p>';
});
Then add the [hello_shortcode] to your site.
This should display that simple message - does that work ?
I already have some custom shortcodes working on the site, so it didn't make no sense.
After some digging I finally found the culprit. For some reason the [...] around the shortcode had been stripped on insertion. I manually typed them back in and voilà, now it's working.
Glad to hear that!
Do you need any assistance with CSS to style the list ?
I'm periodically helplessly lost when it comes to code. CSS however is right up my alley 😉 Thank you David!