- This topic has 8 replies, 4 voices, and was last updated 4 years, 8 months ago by
Philippe.
-
AuthorPosts
-
September 19, 2021 at 4:46 pm #1936054
Philippe
Hello,
I would like to display in the sidebar a widget with a suggestion of articles to read that are in the same category as the current article and this, in a random way
How to do this with GeneratePress press premium please?
Thank you!
September 19, 2021 at 5:51 pm #1936065Leo
StaffCustomer SupportHi there,
There isn’t a feature built-in GP or GP Premium for this.
You can try a plugin like this:
https://en-ca.wordpress.org/plugins/contextual-related-posts/Hope this helps 🙂
September 21, 2021 at 7:01 am #1937806Philippe
Hello,
I used list category post to do this,
https://fr.wordpress.org/plugins/list-category-posts/it allows to style the sidebar widget according to the theme used, only you have to use a template, here is an example template (putting this on child-theme/folder liscategorypost/ theme.php, how can I adapt it to match the example theme “dispatch”. ( [wp_show_posts name=”Sidebar”] with no background : wpsp-card zero-padding no-background)
Here is code of an example of the template :
$lcp_display_output = ''; $lcp_display_output .= '<ul class="lcp_fashionseo">'; global $post; while ( have_posts() ) : the_post(); $lcp_display_output .= '<li>'; $lcp_display_output .= '<div>' . $this->get_thumbnail( $post ) . '</div>'; $lcp_display_output .= '<div>'; $lcp_display_output .= $this->get_post_title( $post, 'span', 'lcp_post' ); if ( $this->get_author( $post ) || $this->get_date( $post ) ) { $lcp_display_output .= '<span>'; if ( $this->get_author( $post ) ) { $lcp_display_output .= __( 'By', 'seomag' ) . ' ' . $this->get_author( $post ); } if ( $this->get_author( $post ) && $this->get_date( $post ) ) { $lcp_display_output .= ', '; } if ( $this->get_date( $post ) ) { if ( $this->get_author( $post ) ) { $lcp_display_output .= __( 'on', 'seomag' ) . ' '; } else { $lcp_display_output .= __( 'On', 'seomag' ) . ' '; } $lcp_display_output .= $this->get_date( $post ) . "</span'></>"; } $lcp_display_output .= '</span>'; } $lcp_display_output .= '</div></li>'; endwhile; $lcp_display_output .= '</ul>'; $this->lcp_output = $lcp_display_output;Many thx
PhilSeptember 21, 2021 at 6:54 pm #1938561Elvin
StaffCustomer SupportHi Philippe,
You can do something like this:
[wp_show_posts name=”Sidebar” settings="taxonomy=category&tax_term=your_category_slug_here&orderby=rand"]or do it with PHP snippet.
Here’s an example shortcode snippet.
add_shortcode('dynamic_wpsp', function($atts){ $queriedArchive = get_queried_object(); $atts = shortcode_atts( array( 'id' => '' ), $atts, 'dynamic_wpsp' ); if( is_archive() ){ $settings = array( 'taxonomy' => $queriedArchive->taxonomy, 'tax_term' => $queriedArchive->slug, 'orderby' => 'rand', ); } ob_start(); wpsp_display( $atts['id'], $settings ); return ob_get_clean(); });And then used the shortcode
[dynamic_wpsp id="1234"]where 1234 is the WPSP list id.This shortcode for archive pages. For Single post pages, it’s going to be a bit trickier as I’m not exactly sure how you determine the primary category or the category to be used for related posts when the post is under multiple categories.
But if you can clarify it a bit more for us, I can modify the snippet to include conditions for posts so you can use the same shortcode for both archive pages and single posts pages.
September 22, 2021 at 4:04 am #1938921Philippe
Hi
many thks for your time.
I have used your php code, but it seem works only for archives and not for post (on post it display sidebar with post of other categories), can you modify this for displaying posts only for the current category of the post we read ?
PS: I will not class post on multiples categories
best regard
PhilSeptember 22, 2021 at 4:54 am #1938956David
StaffCustomer SupportHi there,
you should be able to update Elvins shortcode function to:
add_shortcode('dynamic_wpsp', function($atts){ $atts = shortcode_atts( array( 'id' => '' ), $atts, 'dynamic_wpsp' ); if( is_archive() ){ $queriedArchive = get_queried_object(); $settings = array( 'taxonomy' => $queriedArchive->taxonomy, 'tax_term' => $queriedArchive->slug, 'orderby' => 'rand', ); } if( is_single() ) { $cats = get_the_category(); $cat = $cats[0]; $settings = array( 'tax_term' => $cat->slug, 'orderby' => 'rand', ); } ob_start(); wpsp_display( $atts['id'], $settings ); return ob_get_clean(); });September 22, 2021 at 5:09 am #1938982Philippe
in three word : A WE SOME !
works great! many thanks
PhilSeptember 22, 2021 at 5:11 am #1938984David
StaffCustomer SupportGlad we could be of help!
September 22, 2021 at 5:11 am #1938985Philippe
top
-
AuthorPosts
- You must be logged in to reply to this topic.