Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I made a page called blog.php which will store my blog entries, all the other pages have a custom post type called "Page Content" (for static content). I wanted to make a heading in this blog page which is retrieved from main loop of the current page (the_content), and used a get_posts to retrieve the blog entries.

I tried doing the opposite: using get_posts to get the_content of the current page but it retrieved the_content of the blog entries. (and using the main loop to retrieve the posts).

Will I have problems by using this method? Is there a better way to do this?

The desired result: enter image description here

<?php
/**
 * Template Name: Blog
 * @package WordPress
 * @subpackage Prominent
 * @since Prominent 1.0
 */
get_header(); ?>
<div class="shadow-top">
 <!-- Shadow at the top of the slider -->
</div>
<div id="intro2">
 <div class="container">
  <?php // Start the Main Loop
  if ( have_posts() ) : ?>
   <?php while ( have_posts() ) : the_post(); ?>
   <div class="content">
     <h2><?php the_content(); ?></h2>
   </div><!-- .content -->
   <?php endwhile; ?>
  <?php endif; ?>
  </div><!-- #slider-wrapper -->
 </div><!-- .container -->
</div><!-- #featured -->
<div class="shadow-bottom">
 <!-- Shadow at the bottom of the slider -->
</div>
<div id="content">
 <div class="container">
  <?php // Create custom loop
  $custom_posts = get_posts(); // Defaults args fetch posts starting with the most recent ?>
  <?php foreach( $custom_posts as $post ) : setup_postdata( $post ); ?>
   <h2><?php the_title(); ?></h2>
   <?php the_content(); ?>
    <?php comments_popup_link( __( 'Leave a comment', 'twentyten' ), __( '1 Comment', 'twentyten' ), __( '% Comments', 'twentyten' ) ); ?>
  <?php endforeach; ?>
<?php wp_reset_query(); ?> </div><!-- .container -->
</div><!-- #content-bottom -->
<?php get_footer(); ?>
share|improve this question
add comment

1 Answer

Use query_posts() instead of get_posts() and you should be able to use the posts as in the normal loop.

Edit - don't forget to reset the default query with wp_reset_query() before using query_posts or I believe the function will again return the page contents.

share|improve this answer
    
the code above works. I get blog entries using get_posts() and the main loop gets the page title and content. So what would be the difference if I use query_posts()? –  janoChen Jan 25 '11 at 20:29
    
@janoChen not much of a difference as far as I can tell, just that using query_posts on its own makes posts behave like in the usual loop, so it asves you a few attributions & calls to setup_postdata –  Cronco Jan 25 '11 at 20:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.