Solved

Please help with wordpress sticky post issue

Posted on 2013-12-18
6
534 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

In Part I (http://www.experts-exchange.com/Web_Development/Blogs/WordPress/A_8410-Getting-Started-In-WordPress-Part-I.html), I introduced you to the powerful WordPress backend, the WordPress administrative Dashboard.  In Part II, I will introduce yo…
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

760 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

18 Experts available now in Live!

Get 1:1 Help Now