So I got this template which originally queried regular WP posts by their categories so that the page would show a category name for “category 1” as a headline and then posts from that category below it. The same for “category 2”, “category 3”, etc.
I’m trying to make it work with custom post types, so that “Post type 1” would be the headline and posts from “Post type 1” would be below it. And again the same for “Post type 2”, etc. Basically the exact same thing but with custom post types instead of categories.
With the code below, I’ll get the headlines to their places, but no posts appear below them. I’m a bit stuck with this and any help would be greatly appreciated.
<?php
$args = array('_builtin' => 0);
$post_types = get_post_types($args);
foreach ($post_types as $post_type) {
$post_type_link = get_post_type_archive_link( $post_type -> $post->ID );
?>
<div class="cat_list full">
<h3 class="h3title"><a href="<?php echo $post_type_link; ?>"><span><?php echo $post_type; ?></span></a></h3>
<div class="cat inline-block">
<ul class="switch half clearfix">
<?php
//$numberposts = admin::get_field_val('category_posts_nbr_front');
$numberposts = admin_options::get_values( 'front_page' , 'nr_post' );
$post_number = 0;
$args = array('post_type' => array( 'make', 'model', 'price' ), 'showposts'=>100, 'orderby'=>'date');
$type_posts = new WP_Query($args); while($type_posts->have_posts()) : $type_posts->the_post();
if(get_post_thumbnail_id($post->ID) )
{
$post_img = wp_get_attachment_image(get_post_thumbnail_id($post->ID),'62x62','' );
}
else
{
$post_img = get_first_image($post->ID,'54x54');
}
?>
<li <?php if($post_number % 2 == 1) echo "class='col_2'" ?>>
<a href="<?php echo get_permalink( $post->ID ); echo '#more'; ?>"><?php echo $post_img; ?></a>
<h5><a href="<?php echo get_permalink($post->ID ); echo '#more'; ?>"><?php echo mb_substr(get_the_title( $post->ID ),0,BLOCK_TITLE_LEN); if(strlen(get_the_title( $post->ID ) ) > BLOCK_TITLE_LEN ) { echo ' ...'; } ?></a></h5>
<span class="date"><?php echo mysql2date(get_option( 'date_format' ) ,$post ->post_date) ?> //</span>
<span class="comm">
<?php if ('open' == $post->comment_status) { ?>
<a href="<?php echo get_permalink( $post->ID); echo '#comments'; ?>"><?php echo $post->comment_count.' '; if($post->comment_count ==1) {_e('Comment');} else {_e(' Comments');} ?> </a>
<?php }else{ // comments are closed ?>
<a><?php _e( 'Comments Off' ); ?></a>
<?php } ?>
</span>
</li>
<?php
$post_number ++;
endwhile;
?>
</ul>
<div class="no_bottom_border"></div>
</div>
</div> <!-- EOF cat_list-->
<?php
} /*EOF foreach categories*/ ?>
EDIT: Well, now it displays the posts below the headlines, but without separating them for their own post types. In other words, every post made is shown under every post type headline. What gives?
I believe the problem is how you are using this code:
You may want to change it to something like this:
So that it only grabs posts for that post type in your
foreach
loop that loops through each post type.Hope this helps.