I am working with custom post types in the bones theme from themble. There, I want to create a link list and therefor i've created a custom post type, called links.
This custom post type has a custom taxonomy, called links_categories. Now I would like to display all links on one page, like this:
Link Category 1
- Post 1
- Post 2
- ...
Link Category 2
- Post 1
- Post 2
- ...
While researching on how to achieve this dynamically, i tried this two solutions:
Loop through custom taxonomies and display posts
Custom post type multiple loop by taxonomy term
Strangely enough I had the same issue with both versions. It seems like WP_Query is not getting any posts - I just get the slug of the custom category term.
Here is what I have for now:
<?php get_header(); ?>
<div id="content">
<div id="inner-content" class="wrap clearfix">
<div id="main" class="eightcol first clearfix" role="main">
<h1 class="archive-title h2"><?php post_type_archive_title(); ?></h1>
<?php
$post_type = 'links';
// Get all the taxonomies for this post type
$taxonomies = get_object_taxonomies( (object) array( 'post_type' => $post_type ) );
foreach( $taxonomies as $taxonomy ) :
// Gets every "category" (term) in this taxonomy to get the respective posts
$terms = get_terms( $taxonomy );
foreach( $terms as $term ) :
$wp_query = new WP_Query( array(
'taxonomy' => $taxonomy,
'term' => $term->slug,
'posts_per_page' => '-1'
)
);
if( $wp_query->have_posts() ) :
while( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?> role="article">
<header class="article-header">
</header> <!-- end article header -->
<section class="entry-content clearfix">
<div class="threecol first">
<?php // show the post thumbnail
if ( has_post_thumbnail() ) { ?>
<a class="fadeit" href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
</a>
<?php } ?>
</div>
<div class="ninecol">
<strong><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></strong>
<?php the_content(); ?>
</div>
</section> <!-- end article section -->
<footer class="article-footer">
</footer> <!-- end article footer -->
</article> <!-- end article --> <?php
endwhile;
else : ?>
<article id="post-not-found" class="hentry clearfix">
<header class="article-header">
<h1><?php _e("Oops, Post Not Found!", "bonestheme"); ?></h1>
</header>
<section class="entry-content">
<p><?php _e("Uh Oh. Something is missing. Try double checking things.", "bonestheme"); ?></p>
</section>
<footer class="article-footer">
<p><?php _e("This is the error message in the custom posty type archive template.", "bonestheme"); ?></p>
</footer>
</article> <?php
endif;
endforeach;
endforeach;
?>
</div> <!-- end #main -->
<?php get_sidebar(); ?>
</div> <!-- end #inner-content -->
</div> <!-- end #content -->
var-dump($wp_query);
right before yourif( $wp_query->have_posts() ) :
you get an object with nothing in it but the slug term? Also, WordPress uses$wp-query
. You are clobbering that object, so choose another name for your new object.