- This topic has 20 replies, 3 voices, and was last updated 4 years, 4 months ago by
Tom.
-
AuthorPosts
-
January 13, 2019 at 11:21 am #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
January 13, 2019 at 5:23 pm #781004Tom
Lead DeveloperLead DeveloperHi 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 🙂
January 13, 2019 at 9:41 pm #781091Kir29 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!
January 14, 2019 at 2:11 am #781198David
StaffCustomer SupportHi there,
you can add that shortcode to your Header Element.
January 14, 2019 at 5:16 am #781311Kir29 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!
January 14, 2019 at 5:48 am #781332David
StaffCustomer Supportso you have a separate slider for each of the different posts?
January 14, 2019 at 5:59 am #781349Kir29 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
January 14, 2019 at 6:26 am #781370David
StaffCustomer SupportHow 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.
January 14, 2019 at 6:32 am #781383Kir29 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.
January 14, 2019 at 6:56 am #781535David
StaffCustomer SupportSo 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.
January 14, 2019 at 9:07 am #781654Kir29 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 sliderCan you think of a simple way to do it in the Heather Element?
Thanks
January 14, 2019 at 9:25 am #781677David
StaffCustomer SupportThe 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.January 14, 2019 at 9:37 am #781691Tom
Lead DeveloperLead DeveloperYea, 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]"]
January 15, 2019 at 11:12 am #782866Kir29 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
January 15, 2019 at 4:42 pm #783112Tom
Lead DeveloperLead DeveloperThat’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.
-
AuthorPosts
- You must be logged in to reply to this topic.