0

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; ?> 
14
  • what is the parent theme?
    – iEmanuele
    Aug 23, 2013 at 10:45
  • 1
    @iEmanuele TwentyTwelve
    – localhost
    Aug 23, 2013 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, 2013 at 11:42
  • @iEmanuele my query, what you mean?
    – localhost
    Aug 23, 2013 at 11:51
  • are you sure you dont have content and or excerpt area not blank at the back end ? Aug 23, 2013 at 11:53

1 Answer 1

1

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;
7
  • 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.
    – localhost
    Aug 23, 2013 at 12:38
  • yeah! yes sure, i've updated the answer look at the end
    – iEmanuele
    Aug 23, 2013 at 12:39
  • You can tune the $args array to run a query that fits your needs
    – iEmanuele
    Aug 23, 2013 at 12:53
  • It was the same also for me : )
    – iEmanuele
    Aug 23, 2013 at 13:07
  • for some reason my code isn't getting posts from a specific category, this is my code if( $query->have_posts(in_category('6')) ) : while( $query->have_posts() ) : $query->the_post(); i tried to test it with a category that doesn't exist and it still shows all post. am i missing something?
    – localhost
    Aug 23, 2013 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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