Archived topic
Static link in dynamic headline element
12 replies · Started by George on October 5, 2021
I am using a dynamic headline element to display the author name but need the link to go to the About Us page. Can you help?
Hi George,
You may have to do it with filter.
Example:
add_filter( 'generate_dynamic_element_text', function( $post_author, $block ){
if ( 'post-author' === $block['attrs']['gpDynamicTextType'] && 'dynamic-author' === $block['attrs']['className']) {
$pageID = 3;
$tag = 'h5';
$link = get_permalink( $pageID );
$post_author = '<a href="'.$link.'"><'.$tag.' class="'.$block['attrs']['className'].'">'.get_the_author(get_the_ID()).'</'.$tag.'></a>';
}
return $post_author;
},15,2);
This completely overrides the tag and the link to your preference. Change the value of $pageID to the ID of About us page and change the tag from h5 to your preferred html tag.
Doesn't seem to be working. The author will still link to the dynamic link or to no link if a dynamic link is not applied. Author meta is being displayed at the top of every single post like the attached.
Hi George,
are you using GB Pro ? If so the simplest solution would be to place the dynamic author name inside a Container Block, and then give the container a static link.
Hmmm. All the meta info is already inside a grid container as inline width headlines. Can't use an inline container unless I use a grid and I would need to use one with the new flex functionality currently in Beta. I could use inline post items but those are not responsive, they always stay in one line.
There should be nothing stopping you adding a separate container block inside your inline grid container. Should behave itself.
No, what I mean is that the author meta is constructed with a 2-column grid. Left is the avatar, right is all the inline headlines separated by right margins. I can't add a container in there, it will go full-width.
So your second grid container is set to Inline post meta items if you add a Container Block inside it, it will go inline and its size will shrink to whatever is inside it ( in this case that would be the author meta ).
No, I don't use inline post meta items because they always stay in one line and I was them to wrap on mobile, that's why I use headline elements set to inline-width.
I managed to do it with ACF and a custom link field in the user meta, you can see it in the example URL, it just seems overkill to install ACF for this. Isn't there a way to override the particular dynamic text element?
I managed to do it with ACF and a custom link field in the user meta, you can see it in the example URL, it just seems overkill to install ACF for this. Isn’t there a way to override the particular dynamic text element?
For the snippet I've provided, I forgot to mention you should keep the option for the link unset and add dynamic-author class on it. It won't work without it. My bad. :D
The filter I've previously provided is an example override for dynamic elements. The purpose of adding the class was for specificity :)
Say for example, on this filter:
add_filter( 'generate_dynamic_element_text', function( $post_author, $block ){
if ( 'post-author' === $block['attrs']['gpDynamicTextType'] && 'dynamic-author' === $block['attrs']['className']) {
$post_author = 'do your code override here';
}
return $post_author;
},15,2);
The condition basically checks for a dynamic block that was set to Post author with a class of dynamic-author. If found, it applies the override to it and returns it. :D
It works perfectly, thanks Elvin!
No problem. Glad you got it to work. :D