Site logo

Archived topic

How to get slug inside a Element Header

20 replies · Started by Kir29 LLC on January 13, 2019

Viewing posts 1–15 of 21

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

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 :)

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!

Hi there,

you can add that shortcode to your Header Element.

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!

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

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

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.

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.

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.

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

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.

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]"]

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

This archived topic is closed to new replies.