WordPress.org

Ready to get started?Download WordPress

Forums

showing one post form a category before regular page content (5 posts)

  1. fitzpatrick512
    Member
    Posted 2 years ago #

    So I'm using this bit of code to display the content of one post from a category on my homepage.

    <?php $wp_query = new WP_Query(array('posts_per_page' => 1, 'cat'=> 14 ) ); ?>
    <?php if($wp_query->have_posts()) : while($wp_query->have_posts()) : $wp_query->the_post(); ?>
    <h2><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>
    <?php the_content();?>
    <?php endwhile; ?>
    <?php endif; // do not delete ?>

    I'd like to use the same bit of code to display the same post above the Page content of a certain page. The loop doens't seem to close properly and instead of showing the post content, and then the normal page content, it's showing the post content twice:

    [Code moderated as per the Forum Rules. Please use the pastebin]

    What am I doing wrong?

  2. fitzpatrick512
    Member
    Posted 2 years ago #

    ok, pastebin is down, but i have that on top and this below:

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
                <div class="posttitle">
                	<h3><?php the_title(); ?></h3>
                </div>
    
                <?php the_content(__("Read More", 'organicthemes'));?><div style="clear:both;"></div><?php edit_post_link(__("(Edit)", 'organicthemes'), '', ''); ?>
    
                <?php endwhile; else: ?>
    			<p><?php _e("Sorry, no posts matched your criteria.", 'organicthemes'); ?></p>
    			<?php endif; ?>
  3. fitzpatrick512
    Member
    Posted 2 years ago #

    So it seems that I need something between the two to close the loop? What's happening is that it shows the post above as desired, but then below, instead of showing the page content, it just shows the post content again.

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

    either use a different variable name for the query (i.e. don't use $wp_query which is the default variable name), or try to use wp_reset_query() after the endif of the first look.

  5. dtrickky
    Member
    Posted 2 years ago #

    Yes, I would try a different variable & a query reset.

    If that doesn't work, you could try a get_posts query above the page content.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.