Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

The template tag for the current slug

  • Hi support

    I use the GP premium and I like to use a template tag for the current slug of a post or page. Is there any tag for that like {{post_title}}?

    http://www.example.com/post/this-is-the-slug.

    Appreciated.

    Flaim

  • Hi Flaim,

    You’ll need to register a custom dynamic tag. Here’s the snippet to do that:

    add_action('init', function () {
        if (!class_exists('GenerateBlocks_Register_Dynamic_Tag')) {
            return;
        }
    
        new GenerateBlocks_Register_Dynamic_Tag([
            'title'       => __('Post Slug', 'generatepress-child'),
            'tag'         => 'post_slug',
            'type'        => 'post',
            'supports'    => ['source', 'link'],
            'description' => __('Display the post slug for the current post.', 'generatepress-child'),
    
            'return' => function ($options, $block, $instance) {
                $id = GenerateBlocks_Dynamic_Tags::get_id($options, 'post', $instance);
    
                if (!$id) {
                    return '';
                }
    
                $post_slug = get_post_field('post_name', $id);
    
                if (is_wp_error($post_slug) || empty($post_slug)) {
                    return '';
                }
    
                return GenerateBlocks_Dynamic_Tag_Callbacks::output($post_slug, $options, $instance);
            },
        ]);
    });

    Once added, you can use the {{post_slug}} dynamic tag to output the current post’s slug.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.