tokmak
tokmak

Reputation: 459

Display custom post type excerpt on a page

I have created a simple custom post type that goes like this:

add_action( 'init', 'create_services_post_type' );

function create_services_post_type() {
    $args = array(
                  'description' => 'Services post type',
                  'show_ui' => true,
                  'menu_position' => 4,
                  'exclude_from_search' => true,
                  'labels' => array(
                                    'name'=> 'Services',
                                    'singular_name' => 'Service',
                                    'add_new' => 'Add New Service',
                                    'add_new_item' => 'Add New Service',
                                    'edit' => 'Edit Services',
                                    'edit_item' => 'Edit Service',
                                    'new-item' => 'New Service',
                                    'view' => 'View Services',
                                    'view_item' => 'View Service',
                                    'search_items' => 'Search Services',
                                    'not_found' => 'No Services Found',
                                    'not_found_in_trash' => 'No Services Found in Trash',
                                    'parent' => 'Parent Service'
                                   ),
                 'public' => true,
                 'capability_type' => 'post',
                 'hierarchical' => false,
                 'rewrite' => true,
                 'supports' => array('title', 'editor', 'thumbnail', 'comments')
                 );
    register_post_type( 'services' , $args );
}

It is very simple and it works as it should.Now, i created a new page template on which i would like to display 3 newest post excerpts from that category (Services), but since i am a PHP newbie, no matter how much i tried, i couldnt make it work.

Any good soul want to help me? Thanks a bunch!

Upvotes: 0

Views: 4490

Answers (1)

WpTricks24
WpTricks24

Reputation: 825

Try Using this code where you want to show the result :

$args = array( 'post_type' => 'services', 'posts_per_page' => 3,'cat' => cat_id_of_services_category );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
  the_title();
  echo '<div class="entry-content">';
  the_excerpt();
  echo '</div>';
endwhile;

If in a case you will not get the required result, then just have a look here : http://codex.wordpress.org/Class_Reference/WP_Query

Upvotes: 2

Related Questions