Site logo

[Support request] radom post suggestion of the same category on sidebar widget

Home Forums Support [Support request] radom post suggestion of the same category on sidebar widget

Home Forums Support radom post suggestion of the same category on sidebar widget

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #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!

    #1936065
    Leo
    Staff
    Customer Support

    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 🙂

    #1937806
    Philippe

    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

    #1938561
    Elvin
    Staff
    Customer Support

    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.

    #1938921
    Philippe

    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

    #1938956
    David
    Staff
    Customer Support

    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();
    });
    #1938982
    Philippe

    in three word : A WE SOME !

    works great! many thanks
    Phil

    #1938984
    David
    Staff
    Customer Support

    Glad we could be of help!

    #1938985
    Philippe

    top

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.