- This topic has 5 replies, 2 voices, and was last updated 3 years, 7 months ago by
Fernando.
-
AuthorPosts
-
September 21, 2022 at 7:25 pm #2350168
Michael
Hi there,
I am trying to dynamically create an external link based on the post tag of each post.
For example, if the tag is Coffee, I want to create a link that generates: google.com/coffee
I was planning on inserting on content-single.php
Does anyone know how this can be done?
Thanks
September 21, 2022 at 8:05 pm #2350186Fernando Customer Support
Hi Michael,
To clarify, if there’s multiple tags, will there also be multiple links?
Have you considered creating a custom post meta field for “coffee” and whatever custom link you want for each post?
September 21, 2022 at 8:41 pm #2350193Michael
Actually on second thought, rather than using the tag, how about using a custom field?
One follow up question to that, is there an easy way to bulk upload a custom field to all my posts?
I have post ID and the desired text in a CSV.
September 21, 2022 at 8:58 pm #2350203Fernando Customer Support
How many tags/field values will you have?
We usually advice our users to use a plugin like ACF. It’s capable of adding field, but I’m not sure if there’s a way to add values to fields in bulk. Perhaps you could reach out to their support if it’s possible or ask for recommendations in our Facebook community as well: https://www.facebook.com/groups/generatepress
September 21, 2022 at 9:01 pm #2350205Michael
Thanks! Last question… for now.
Let’s say I do have custom fields. How can I get it to print for example: google.com/{custom_field}
September 21, 2022 at 9:14 pm #2350208Fernando Customer Support
You can create a GB Headline Block, then add
class my-headline-linkto its class list. Then set the url toplaceholder.comThen, add this snippet:
add_filter( 'render_block', function( $block_content, $block ) { if ( !is_admin() && ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'my-headline-link' ) !== false ) { $myreplace = 'placeholder.com'; $my_meta = get_post_meta( get_the_ID(), 'my-meta-name', false); $my_link = 'google.com'; $my_insert = $my_link . '/' . $my_meta; $block_content1 = str_replace( $myreplace, $myinsert , $block_content ); } return $block_content; }, 10, 2 );Replace
my-meta-nameto the meta field slug andgoogle.comwith the domain url.Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets
-
AuthorPosts
- You must be logged in to reply to this topic.