Home › Forums › Pro Support › Dynamic Shortcode Based on Category, or Alternative
- This topic has 7 replies, 2 voices, and was last updated 2 years, 5 months ago by
elvin.
-
AuthorPosts
-
June 17, 2021 at 5:20 am #31281
afv
ParticipantWe are building a site that has gotten somewhat out of hand in its complexity.๐
When the client requested custom designed pages for a handful of special categories, I used GPP, GBP, dynamic options & CSS to create one hero that can be applied to all but includes a unique category image and description. He was very happy, but said these pages MUST also offer a block of highlighted posts that is unique to each category.
I saw no way around having to create and maintain multiple elements to achieve this.๐ I’m concerned about not just the additional work, but future consistency.
To accommodate the request, I created an additional element for each category; it is pulled in after the hero (so at least that element can retain a single, consistent templateโฆyay). This “hero part 2” element has only a WPSP shortcode and a headline block for the category post list that appears after it once WP assembles all the parts for the visitor.
In the WPSP shortcode I am adding query terms to populate the list based on a tag I’ve asked the client to apply to posts he wants to feature in that block. I pretty quickly experienced WP’s ampersand issue. After coming here to see if there was a fix for it, I discovered Tom’s shortcode-within-a-shortcode workaround.
I will use that fix if you all think it’s the best way. But I was wondering if instead I could check the current category and include it in the query. If so, I can go back to a single hero element. However, I am a PHP hack at best. Neither my current function nor a couple of previous iterations are working. What I’ve got now:
add_shortcode( 'cat-highlights', function() { ob_start(); $current_cat = get_queried_object(); $term_id = $current_cat->term_id; $taxonomy_name = 'category'; $highlights_shortcode = 'wp_show_posts id="62367" settings="taxonomy=post_tag&tax_term=' . $taxonomy_name . 'highlights"'; echo do_shortcode( [ $highlights_shortcode ] ); return ob_get_clean(); } );
This function just outputs [cat-highlights] on the front end.
I would be grateful for help with either pointing out the error(s) in my function or that the entire idea isn’t doable and I should stick with multiple elements/shortcodes.
June 17, 2021 at 7:53 pm #31291elvin
ModeratorHi there,
You can use Tom’s code here
https://generatepress.com/forums/topic/custom-category-archive-page/#post-1777697I believe his code basically does the same thing your code is trying to do except his shortcode goes directly to the wpsp_display function instead of passing it through shortcode parameter.
We can modify this a bit since you seem to be adding prefix “highlights” to the taxonomy term.
add_shortcode('dynamic_wpsp', function($atts){ $queriedArchive = get_queried_object(); $atts = shortcode_atts( array( 'id' => '' ), $atts, 'dynamic_wpsp' ); $queried_term = $queriedArchive->slug.'highlights'; $settings = array( 'taxonomy' => $queriedArchive->taxonomy, 'tax_term' => $queried_term, ); ob_start(); wpsp_display( $atts['id'], $settings ); return ob_get_clean(); });
As for your code specifically, I think echo shouldn’t be used if the do_shortcode()’s containing shortcode already echoes something.
June 18, 2021 at 1:25 pm #31313afv
ParticipantHey, Elvin โ That snippet got me pretty close, however I got a PHP error when I tried to save the Element with the updated shortcode in it. I looked at the code snippet you provide and it seemed like it couldn’t possibly be using the correct taxonomy (post_tag).
I wasn’t 100% sure of the proper way to change it, but I sort of hijacked your atts array and that seems to be working fine with no errors. Here is what I ended up with:
add_shortcode('dynamic_wpsp', function($atts){ $queriedArchive = get_queried_object(); $atts = shortcode_atts( array( 'id' => '', 'queried_taxonomy' => 'post_tag', ), $atts, 'dynamic_wpsp' ); $queried_term = $queriedArchive->slug.'-highlights'; $settings = array( 'taxonomy' => $atts['queried_taxonomy'], 'tax_term' => $queried_term, ); ob_start(); wpsp_display( $atts['id'], $settings ); return ob_get_clean(); });
Do you see any problems with my revised code, or is there a better/proper way to specify post_tag as the taxonomy?
June 20, 2021 at 4:24 pm #31330elvin
ModeratorThis kind of code isn’t meant to be added on GPP’s Hook element.
GPP’s Hook element is for presentation purposes. You’re not supposed to do something like this on it.
You should use a child theme’s functions.php or a Code Snippets plugin for this.
As for the code:
Doing that means the WPSP shortcode is fixed to only doing queries for post_tag. Are you sure about that? If that’s the case then the shortcode should be fine. ๐
June 20, 2021 at 4:33 pm #31334afv
ParticipantIn this case, the post_tag makes sense because it is the easiest way to allow the client to choose which posts he wants to feature. But I can see where it would be helpful for another project (or heck, possibly even this one, in another area) to allow the taxonomy to be set in the shortcode. Is that what you were getting at?
Can you help me understand the downside of putting this code in a hook element?
The reason I did it is because I really like the ability with Elements to limit where the code will execute. I do use Code Snippets all the time so it can certainly go in as a snippet if there’s a serious downside to doing things this way, and if it won’t impact pages/posts outside of the ones this shortcode is meant for.
Thanks for any additional insight, Elvin. ๐
June 20, 2021 at 4:52 pm #31336elvin
ModeratorBut I can see where it would be helpful for another project (or heck, possibly even this one, in another area) to allow the taxonomy to be set in the shortcode. Is that what you were getting at?
Yes, that’s exactly it.
The downside of adding this code in a hook element is, it’s not good practice and it may cause performance issues.
GPP’s Hook element is meant to add things to the front-end. But this shortcode is basically a helper function to make a plugin work in a custom manner.
Having this snippet within a hook element is basically writing it this way:
add_action('hook_set_on_hook_elemement',function(){ //the code snippet provided runs here here });
It’s going through an unnecessary process which is it has to run through your specified hook first instead of running as it is. It’s extra work for the server.
If you want to use the display rule location of where the shortcode does, do it with the actual shortcode, not with the PHP snippet that creates it.
June 21, 2021 at 4:40 pm #31351afv
ParticipantI am tripping or something. I can’t say I’ve never used an Element for PHP, but this particular code chunk is and probably always has been in a code snippet.
Thanks for explaining the potential issues with doing that, Elvin.
June 21, 2021 at 8:17 pm #31358elvin
ModeratorNo problem. Glad to be of any help. ๐
-
AuthorPosts
- You must be logged in to reply to this topic.