Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 565
  • Last Modified:

Please help with wordpress sticky post issue

Hello

I'm trying to show 2 blog excerpts on the home page. If there are sticky posts I would like to show them. If there is only one sticky post then I want that to show first and then the regular post.

I found http://codex.wordpress.org/Sticky_Posts and it's seems pretty straight forward, but I can't see to get it to work correctly. Either I have no sticky posts showing or I have both sticky posts and regular posts showing.  I've tried so many different variations I'm not sure what code I should present so I decided to show the original code before I tried displaying sticky posts.

<?php                              
	$blog_cats_include = get_option('vilisya_blog_cats_include');
	if(is_array($blog_cats_include)) {
	$blog_include = implode(",",$blog_cats_include);
	} 
	
	$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
	$hmblog_num = get_option('vilisya_hmblog_num');
	$blogtext = (get_option('vilisya_blogtext')) ? get_option('vilisya_blogtext') : 30;	
	query_posts("cat=$blog_include&showposts=$hmblog_num&paged=$page");
	while ( have_posts() ) : the_post();
	$wp_query->is_home = false;	
?>

	<div class="blog-post clearfix">
		<?php if ( function_exists("has_post_thumbnail") && has_post_thumbnail() ) { the_post_thumbnail(array(200,150),array("class" => "alignleft")); } ?>
		<h3><a href="<?php the_permalink();?>"><?php the_title();?></a></h3> 
		<p><?php excerpt($blogtext);?></p>    
		<a href="<?php the_permalink();?>"><img src="<?php bloginfo('template_directory');?>/images/read-more.gif" alt="" class="imgleft" /></a>
	</div><!-- /blog-post -->

<?php endwhile;?>

Open in new window

Appreciate the assist!
Jules
0
Jules Webb
Asked:
Jules Webb
  • 4
  • 2
2 Solutions
 
jrm213jrm213Commented:
Hi JulesWebb,

So you just want to show 2 excerpts on the homepage, the sticky ones if they exist, if not fill in the space with the most recent post excerpts? Is that correct?

You could try something like the following, basically get the sticky posts, if there are less than or equal to 2 sticky posts (so 2, 1 or 0) then loop through the sticky posts and display them.
<?php
           $sticky = get_option( 'sticky_posts' );

           $args = array(
                'posts_per_page' => 2,
                'post__in'  => $sticky
             );
            query_posts( $args );
            
             while ( have_posts() ) : the_post();
	$wp_query->is_home = false;	
?>

	<div class="blog-post clearfix">
		<?php if ( function_exists("has_post_thumbnail") && has_post_thumbnail() ) { the_post_thumbnail(array(200,150),array("class" => "alignleft")); } ?>
		<h3><a href="<?php the_permalink();?>"><?php the_title();?></a></h3> 
		<p><?php excerpt($blogtext);?></p>    
		<a href="<?php the_permalink();?>"><img src="<?php bloginfo('template_directory');?>/images/read-more.gif" alt="" class="imgleft" /></a>
	</div><!-- /blog-post -->

<?php endwhile;?>

if(count($sticky) < 2){

$blog_cats_include = get_option('vilisya_blog_cats_include');
	if(is_array($blog_cats_include)) {
	$blog_include = implode(",",$blog_cats_include);
	} 
	
	$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
	$hmblog_num = get_option('vilisya_hmblog_num') - count($sticky);
	$blogtext = (get_option('vilisya_blogtext')) ? get_option('vilisya_blogtext') : 30;	
	query_posts("cat=$blog_include&showposts=$hmblog_num&paged=$page");
	while ( have_posts() ) : the_post();
	$wp_query->is_home = false;	
?>

	<div class="blog-post clearfix">
		<?php if ( function_exists("has_post_thumbnail") && has_post_thumbnail() ) { the_post_thumbnail(array(200,150),array("class" => "alignleft")); } ?>
		<h3><a href="<?php the_permalink();?>"><?php the_title();?></a></h3> 
		<p><?php excerpt($blogtext);?></p>    
		<a href="<?php the_permalink();?>"><img src="<?php bloginfo('template_directory');?>/images/read-more.gif" alt="" class="imgleft" /></a>
	</div><!-- /blog-post -->

<?php endwhile;?>
<?php
} //end if
?>

Open in new window


so basically get and display any sticky posts, if the count of sticky posts is less than 2, then get the most recent post from the categories you chose in your theme (vilisya_blog_cats_include) and display vilisya_hmblog_num - however many sticky posts were displayed.

I was unable to test the above code, so consider it psuedocode, if it does happen to just work, all the better.
0
 
Jules WebbFreelance Web DeveloperAuthor Commented:
Hi jrm213jrm213

Thanks for the help!

I tested your codebit and below are the results.
------------------------------------------------------------------------

0 sticky post  ==  2 regular posts present twice indicating that the first and second conditions are both being met.       

1 Sticky post  == 1 sticky post and 1 regular post present

2 Sticky posts === 2 sticky posts present

3 Sticky posts  ==  3 sticky posts present

4 Sticky posts  ==  4 sticky posts present


These are similar results to what I was running into, though I like the logic in your code better than what I was coming up with ---LOL I was trying to do it all within one "while"

Thanks so much for your time.
Jules
0
 
jrm213jrm213Commented:
hmm I am surprised that it is bringing back more than 2 sticky posts are showing up if present.

If you only want 2 sticky posts to show up no matter what then I would add an iterator to the first while statement and break out of it, it is > 2;

<?php
           $sticky = get_option( 'sticky_posts' );

           $args = array(
                'posts_per_page' => 2,
                'post__in'  => $sticky
             );

             $sticky_count = 0;
            query_posts( $args );
            
             while ( have_posts() ) : the_post();
	$wp_query->is_home = false;
         $sticky_count++;
         if($sticky_count) >2){break;}
?>

	<div class="blog-post clearfix">
		<?php if ( function_exists("has_post_thumbnail") && has_post_thumbnail() ) { the_post_thumbnail(array(200,150),array("class" => "alignleft")); } ?>
		<h3><a href="<?php the_permalink();?>"><?php the_title();?></a></h3> 
		<p><?php excerpt($blogtext);?></p>    
		<a href="<?php the_permalink();?>"><img src="<?php bloginfo('template_directory');?>/images/read-more.gif" alt="" class="imgleft" /></a>
	</div><!-- /blog-post -->

<?php endwhile;?>

if(count($sticky) < 2){

$blog_cats_include = get_option('vilisya_blog_cats_include');
	if(is_array($blog_cats_include)) {
	$blog_include = implode(",",$blog_cats_include);
	} 
	
	$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
	$hmblog_num = get_option('vilisya_hmblog_num') - count($sticky);
	$blogtext = (get_option('vilisya_blogtext')) ? get_option('vilisya_blogtext') : 30;	
	query_posts("cat=$blog_include&showposts=$hmblog_num&paged=$page");
	while ( have_posts() ) : the_post();
	$wp_query->is_home = false;	
?>

	<div class="blog-post clearfix">
		<?php if ( function_exists("has_post_thumbnail") && has_post_thumbnail() ) { the_post_thumbnail(array(200,150),array("class" => "alignleft")); } ?>
		<h3><a href="<?php the_permalink();?>"><?php the_title();?></a></h3> 
		<p><?php excerpt($blogtext);?></p>    
		<a href="<?php the_permalink();?>"><img src="<?php bloginfo('template_directory');?>/images/read-more.gif" alt="" class="imgleft" /></a>
	</div><!-- /blog-post -->

<?php endwhile;?>
<?php
} //end if
?>
                                            

Open in new window

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Jules WebbFreelance Web DeveloperAuthor Commented:
Hi jrm213jrm213

Things are working better. The only instance where the conditons are not being met correctly are when there are no sticky posts.

I've reposted the codebit below because you had an syntax error on line 27. I just figured it will be better for future visitors if I accept a solution where you have an accurate codebit represented.


<?php
	$sticky = get_option( 'sticky_posts' );	
	$args = array(
		'posts_per_page' => 2,
		'post__in'  => $sticky
	);	
	$sticky_count = 0;
	$blogtext = (get_option('vilisya_blogtext')) ? get_option('vilisya_blogtext') : 30;
	
	query_posts( $args );
	
	while ( have_posts() ) : the_post();
	$wp_query->is_home = false;
	$sticky_count++;
	if($sticky_count >2){break;}
?>

	<div class="blog-post clearfix">
		<?php if ( function_exists("has_post_thumbnail") && has_post_thumbnail() ) { the_post_thumbnail(array(200,150),array("class" => "alignleft")); } ?>
		<h3><a href="<?php the_permalink();?>"><?php the_title();?></a></h3> 
		<p><?php excerpt($blogtext);?></p>    
		<a href="<?php the_permalink();?>"><img src="<?php bloginfo('template_directory');?>/images/read-more.gif" alt="" class="imgleft" /></a>
	</div><!-- /blog-post -->

<?php endwhile;?>

<?php
if(count($sticky) < 2){

$blog_cats_include = get_option('vilisya_blog_cats_include');
	if(is_array($blog_cats_include)) {
	$blog_include = implode(",",$blog_cats_include);
	} 
	
	$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
	$hmblog_num = get_option('vilisya_hmblog_num') - count($sticky);
	$blogtext = (get_option('vilisya_blogtext')) ? get_option('vilisya_blogtext') : 30;	
	query_posts("cat=$blog_include&showposts=$hmblog_num&paged=$page");
	while ( have_posts() ) : the_post();
	$wp_query->is_home = false;	
?>

	<div class="blog-post clearfix">
		<?php if ( function_exists("has_post_thumbnail") && has_post_thumbnail() ) { the_post_thumbnail(array(200,150),array("class" => "alignleft")); } ?>
		<h3><a href="<?php the_permalink();?>"><?php the_title();?></a></h3> 
		<p><?php excerpt($blogtext);?></p>    
		<a href="<?php the_permalink();?>"><img src="<?php bloginfo('template_directory');?>/images/read-more.gif" alt="" class="imgleft" /></a>
	</div><!-- /blog-post -->

<?php endwhile;?>
<?php
} //end if
?> 

Open in new window


Below are the new test results.

0 sticky post  ==  2 regular posts present twice indicating that the first and second conditions are both being met. (see embeded image below)      

1 Sticky post  ==  1 sticky post and 1 regular post present
2 Sticky posts  ==  2 sticky posts present
3 Sticky posts  ==  2 sticky posts present
4 Sticky posts  ==  2 sticky posts present

0 sticky posts == 2 regular posts showing up twice
0
 
Jules WebbFreelance Web DeveloperAuthor Commented:
Hi jrm213jrm213

I was able to get it working. I added if (!is_sticky()) continue; (line 16)

Thank you so much for your help resolving this issue!

Cheers!
Jules



For anyone interested, here is the code that worked.
<?php
	$sticky = get_option( 'sticky_posts' );	
	$args = array(
		'posts_per_page' => 2,
		'post__in'  => $sticky
	);	
	$sticky_count = 0;
	$blogtext = (get_option('vilisya_blogtext')) ? get_option('vilisya_blogtext') : 30;
	
	query_posts( $args );
	
	while ( have_posts() ) : the_post();
	$wp_query->is_home = false;
	$sticky_count++;
	if($sticky_count >2){break;}
	if (!is_sticky()) continue;
?>

	<div class="blog-post clearfix">
		<?php if ( function_exists("has_post_thumbnail") && has_post_thumbnail() ) { the_post_thumbnail(array(200,150),array("class" => "alignleft")); } ?>
		<h3><a href="<?php the_permalink();?>"><?php the_title();?></a></h3> 
		<p><?php excerpt($blogtext);?></p>    
		<a href="<?php the_permalink();?>"><img src="<?php bloginfo('template_directory');?>/images/read-more.gif" alt="" class="imgleft" /></a>
	</div><!-- /blog-post -->

<?php endwhile;?>

<?php
if(count($sticky) < 2){

$blog_cats_include = get_option('vilisya_blog_cats_include');
	if(is_array($blog_cats_include)) {
	$blog_include = implode(",",$blog_cats_include);
	} 
	
	$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
	$hmblog_num = get_option('vilisya_hmblog_num') - count($sticky);
	$blogtext = (get_option('vilisya_blogtext')) ? get_option('vilisya_blogtext') : 30;	
	query_posts("cat=$blog_include&showposts=$hmblog_num&paged=$page");
	while ( have_posts() ) : the_post();
	$wp_query->is_home = false;	
?>

	<div class="blog-post clearfix">
		<?php if ( function_exists("has_post_thumbnail") && has_post_thumbnail() ) { the_post_thumbnail(array(200,150),array("class" => "alignleft")); } ?>
		<h3><a href="<?php the_permalink();?>"><?php the_title();?></a></h3> 
		<p><?php excerpt($blogtext);?></p>    
		<a href="<?php the_permalink();?>"><img src="<?php bloginfo('template_directory');?>/images/read-more.gif" alt="" class="imgleft" /></a>
	</div><!-- /blog-post -->

<?php endwhile;?>
<?php
} //end if
?> 

Open in new window

0
 
Jules WebbFreelance Web DeveloperAuthor Commented:
Thanks for you help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now