Support » Themes and Templates » Display one post from certain category

  • Resolved quech

    (@quech)


    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?

Viewing 2 replies - 1 through 2 (of 2 total)
  • 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; ?>

    thanks, problem solved.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Display one post from certain category’ is closed to new replies.