Archived topic
radom post suggestion of the same category on sidebar widget
8 replies · Started by Philippe on September 19, 2021
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!
Hi 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 :)
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
Phil
Hi 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.
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
Phil
Hi 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();
});
in three word : A WE SOME !
works great! many thanks
Phil
Glad we could be of help!
top