WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Post Loop leaking into other Posts (13 posts)

  1. jadedchron
    Member
    Posted 3 years ago #

    I have a banner positioned in my header so the user can modify the single post effectively changing the banner.

    My problem is, it leaks into other posts on the other pages and includes the banner post where it shouldn't.

    Here is the code I'm using in the header:

    <?php $posts = get_posts('numberposts=1&category_name=Banner'); ?>
    		<?php if(count($posts) > 0): foreach($posts as $post) : setup_postdata($post); ?>
    
              <div id="banner-top">
    
                  <div id="banner-left">
                    <?php if(short_meta('Image URL')): ?><a href="<?php echo short_meta('Banner URL'); ?>"><img src="<?php echo short_meta('Image URL'); ?>" /></a><?php endif; ?>
                  </div>
                  <div id="banner-right">
                    <h3><a href="<?php echo short_meta('Banner URL'); ?>">IrisWork News & Events</a></h3>
                    <img src="wp-content/uploads/2010/09/divider.jpg" alt="Divider" />
                    <?php the_content(); ?>
                  </div>
                  <div class="clear"><!--clearing--></div>
    
              </div>
    
    		 <?php endforeach; endif; ?>

    It works fine on the homepage because it doesn't have any actual post loops but any other sub-pages such as my "blog" page, it includes it as the first post. I've tried rewind_posts but I'm not having any luck.

    Any ideas?

  2. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    there is no conditional statement around this code to stop it from showing on any page or with any template.

    if it is only supposed to show on the front page, try is_home() or is_front_page()
    http://codex.wordpress.org/Conditional_Tags

  3. jadedchron
    Member
    Posted 3 years ago #

    What conditional statement could I write to wrap around it?

    I want it on every page available, not just the home page.

    Thanks,
    Nick

  4. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    I want it on every page available,

    no conditional needed then.
    i was confused by your earlier

    but any other sub-pages such as my "blog" page, it includes it as the first post

    can you post a link to illustrate what you would like to change, and maybe add more details?

    at the moment, i understand:
    the showing is fine?
    but the way it shows is wrong?

  5. jadedchron
    Member
    Posted 3 years ago #

    Please see this link: http://www.iriswork.com/

    On the homepage, it displays fine, because there are no post loops (I assume); however, on the sub-pages such as The Work ( http://www.iriswork.com/category/work/best_sellers/ ), it's being included in that post rotation.

    So, I'm trying to prevent it from showing up in all other aspects of the site where it pulls posts because it's including the banner which isn't what I'm trying to do.

    Thanks a bunch for your replies thus far!
    Nick

    P.s. this is if I take out the is_home and rewind_posts, obviously.

  6. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    i assume it is because you are using the same variable names as wordpress uses by default to ahow posts; i.e. in particular $post.

    try to use different names in your code, for instance:

    <?php $banner_posts = get_posts('numberposts=1&category_name=Banner'); ?>
    		<?php if(count($banner_posts) > 0): foreach($banner_posts as $banner_post) : setup_postdata($banner_post); ?>
    
              <div id="banner-top">
    
                  <div id="banner-left">
                    <?php if(short_meta('Image URL')): ?><a href="<?php echo short_meta('Banner URL'); ?>"><img src="<?php echo short_meta('Image URL'); ?>" /></a><?php endif; ?>
                  </div>
                  <div id="banner-right">
                    <h3><a href="<?php echo short_meta('Banner URL'); ?>">IrisWork News & Events</a></h3>
                    <img src="wp-content/uploads/2010/09/divider.jpg" alt="Divider" />
                    <?php the_content(); ?>
                  </div>
                  <div class="clear"><!--clearing--></div>
    
              </div>
    
    		 <?php endforeach; endif; ?>

    hope this does not interfere with 'short_meta()' as this is not a original wordpress function - it might use $post to fetch the custom field?

    if it does interfere, forget the above (hopefully you had made a backup copy of your files before editing) and try to use a 'wp_reset_query();' after the 'endif;' of your code.

  7. jadedchron
    Member
    Posted 3 years ago #

    Hi there.

    I originally tried swapping out the variables but ended up with this:
    http://www.iriswork.com/category/work/best_sellers/

    It seems to lose traction and throw the divider img into the other posts. It's getting close, though...

    Side note: I also didn't know 'short_meta()' wasn't a native function (this code was pulled from another client's sample site). I have two custom fields in the post and I figured short_meta was the appropriate function to retrieve them, but if there's another way, I'm all ears.

  8. jadedchron
    Member
    Posted 3 years ago #

    Here's updated code I'm working with:

    !-- Start Banner -->
                 <?php $banner_posts = get_posts('numberposts=1&category_name=Banner'); ?>
    		<?php if(count($banner_posts) > 0): foreach($banner_posts as $banner_post) : setup_postdata($banner_post); ?>
    
              <div id="banner-top">
    
                  <div id="banner-left">
                    <?php if(get_post_meta($post->ID, 'Image URL', true)): ?><a href="<?php echo get_post_meta($post->ID, 'Banner URL', true); ?>"><img src="<?php echo get_post_meta($post->ID, 'Image URL', true); ?>" /></a><?php endif; ?>
                  </div>
                  <div id="banner-right">
                    <h3><a href="<?php echo get_post_meta($post->ID, 'Banner URL', true); ?>">IrisWork News & Events</a></h3>
                    <img src="wp-content/uploads/2010/09/divider.jpg" alt="Divider" />
                    <?php the_content(); ?>
                  </div>
                  <div class="clear"><!--clearing--></div>
    
              </div>
    
    		 <?php endforeach; endif; ?>
    
    wp_reset_query();
    		 <!-- End Banner -->

    Still same result shown here as described above: http://www.iriswork.com/category/work/best_sellers/

  9. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    the only thing i see, is that you lost your banner image and link:

    with the new variable names, you need to adapt where it says $post->ID to $banner_post->ID

    <!-- Start Banner -->
                 <?php $banner_posts = get_posts('numberposts=1&category_name=Banner'); ?>
    		<?php if(count($banner_posts) > 0): foreach($banner_posts as $banner_post) : setup_postdata($banner_post); ?>
    
              <div id="banner-top">
    
                  <div id="banner-left">
                    <?php if(get_post_meta($banner_post->ID, 'Image URL', true)): ?><a href="<?php echo get_post_meta($banner_post->ID, 'Banner URL', true); ?>"><img src="<?php echo get_post_meta($banner_post->ID, 'Image URL', true); ?>" /></a><?php endif; ?>
                  </div>
                  <div id="banner-right">
                    <h3><a href="<?php echo get_post_meta($banner_post->ID, 'Banner URL', true); ?>">IrisWork News & Events</a></h3>
                    <img src="wp-content/uploads/2010/09/divider.jpg" alt="Divider" />
                    <?php the_content(); ?>
                  </div>
                  <div class="clear"><!--clearing--></div>
    
              </div>
    
    		 <?php endforeach; endif; wp_reset_query(); ?>
    
    		 <!-- End Banner -->

    and 'wp_reset_query();' belongs into the php tags (corrected above)

    can you describe more what the 'leaking into other posts' means?

  10. jadedchron
    Member
    Posted 3 years ago #

    Ah, good eye!

    What I mean by leaking into the other posts is, if you go to the homepage:http://www.iriswork.com/

    and then go to any sub-page which is pulling posts such as: http://www.iriswork.com/category/work/best_sellers/

    ...<img src="wp-content/uploads/2010/09/divider.jpg" alt="Divider" /> divider is showing up multiple times in the main body and also missing from the banner at the top.

    We're so close!

  11. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    the divider is mossing at the top, because it is called with a relative image psth instead of an absolute one; change to:

    change from:

    <img src="wp-content/uploads/2010/09/divider.jpg" alt="Divider" />

    to:

    <img src="<?php bloginfo('wpurl'); ?>/wp-content/uploads/2010/09/divider.jpg" alt="Divider" />

    http://codex.wordpress.org/Function_Reference/bloginfo

    the similar looking line in the posts seems to be coming from this style in style.css of the theme:
    .thepost { padding: 20px; margin: 10px 0; background: transparent url(images/postline_bg.png ) top left no-repeat;}

  12. jadedchron
    Member
    Posted 3 years ago #

    Haha. Oh, goodness. I guess my client modified the header CSS underneath me and it just looked the same.

    Thank you sooooooo much for your help. It's my 3rd or 4th post here on the forums and first time I've received help, so it's greatly appreciated.

    Best,
    Nick

  13. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    you are welcome.

    getting help depends on so many factors, so it is not always guaranteed.

    have a good day, and good luck with your project ;-)

Topic Closed

This topic has been closed to new replies.

About this Topic