Home › Forums › Pro Support › category specific side bar
- This topic has 7 replies, 3 voices, and was last updated 2 years, 3 months ago by
elvin.
-
AuthorPosts
-
June 18, 2021 at 1:28 pm #31315
Mike
ParticipantI can see how to globally choose categories in taxonomy. Is it possible to show only post in the same category on a page SIDEBAR when I have several categories?
Thanks
Mike MahaffeyJune 20, 2021 at 4:27 pm #31332elvin
ModeratorHi Mike,
You’ll need a helper function for that.
Tom wrote a PHP snippet for this.
It’s this one:
add_shortcode('dynamic_wpsp', function($atts){ $queriedArchive = get_queried_object(); $atts = shortcode_atts( array( 'id' => '' ), $atts, 'dynamic_wpsp' ); $settings = array( 'taxonomy' => $queriedArchive->taxonomy, 'tax_term' => $queriedArchive->slug, ); ob_start(); wpsp_display( $atts['id'], $settings ); return ob_get_clean(); });
It’s a snippet for a shortcode and the usage is
[dynamic_wpsp id="1234"]
where1234
is the WPSP ID.June 23, 2021 at 6:47 am #31389paul
ParticipantHi – sorry to hijack, I am trying to achieve the exact same thing. Have added the snippet to functions however the shortcode outputs my most recent posts from all categories rather than posts from the same category – do I need to apply any settings in the WPSP frontend?
June 23, 2021 at 8:37 pm #31403elvin
ModeratorHi – sorry to hijack, I am trying to achieve the exact same thing. Have added the snippet to functions however the shortcode outputs my most recent posts from all categories rather than posts from the same category – do I need to apply any settings in the WPSP frontend?
Can you specify what page did you add the shortcode?
The specific shortcode was written for WPSP added on archive page. If you use it on a static page, it won’t know what taxonomy to use because static pages normally don’t have taxonomy assigned to them.
June 23, 2021 at 11:38 pm #31412paul
ParticipantHi Elvin – I want to use it on all standard posts on my site – is there a way to do this – let me know if I should open a new ticket for this.
June 24, 2021 at 12:25 am #31415elvin
ModeratorWell, we can modify the shortcode a bit so it takes the taxonomy terms of the current post.
Try this:
add_shortcode('dynamic_wpsp', function($atts){ $posttax = 'category'; $queriedPost = get_the_terms( get_the_ID(), $posttax ); foreach ( $queriedPost as $tax ) { $arg_slug = $tax->slug; } $queriedArchive = get_queried_object(); $atts = shortcode_atts( array( 'id' => '' ), $atts, 'dynamic_wpsp' ); if(is_archive()){ $settings = array( 'taxonomy' => $queriedArchive->taxonomy, 'tax_term' => $queriedArchive->slug, ); } if(is_single()){ $settings = array( 'tax_term' => $arg_slug, ); } ob_start(); wpsp_display( $atts['id'], $settings ); return ob_get_clean(); });
June 24, 2021 at 7:45 am #31423paul
ParticipantWorks perfectly. Thanks
June 24, 2021 at 6:48 pm #31429elvin
ModeratorNo problem. Glad to be of any help. 😀
-
AuthorPosts
- You must be logged in to reply to this topic.