1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Need some help with wordpress sticky / featured post duplication

Discussion in 'PHP' started by jhmattern, Jul 17, 2009.

  1. #1
    I'm making some edits to a theme I purchased so I can distribute it through one of my sites. You can see the demo at http://allbusinesswriting.com

    I'm having two problems I hope someone can help with:

    1. To the left of the tweet section, there is a post. Actually, there should be only one post, but right now there are two. It's set to show one. The problem is that when a sticky post is added it overrides that 1 post option and shows both the sticky and the most recent post.

      I need that to be an either / or thing. If there is a sticky post, it should show the latest sticky. If there's no sticky (since not all users will use that function), it should show the latest post.

    2. The post showing in the first loop at the top shouldn't be duplicated in the main loop below. But it is (they are, since we have 2 for now). I know I should be able to fix that with an array or something, but for the life of me I can't seem to figure it out.

    If anyone knows how to get past either of those issues, I'd appreciate some tips. If I have to make the first loop stickies only (forcing the user to create a sticky post) I will, but I'd really rather not do that.

    Thanks in advance!

    Jenn
     
    jhmattern, Jul 17, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Can you post the source code to the sidebar and the main posts section?
     
    jestep, Jul 17, 2009 IP
  3. jhmattern

    jhmattern Illustrious Member

    Messages:
    8,909
    Likes Received:
    794
    Best Answers:
    2
    Trophy Points:
    455
    #3
    It has nothing to the sidebar. The code's all in the main index.php file. Here's the code for that as it is now:

    <?php get_header(); ?>
    <?php if (!($paged > 1) ) { ?>
    <div class="sticky">
    <div class="stickyleft">
    <?php
      $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts("showposts=1&paged=$page&order=desc");
      ?>
    
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php $image = get_post_meta($post->ID, 'img', true); ?>
    <?php if ( isset($image) && !empty($image)) { ?>
    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
    <img class="stickythumb" src="<?php echo $image; ?>" alt="Sticky Post: <?php the_title(); ?>" />
    </a> <? } ?>
    <h2 class="h2sticky"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
    <ul class="stickydate">
    <li>Posted: <?php the_time('F j, Y'); ?></li>
    </ul>
    <?php the_excerpt(); ?>
    
    <?php endwhile; else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>
    </div>
    
    <div class="stickyright">
    <a href="http://twitter.com/queryfreewriter" title="Follow Us On Twitter">
    <img class="twitter" src="<?php bloginfo('template_directory'); ?>/img/twitter.png" alt="Follow Us On Twitter" />
    </a>
    <div id="twitter_div">
    <ul id="twitter_update_list">
    <li></li>
    </ul>
    </div>
    <script type="text/javascript" src="http://twitter.com/javascripts/blogger.js"></script>
    <script type="text/javascript" src="http://twitter.com/statuses/user_timeline/queryfreewriter.json?callback=twitterCallback2&amp;count=1"></script>
    </div> <!-- /stickyright -->
    </div> <!-- /sticky -->
    <? } ?>
    
    <div class="clear"></div>
    
    <?php get_sidebar(); ?>
    
    <div class="index">
    <?php
      $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
      query_posts("caller_get_posts=1&showposts=10&paged=$page&order=desc");
      ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); $loopcounter++; ?>
    <?php $image = get_post_meta($post->ID, 'img', true); ?>
    
    
    <ul <?php if ($loopcounter > 1) { ?> style="margin-top:40px" <? } ?> class="info">
    <li>Author: <?php the_author_posts_link(); ?></li>
    <li>Posted: <?php the_time('F j, Y'); ?></li>
    <li style="float:right; padding-right:10px"><a href="<?php comments_link(); ?>"><?php comments_number('No Comments Yet', '1 Comment', '% Comments'); ?></a></li>
    </ul>
    
    <div class="clear"></div>
    
    <div class="postcontain">
    <?php if ( isset($image) && !empty($image)) { ?>
    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
    <img class="sthumb" src="<?php echo $image; ?>" alt="Post: <?php the_title(); ?>" />
    </a> <? } ?>
    <h3><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
    <?php the_excerpt(); ?>
    </div>
    
    
    <?php endwhile; else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>
    <div class="postsnavlink">
    <?php posts_nav_link(); ?>
    </div>
    </div>
    <?php get_footer(); ?>
    PHP:
     
    jhmattern, Jul 17, 2009 IP
  4. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #4
    For the very first loop (the single sticky/top post) try this:

    Replace:
    
    $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts("showposts=1&paged=$page&order=desc");
      ?>
    
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
    PHP:
    With:
    
    $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
    	
    if($sticky=get_option('sticky_posts'))
    {
    	query_posts('p=' . $sticky[0]);
    } else 
    {
    query_posts("caller_get_posts=1&showposts=1&paged=$page&order=desc");
    }
    
    ?>
        
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); 
    $exclude_post = $post->ID;
    
    PHP:
    On the second loop, you can then exclude the top post using: $exclude_post from the loop.
     
    jestep, Jul 17, 2009 IP
  5. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #5
    Actually if you use an array, this gets easier...

    $exclude_post[] = $post->ID;

    On the lower loop use:
    $args=array(
    'caller_get_posts'=>1,
    'showposts' => 10,
    'post__not_in' => $exclude_post,
    'paged'=>$page,
    'order'=>'desc'
    );

    query_posts($args);
     
    jestep, Jul 17, 2009 IP
  6. jhmattern

    jhmattern Illustrious Member

    Messages:
    8,909
    Likes Received:
    794
    Best Answers:
    2
    Trophy Points:
    455
    #6
    I think that's the biggest problem I've been having with the second problem - I've found the array info before, but I'm not sure exactly where to put it, b/c no matter what I do with it I get no results. Where in the code exactly would I copy that?

    The solution to the first problem gives me the following error:

    Parse error: syntax error, unexpected '<' in /home/jhm2980/public_html/allbusinesswriting/wp-content/themes/pushit/index.php on line 20
    Code (markup):
    Thanks for your help so far!
     
    jhmattern, Jul 17, 2009 IP
  7. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #7
    Here's the whole script with the changes:

    
    <?php get_header(); ?>
    <?php if (!($paged > 1) ) { ?>
    <div class="sticky">
        <div class="stickyleft">
        <?php
        $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
    	if($sticky=get_option('sticky_posts'))
    	{
    		query_posts('p=' . $sticky[0]);
    	} else 
    	{
    		query_posts("caller_get_posts=1&showposts=1&paged=$page&order=desc");
    	}
        ?>
        <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); 
    	$exclude_post[] = $post->ID;
    	?>
        <?php $image = get_post_meta($post->ID, 'img', true); ?>
        <?php if ( isset($image) && !empty($image)) { ?>
        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
        <img class="stickythumb" src="<?php echo $image; ?>" alt="Sticky Post: <?php the_title(); ?>" />
        </a> <? } ?>
        <h2 class="h2sticky"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
        <ul class="stickydate">
        <li>Posted: <?php the_time('F j, Y'); ?></li>
        </ul>
        <?php the_excerpt(); ?>
        
        <?php endwhile; else: ?>
        <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
        <?php endif; ?>
        </div>
        
        <div class="stickyright">
        <a href="http://twitter.com/queryfreewriter" title="Follow Us On Twitter">
        <img class="twitter" src="<?php bloginfo('template_directory'); ?>/img/twitter.png" alt="Follow Us On Twitter" />
        </a>
        <div id="twitter_div">
        <ul id="twitter_update_list">
        <li></li>
        </ul>
        </div>
        <script type="text/javascript" src="http://twitter.com/javascripts/blogger.js"></script>
        <script type="text/javascript" src="http://twitter.com/statuses/user_timeline/queryfreewriter.json?callback=twitterCallback2&amp;count=1"></script>
        </div> <!-- /stickyright -->
    </div> <!-- /sticky -->
    <? } ?>
    
    <div class="clear"></div>
    
    <?php get_sidebar(); ?>
    
    <div class="index">
    <?php
      $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
      
      $args=array(
    	'caller_get_posts'=>1,
    	'showposts' => 10,
    	'post__not_in' => $exclude_post,
    	'paged'=>$page,
    	'order'=>'desc'
    	);
    	
    	query_posts($args);
      
    ?>
    
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); $loopcounter++; ?>
    <?php $image = get_post_meta($post->ID, 'img', true); ?>
    
    
    <ul <?php if ($loopcounter > 1) { ?> style="margin-top:40px" <? } ?> class="info">
    <li>Author: <?php the_author_posts_link(); ?></li>
    <li>Posted: <?php the_time('F j, Y'); ?></li>
    <li style="float:right; padding-right:10px"><a href="<?php comments_link(); ?>"><?php comments_number('No Comments Yet', '1 Comment', '% Comments'); ?></a></li>
    </ul>
    
    <div class="clear"></div>
    
    <div class="postcontain">
    <?php if ( isset($image) && !empty($image)) { ?>
    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
    <img class="sthumb" src="<?php echo $image; ?>" alt="Post: <?php the_title(); ?>" />
    </a> <? } ?>
    <h3><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
    <?php the_excerpt(); ?>
    </div>
    
    
    <?php endwhile; else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>
    <div class="postsnavlink">
    <?php posts_nav_link(); ?>
    </div>
    </div>
    <?php get_footer(); ?>
    
    PHP:
    I am not seeing what would cause the parse error at all.
     
    jestep, Jul 17, 2009 IP
    jhmattern likes this.
  8. jhmattern

    jhmattern Illustrious Member

    Messages:
    8,909
    Likes Received:
    794
    Best Answers:
    2
    Trophy Points:
    455
    #8
    You are awesome. Thank you so much for your help! The code changes you made worked perfectly. :)

    If you send me your paypal address I'll send a little something over for taking the time to help me out.
     
    jhmattern, Jul 17, 2009 IP