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 a custom template for homepage and in it, i want to display excerpt from some category. but not even the simple the_excerpt() isn't working. i tried the_title and the_content, but only title display not content. The following is my code,am i missing something?

 <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
       <?php the_excerpt();?>
     <?php endwhile;?>
       <?php endif; ?> 
share|improve this question
    
what is the parent theme? –  iEmanuele Aug 23 '13 at 10:45
1  
@iEmanuele TwentyTwelve –  Nofel Aug 23 '13 at 10:53
    
this problem is unbelievable! I tried it with a child theme of TwentyTwelve and I could not replicate the fault. It's working fine. Last question: have you tried to use a your own query to get those posts? –  iEmanuele Aug 23 '13 at 11:42
    
@iEmanuele my query, what you mean? –  Nofel Aug 23 '13 at 11:51
    
are you sure you dont have content and or excerpt area not blank at the back end ? –  pr1nc3 Aug 23 '13 at 11:53

1 Answer 1

up vote 1 down vote accepted

Inside a template file, the standard loop syntax will display only the information about that page( In fact, a template file is no more than a page ). If you want to display some posts you need to run a custom query or to include ( get_template_part() ) another template file which normally runs a query for some specific post types or categories.

Page template example

/*Template name: Example page */
get_header();
    /* The following loop refers to the page object */
    if( have_posts() ) : while( have_posts() ) : the_post();
        the_title();
        the_excerpt();
    endwhile; endif;

    /* Now you can query for your post using WP_Query Class */
    /* Defining $args array of arguments */
    $args = array(
        'post_type'         => 'post', // maybe page | custom post type | an array of these
        'posts_per_page'    => 6,      // number of items you want to display
    );

    /* Define new $query object to loop through */
    $query = new WP_Query( $args );

    //Start your custom loop
    if( $query->have_posts() ) : while( $query->have_posts() ) : $query->the_post();
        the_title();
        the_excerpt();
    endwhile; else: 
        echo 'No posts found';
    endif;      
 get_footer();

Let me know if you get stuck : )

Please, look these from WordPress Codex:

  1. Templates
  2. Stepping into templates
  3. Template Hierarchy
  4. The Loop
  5. WP_Query

UPDATE

If you look at my example code you will see an $args array. You can define several options within it please refer to WP_Query docs. Note: have_posts(); and the_post(); functions don't accept any arguments. If you want to use the in_category(); you have to do it in the loop.

Custom loop

if( $query->have_posts() ) : while( $query->have_posts() ) : $query->the_post();

  if( in_category( 6 ) ){
    //do something
  }else{
    //do something else
  }

endwhile; endif;
share|improve this answer
    
it worked! i was only applying the simple page loop, this is the first time i came across wp_query_class and $args,never came across that. is that something i can read in codex/wp_query ?, i see its based on oop as it point out to the object -> in array, now what if i want to get posts from a specific category and loop over it as i want to display it in div's and i am unknown by OOP as used. –  Nofel Aug 23 '13 at 12:38
    
yeah! yes sure, i've updated the answer look at the end –  iEmanuele Aug 23 '13 at 12:39
    
You can tune the $args array to run a query that fits your needs –  iEmanuele Aug 23 '13 at 12:53
    
thank you! this problem had my head scratching for days –  Nofel Aug 23 '13 at 12:59
    
It was the same also for me : ) –  iEmanuele Aug 23 '13 at 13:07

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.