Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #690433

    I have added a blog grid by shortcode:
    [av_blog blog_type='posts' link='category' blog_style='blog-grid' columns='3' contents='title_read_more' content_length='content' preview_mode='custom' image_size='portfolio' items='6' offset='0' paginate='no' conditional='']
    In functions.php I have written

    add_filter('the_posts', 'bump_sticky_posts_to_top');
    function bump_sticky_posts_to_top($posts) {
     foreach($posts as $i => $post) {
      if(is_sticky($post->ID)) {
       $stickies[] = $post;
       unset($posts[$i]);
       }
      }
     if(!empty($stickies))
      return array_merge($stickies, $posts);
      return $posts;
     }

    When I tag a post as sticky, it works as long the post is within the newest 6 posts. Number 7 and higher don’t appear as sticky post. When I set items=’20’ in the shortcode it works for the first 20 posts.
    It seems to be a specific blog-grid-problem.

    When I use e.g. blog_style=’single-big’ instead of blog_style=’blog-grid’ stycky posts work fine in every case.

    Please can you help me, to solve this problem.

    Best regards,
    Günter

    • This topic was modified 8 years, 2 months ago by günter. Reason: typo
    #691032

    Hi,
    meanwhile I have found a tolerably working solution at WP Codex:

    add_filter('avia_post_slide_query', 'avia_post_slide_query_mod');
    function avia_post_slide_query_mod($query) {
     $sticky = get_option( 'sticky_posts' );
     $query = new WP_Query( 'p=' . $sticky );
     return $query;
     }

    But sticky posts increase the number of intended posts per page (items=’6′ + 1 sticky post = 7).
    Is there a way to limit the shown posts beside Css-methods?

    Regards,
    Günter

    #692059

    Hi,

    Thank you for using Enfold.

    We tested the code but it’s not working on our end. Please remove it then replace it with the following:

    // sticky posts
    add_filter('avia_post_slide_query', 'avia_post_slide_query_mod', 10, 2);
    function avia_post_slide_query_mod($query, $params) {
    	$include = array();
    	$sticky = get_option( 'sticky_posts' );
    
    	$args = array(
    	  'taxonomy' => $params['taxonomy'],
    	  'post__not_in' => $sticky,
    	);
    	$posts = get_posts( $args );
    
    	foreach($posts as $post) {
    		$include[] = $post->ID;
    	}
    
    	$include = array_merge($sticky, $include);
    
    	// convert values of the $include from string to int
    	function sti($n)
    	{
    		settype($n, 'int');
    		return $n ;
    	}
    
    	$include = array_map("sti", $include);
    
    	$query['post__in'] = $include;
    	$query['posts_per_page'] = 6;
    	$query['orderby'] = 'post__in'; // sort items based on the post__in value
    	return $query;
    }

    Best regards,
    Ismael

    #692399

    Hi Ismael,

    thank you for your code. It works fine ad helps me to reduce my effort.

    Best Regards,
    Günter

    #692533

    Hey!

    Glad it is working. :)

    Regards,
    Ismael

Viewing 5 posts - 1 through 5 (of 5 total)
  • The topic ‘Sticky Posts in Blog Grid’ is closed to new replies.