[Resolved] How to get slug inside a Element Header

Home Forums Support [Resolved] How to get slug inside a Element Header

Home Forums Support How to get slug inside a Element Header

Viewing 15 posts - 1 through 15 (of 21 total)
  • Author
    Posts
  • #780839
    Kir29 LLC

    I’m successfully using {{post_title}} in one of my Element Headers but I’d like to access to the post / page slug value.

    Is there a way to reference it similar to {{post_title}} {{post_date}} {{post_author}}?

    Thank you for the help

    #781004
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    You can create a shortcode:

    add_shortcode( 'post_slug', function() {
        global $post;
    
        if ( is_object( $post ) ) {
            return $post->post_name;
        }
    } );

    Then you can add this in your Element:

    [post_slug]

    Let me know if that helps or not 🙂

    #781091
    Kir29 LLC

    Thanks Tom.

    Having accesss to [post_slug] that way would be perfect.

    I would like to execute that shortcode for any page/post yah is loaded without having to include the piece of shortcode in every page/post.

    What would you recommend is the best way to achieve that?

    I’m not a WP expert as you can see and wondered if I could somehow create a library that will be executed for any loaded page without me having to include it manually one by one.

    Thanks you!

    #781198
    David
    Staff
    Customer Support

    Hi there,

    you can add that shortcode to your Header Element.

    #781311
    Kir29 LLC

    Hi again,

    Sorry about my missing understanding. I’ve added the following text to my Element Header but it doesn’t seem to be working:

    —-BEGING—

    add_shortcode( ‘post_slug’, function() {
    global $post;

    if ( is_object( $post ) ) {
    return $post->post_name;
    }
    } );

    [smartslider3 slider=”slider-{{post_slug}}”]

    —–END—-

    I’m trying to embed a slider with an slider ID called ‘slider-‘ + my post_slug. I tried using {{post_slug}} and [post_slug] but none of them seems to be working.

    I’m sure I’m missing something but I’m not sure what it is.

    Thanks again for the help!

    #781332
    David
    Staff
    Customer Support

    so you have a separate slider for each of the different posts?

    #781349
    Kir29 LLC

    Yea Tom, that’s the idea I’m trying to implement.

    Dynamically loading a slider that is called ‘slider-‘ concstenated with the page slug.

    Does it make sense?

    Thanks

    #781370
    David
    Staff
    Customer Support

    How about this for an alternative.
    In the Post editor you can activate Custom Fields.
    In classic editor it is in the Screen Options. In the Gutenberg Editor it is under the 3 dot menu > Options > advance tab.

    1. Give it a name: slider-shortcode
    2. In value add your Smart Slider Shortcode.
    3. In the header element use the custom field template tag which will look like this:

    {{custom_field.slider-shortcode}}

    Then you can just add a different shortcode to that custom field on any post. Saves having to create sliders that are named after specific post slugs. Let me know.

    #781383
    Kir29 LLC

    Hi again,

    Having specific slider name per post is not a problem because the content of the slider is different.

    I really want to avoid adding on-off code to the page/post that’s the main reason why I want to code it as part of a common Header Element.

    Can you think of a way?

    Thank you for the help.

    #781535
    David
    Staff
    Customer Support

    So the Code Tom provided needs to be added to your child theme function file or Code Snippets plugin – see here:

    https://docs.generatepress.com/article/adding-php/

    But nesting a shortcode within a shortcode will not work out of the box, and would require PHP ( which won’t work in a Page Header ) however this plugin may do the trick:

    https://wordpress.org/support/plugin/nested-shortcodes/

    The content in the header element should look like this:

    [smartslider3 slider="slider-[post_slug]"]

    If not we would have to look at using PHP and GP Hooks.

    #781654
    Kir29 LLC

    Ufff, that’s even more complex.

    Sorry I’m not that savvy and having to add code to multiple places concerns me.

    Is there any other way I could have a conditional sentence in my Heather Element and I’d hardcore the different slider names.

    Almost like
    IF post=abc the slider=abc
    IF post=def the slider=def
    Else NO slider

    Can you think of a simple way to do it in the Heather Element?

    Thanks

    #781677
    David
    Staff
    Customer Support

    The simplest method is to just create a separate Header Element for each of the sliders.
    The alternative is to use a Hook with PHP conditionals to add each slider, this keeps them all in one place but not as simple to change as the first option.

    #781691
    Tom
    Lead Developer
    Lead Developer

    Yea, unfortunately there isn’t really a super easy solution for something like this.

    The easiest method is the one David mentioned here: https://generatepress.com/forums/topic/how-to-get-slug-inside-a-element-header/#post-781535

    It only requires a few steps:

    1. Add the PHP I provided for the shortcode using these instructions: https://docs.generatepress.com/article/adding-php/

    2. Install the plugin David mentioned: https://wordpress.org/support/plugin/nested-shortcodes/

    3. Add the shortcode he provided in your Header Element: [smartslider3 slider="slider-[post_slug]"]

    #782866
    Kir29 LLC

    Hi again Tom &

    I Followed your recommendation and installed the Code Snippets plugin for PHP (https://en-ca.wordpress.org/plugins/code-snippets/) and it seems to be working perfectly with Tom’s PHP code:

    add_shortcode( ‘post_slug’, function() {
    global $post;

    if ( is_object( $post ) ) {
    return $post->post_name;
    }
    } );

    I can now include my shortcode on its own using [post_slug].

    I then installed the pluging David recommended for nested shortcodes (https://wordpress.org/support/plugin/nested-shortcodes/) but unfortunately when I tried to call a nested shortcode in the Header Element it doesn’t work:

    [smartslider3 slider=”slider-[post_slug]”]

    I’ve noticed a similar support call in the forum and looks like it’s not possible to use it that way:

    but it looks https://wordpress.org/support/topic/can-this-plugin-nest-another-shortcode-as-parameters-in-another-shortcode/

    So I’m stuck again not knowing what to try now. Any other ideas or suggestions?

    Thanks for the help

    #783112
    Tom
    Lead Developer
    Lead Developer

    That’s a shame.

    In that case, you’re likely only left with the option David mentioned here: https://generatepress.com/forums/topic/how-to-get-slug-inside-a-element-header/#post-781370

    It’s a little more involved, unfortunately. It requires you to add the shortcode with the slug manually into each individual page.

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