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 have made a page where you people can find al the teasers (teaser made with the read more tag) from my posts.( By clicking one of them you "open" the whole article, including photo's, ... . This is working well. On my homepage I want a short welcome text and in a sidebar my 3 recent posts. I'm first trying to get the recent posts on my homepage with following code

<?php
function recentPosts() {
    $rPosts = new WP_Query();
    $rPosts->query('showposts=3');
        while ($rPosts->have_posts()) : $rPosts->the_post(); ?>

                <a href="<?php the_permalink();?>"><?php the_post_thumbnail('recent-thumbnails'); ?></a>
                <h2><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h2>
                <?php the_content(); ?>

        <?php endwhile; 
    wp_reset_query();
}
?>

This is working well but on my homepage I'm getting all the content from my articles and I only want a teaser. (2 lines for example). I don't know how to do this here. Below you see the code of the home page.

<?php
/**
 * @content not visible
 * @content not visible
 *
 * Template Name: homepage
 */
?>
<?php get_header(); ?>
<?php echo recentPosts(); ?>
<?php the_content(); ?>
<?php get_footer(); ?>
share|improve this question
    
How do you want this "welcome text" to be added? I assume, via the post content for the page assigned as page_for_posts? –  Chip Bennett Jan 29 '13 at 18:39
add comment

3 Answers

Your code is calling the the_content() function, which returns the whole post.

If all you want is a "teaser" then your code should call the_excerpt() function

share|improve this answer
add comment

First, ensure that your template file is named home.php.

Second, there's no need to use a custom query loop in this context. If you only want to display 3 posts on the blog posts index (i.e. the "homepage"), then filter the main loop query via pre_get_posts:

function wpse83660_filter_pre_get_posts( $query ) {
    if ( is_home() && $query->is_main_query() ) {
        $query->set( 'posts_per_page', '3' );
    }
}
add_action( 'pre_get_posts', 'wpse83660_filter_pre_get_posts' );

And you can replace your entire recentPosts() function with a normal loop.

Thurd, you're calling the_content() both outside of a loop instantiation, and expecting it to return content other than what it will return:

<?php get_header(); ?>
<?php the_content(); ?>
<?php get_footer(); ?>

This call to get_content() has 2 problems:

  1. You haven't instantiated a loop:

    if ( have_posts() ) : while ( have_posts() ) : the_post();
    
  2. In the blog posts index context, this call to the_content() will return the content of the posts in the blog posts index, not the content of the *page assigned as page_for_posts

By solving the second problem, we'll fix the first as well.

To get at the post_content for the page assigned as page_for_posts, use get_page(), and pass it get_option( 'page_for_posts' ):

$page_object = get_page( get_option( 'page_for_posts' ) );

Then, output its results:

if ( ! is_null( $page_object ) ) {
    echo apply_filters( 'the_content', $page_object->post_content );
}

So, your entire template file will look like so:

/**
 * Blog posts index template file
 *
 * Displays the blog posts index
 * 
 * @filename: home.php
 */

get_header();

if ( have_posts() ) : while ( have_posts() ) : the_post();

    <a href="<?php the_permalink();?>"><?php the_post_thumbnail('recent-thumbnails'); ?></a>
    <h2><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h2>
    <?php the_content(); ?>

endwhile; endif;

$page_object = get_page( get_option( 'page_for_posts' ) );
if ( ! is_null( $page_object ) ) {
    echo apply_filters( 'the_content', $page_object->post_content );
}

get_footer();
share|improve this answer
    
Bennet, When you want to retrieve the content from a page you don't use the_content() ? The_content() is only used for content from blogposts? Is this right? –  Niels Jansen Jan 29 '13 at 22:42
    
I have entered the following code into my template file but nothing happened. There is no content in it... –  Niels Jansen Jan 29 '13 at 23:15
    
Let's back up a bit: I'm assuming that you're using a static front page, and that you're displaying your blog posts index on a separate static page? Is this assumption incorrect? –  Chip Bennett Jan 29 '13 at 23:18
    
Under settings, reading, I have a static frontpage and I also assigned a static page to display the blogposts. (I first create a blank page, and then assigned this page to display blog posts) –  Niels Jansen Jan 29 '13 at 23:38
    
Okay, so, in that case, for the static page you've assigned to display blog posts, you can't access post_content via the_content(), because that page is considered a special/system page. That's why you have to query that page separately, and output post_content directly from the returned object. You could call setup_postdata(), but that adds a couple of unnecessary steps, both to setup_postdata(), and then to wp_reset_postdata() afterward. –  Chip Bennett Jan 29 '13 at 23:42
show 3 more comments

Add <?php global $more; $more = 0; ?> right after/below while ($rPosts->have_posts()) : $rPosts->the_post(); ?> and you should get your "More" tags working.

share|improve this answer
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.