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.

Is it possible to have a section on a page that shows 2 excerpts from 4 different category posts, including their featured images?

All I have so far is something like:

            <?php query_posts("showposts=2&cat=11,12,13,14"); ?>
            <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
               <?php the_excerpt(); ?>
               <?php featured-image(); ?>
            <?php endwhile; endif; ?>

            <?php wp_reset_query(); ?>

but I'm not sure if it would display 4 x 2 excerpts with a featured image in each excerpt?

My custom categories have been added like this:

function event_post_example() {

register_post_type( 'event_type',
    array(
    'labels' => array(
        'name' => __('Events Posts', 'baretheme'),
        'singular_name' => __('Event Post', 'baretheme'),
        'all_items' => __('All Event Posts', 'baretheme'),
        'add_new' => __('Add New Event Post', 'baretheme'),
        'add_new_item' => __('Add New Event Type', 'baretheme'),
        'edit' => __( 'Edit', 'baretheme' ),
        'edit_item' => __('Edit Post Types', 'baretheme'),
        'new_item' => __('New Post Type', 'baretheme'),
        'view_item' => __('View Post Type', 'baretheme'),
        'search_items' => __('Search Post Type', 'baretheme'),
        'not_found' =>  __('Nothing found in the Database.', 'baretheme'),
        'not_found_in_trash' => __('Nothing found in Trash', 'baretheme'),
        'parent_item_colon' => ''
    ), /* end of arrays */
    'description' => __( 'This is the example event post type', 'baretheme' ), /* Custom Type Description */
    'public' => true,
    'publicly_queryable' => true,
    'exclude_from_search' => false,
    'show_ui' => true,
    'query_var' => true,
    'menu_position' => 9,
    'menu_icon' => get_stylesheet_directory_uri() . '/library/images/custom-post-icon.png',
    'rewrite'   => array( 'slug' => 'event_type', 'with_front' => false ),
    'has_archive' => 'event_type',
    'capability_type' => 'post',
    'hierarchical' => false,

    'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'sticky')
    ) /* end of options */
); /* end of register post type */

register_taxonomy( 'event_cat','event_type',
    array(
        'labels' => array(
        'name' => __( 'Event Categories', 'baretheme' ),
        'singular_name' => __( 'Event Category', 'baretheme' ),
        'search_items' =>  __( 'Search Event Categories', 'baretheme' ),
        'all_items' => __( 'All Event Categories', 'baretheme' ),
        'parent_item' => __( 'Parent Event Category', 'baretheme' ),
        'parent_item_colon' => __( 'Parent Event Category:', 'baretheme' ),
        'edit_item' => __( 'Edit Event Category', 'baretheme' ),
        'update_item' => __( 'Update Event Category', 'baretheme' ),
        'add_new_item' => __( 'Add New Event Category', 'baretheme' ),
        'new_item_name' => __( 'New Event Category Name', 'baretheme' )
    ),
    'hierarchical' => true,
    'show_admin_column' => true,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'event-slug' ),
    )
);

register_taxonomy( 'event_tag','event_type',
    array(
        'labels' => array(
        'name' => __( 'Event Tags', 'baretheme' ),
        'singular_name' => __( 'Event Tag', 'baretheme' ),
        'search_items' =>  __( 'Search Event Tags', 'baretheme' ),
        'all_items' => __( 'All Event Tags', 'baretheme' ),
        'parent_item' => __( 'Parent Event Tag', 'baretheme' ),
        'parent_item_colon' => __( 'Parent Event Tag:', 'baretheme' ),
        'edit_item' => __( 'Edit Event Tag', 'baretheme' ),
        'update_item' => __( 'Update Event Tag', 'baretheme' ),
        'add_new_item' => __( 'Add New Event Tag', 'baretheme' ),
        'new_item_name' => __( 'New Event Tag Name', 'baretheme' )
    ),
    'hierarchical' => false,
    'show_admin_column' => true,
    'show_ui' => true,
    'query_var' => true,
    )
);
}

add_action( 'init', 'event_post_example');
share|improve this question

2 Answers 2

up vote 1 down vote accepted

I first want to jump in and say this, you should not be using query_posts, my emphasis, and stated in the codex as well, and I quote

Note: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination)

That said, you can use WP_Query to construct your custom loop. You will need to run four different loops here as you are going to display 2 posts each from three different taxonomies and one category. You also need to display the_excerpt() and the_post_thumbnail()

The following parameters can be used here:

  • posts_per_page to set the amount of posts to retrieve to 2
  • cat to get the posts from a specific category for one loop
  • tax_query to get the posts from the taxonomies

Here is the complete query to get the posts from the category. Just change the cat ID to the ID of your category, and add all the html mark up that you need

<?php 
$args = array(
    'cat' => 21,
    'posts_per_page' => '2'
);

$the_query = new WP_Query( $args );

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

        if ( has_post_thumbnail() ):
            the_post_thumbnail();
        endif;

        the_excerpt(); 

    endwhile; 
wp_reset_postdata(); 
endif; 

For the taxonomies, this will do the trick. Remember to go and have a look at WP_Query

$args = array(
'post_type' => 'event_type',
'posts_per_page' => '2'
    'tax_query' => array(
        array(
            'taxonomy' => 'event_cat',
            'field' => 'slug',
            'terms' => 'testcat'
        )
    )
);
$the_query = new WP_Query( $args );

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

        if ( has_post_thumbnail() ):
            the_post_thumbnail();
        endif;

        the_excerpt(); 

    endwhile; 
wp_reset_postdata(); 
endif; 
share|improve this answer
    
Thanks this worked for my standard category posts but not my custom category posts types. So say number 21 are ordinary category posts but 41,13, and 7 are custom posts, they for some reason don't show? –  Nsokyi Jun 18 at 10:20
    
You should change the cat numbers to your own category ID's. The category ID's in my code was for my site's categories. I just used those to test my code. –  Pieter Goosen Jun 18 at 10:27
    
Sorry I should have mentioned that I did that already. –  Nsokyi Jun 18 at 10:29
    
Are three of your categories connected to custom post types and one to normal posts, is that what you are saying –  Pieter Goosen Jun 18 at 10:30
    
Yes that's right –  Nsokyi Jun 18 at 10:31

This should get you started: (I'll Combine Pieter's answer with this one with comments so that you understand where and why there are loops:

<?php
// We build the query as an array to
// allow us to update it easily in one place
// without breaking it
$sql_args = array('category__and' => array(11,12,13,14), 'orderby' = rand, 'posts_per_page' = 2);
// We now "overload" the function with the array
// we constructed above.
$custom_query = new WP_Query($sql_args);
// OuterLoop, this is the loop you need to customize
  while ($custom_query->have_posts()) : $custom_query->the_post();
  $do_not_duplicate[] = $post->ID;
     if ( has_post_thumbnail() ):
        the_post_thumbnail();
    endif;

    the_excerpt(); 
 endwhile;
    wp_reset_postdata();

// InnerLoop, we can manipulate items further
// in the default WordPress query. As Pieter 
// pointed out, do not use query_posts.
      if (have_posts()) : while (have_posts()) : the_post(); 
      if(in_array ($post->ID == $do_not_duplicate )) continue;
// Show all Posts in all categories
// that weren't chosen by random above.
// If you don't want to display the leftovers
// Remove the InnerLoop
       the_content();
  endwhile;
 endif; ?>
share|improve this answer
    
Thanks< I ran with it but I got an error the line $custom_query = array('cat'=11,12,13,14, "syntax error, unexpected '=', expecting ')'" not sure what that means sorry? –  Nsokyi Jun 18 at 9:38
    
@Nsokyi Fixed The arguements are now all strings, as per the WP_Query Reference –  eyoung100 Jun 18 at 13:29
    
Thanks but I get a Parse error line 10? new WP_Query = ($custom_query) –  Nsokyi Jun 18 at 13:34
    
Try now, crazy finicky compiler, also, changed cat to category__and. This is getting more complicated. Notice the stacked arrays. If that still complains, build the query in sections, until a section complains, then tell me where the parser is gagging :) –  eyoung100 Jun 18 at 13:49
    
d'oh! Still the same line I'm afraid? new WP_Query = ($custom_query) –  Nsokyi Jun 18 at 13:59

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.