Solved

Please help with wordpress sticky post issue

Posted on 2013-12-18
6
538 Views
Last Modified: 2013-12-23
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
Comment
Question by:Jules Webb
  • 4
  • 2
6 Comments
 
LVL 17

Expert Comment

by:jrm213jrm213
ID: 39726930
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
 
LVL 2

Author Comment

by:Jules Webb
ID: 39728234
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
 
LVL 17

Assisted Solution

by:jrm213jrm213
jrm213jrm213 earned 500 total points
ID: 39728250
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Author Comment

by:Jules Webb
ID: 39728279
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
 
LVL 2

Accepted Solution

by:
Jules Webb earned 0 total points
ID: 39728380
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
 
LVL 2

Author Closing Comment

by:Jules Webb
ID: 39735727
Thanks for you help!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Load google maps api into wordpress 5 33
mcrypt_create_iv() is deprecated 4 66
Access Object's Property 9 20
javascript date picker in php while loop 5 20
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
The purpose of this video is to demonstrate how to Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now