-
mpribadi
Hi,
I’m using “Block Hook” element for sticky bar (position in left) act as sets of sharing buttons, I’m using
{{post_permalink}}
to get the current url for generating the share link. Runs perfectly on every post and page.But the share buttons not generating the right permalink for any Archive page, like this one. It fail to get the current URL.
What “dynamic tag” should I use for this case? that will works consistenly for page, post and also archive permalink.
-
Alvind
Hi there,
The {{post_permalink}} tag doesn’t work on archive pages because it uses the
get_permalink()
function under the hood, which only retrieves the permalink for a single post or page. Unfortunately, this means it won’t function as expected on archive pages, and currently there’s no built-in dynamic tag available for that use case. -
mpribadi
Would it work if I use shortcode to replace the dynamic tag script? i.e. [current_url]
or GB will just ignore the shortcode? -
Alvind
If the shortcode returns a full URL, it should work in the Button block’s link field.
-
mpribadi
I’ll try it then
-
Alvind
Let us know how it goes!
-
mpribadi
Shorcode not working.
The shortode works fine returning the current permalink, but the GB Text/Button block url field will ignore the shortcode.
The
[permalink]
shortcode in the link/url will be converted into%5Bpermalink%5D
instead.The dynamic field only accept
{{this_kind_tag}}
. Is there a way to define GB custom dynamic tag? -
Use this PHP code to generate a new dynamic tag
{{archive_url}}
for your button link:add_action('init', function () { // Check if the GenerateBlocks dynamic tag class exists if (!class_exists('GenerateBlocks_Register_Dynamic_Tag')) { return; } // Register the dynamic tag for archive URL new GenerateBlocks_Register_Dynamic_Tag([ 'title' => __('Archive Permalink', 'generateblocks'), 'tag' => 'archive_url', 'type' => 'Archive', 'supports' => [], 'description' => __('Displays the archive permalink.', 'generateblocks'), 'return' => function () { $current_archive_url = get_current_archive_url(); return $current_archive_url ? esc_url($current_archive_url) : ''; } ]); }); /** * Retrieves the URL of the current archive page. * * @return string The archive URL or an empty string if not on an archive page. */ function get_current_archive_url() { $queried_object = get_queried_object(); $url = ''; if (is_category()) { $url = get_category_link($queried_object->term_id); } elseif (is_tag()) { $url = get_tag_link($queried_object->term_id); } elseif (is_tax()) { $url = get_term_link($queried_object); } elseif (is_author()) { $url = get_author_posts_url($queried_object->ID); } elseif (is_date()) { if (is_year()) { $url = get_year_link(get_query_var('year')); } elseif (is_month()) { $url = get_month_link(get_query_var('year'), get_query_var('monthnum')); } elseif (is_day()) { $url = get_day_link(get_query_var('year'), get_query_var('monthnum'), get_query_var('day')); } } elseif (is_post_type_archive()) { $post_type = $queried_object && isset($queried_object->name) ? $queried_object->name : get_query_var('post_type'); $url = get_post_type_archive_link($post_type); } elseif (is_home()) { $url = home_url('/'); } // Handle pagination if (get_query_var('paged') > 1) { $url = trailingslashit($url) . 'page/' . get_query_var('paged') . '/'; } return esc_url($url); }
-
mpribadi
Sweet baby Yoda. It Works, thanks Ying.
-
You are welcome 🙂
- You must be logged in to reply to this topic.