WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display one post from certain category (3 posts)

  1. quech
    Member
    Posted 6 years ago #

    I have this page: http://www.lichens.ie/index.php and I want to pull 2 newest posts from 2 different categories. So I'm using this piece of code:

    <?php
    			 $imposts = get_posts('numberposts=1&category=4');
    			 foreach($imposts as $post) :
    			?>
    				<div class="post" id="home-page-featured-1">
    					<p class="subtitle">Lichen of the month</p>
    					<h2><a><?php the_title() ?></a></h2>
    					<?php the_content('<span class="readmore">...read more</span>'); ?>
    					<div class="clear"></div>
    				</div>
    			<?php endforeach; ?>
    
    			<?php
    			 $imposts = get_posts('numberposts=1&category=3');
    			 foreach($imposts as $post) :
    			?>
    				<div class="post" id="home-page-featured-2">
    					<p class="subtitle">Have you seen this lichen?</p>
    					<h2><a><?php the_title() ?></a></h2>
    					<?php the_content('<span class="readmore">...read more</span>'); ?>
    					<div class="clear"></div>
    				</div>
    			<?php endforeach; ?>

    ...and all I get are just titles - content won't display. Why is that so?

  2. wp_guy
    Member
    Posted 6 years ago #

    Try adding setup_postdata($post); right after your foreach lines... like this:

    $imposts = get_posts('numberposts=1&category=4');
    			 foreach($imposts as $post) :
    			 setup_postdata($post);
    			?>
    				<div class="post" id="home-page-featured-1">
    					<p class="subtitle">Lichen of the month</p>
    					<h2><a><?php the_title() ?></a></h2>
    					<?php the_content('<span class="readmore">...read more</span>'); ?>
    					<div class="clear"></div>
    				</div>
    			<?php endforeach; ?>
    
    			<?php
    			 $imposts = get_posts('numberposts=1&category=3');
    			 foreach($imposts as $post) :
    			 setup_postdata($post);
    			?>
    				<div class="post" id="home-page-featured-2">
    					<p class="subtitle">Have you seen this lichen?</p>
    					<h2><a><?php the_title() ?></a></h2>
    					<?php the_content('<span class="readmore">...read more</span>'); ?>
    					<div class="clear"></div>
    				</div>
    			<?php endforeach; ?>
  3. quech
    Member
    Posted 6 years ago #

    thanks, problem solved.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.