admin管理员组文章数量:1326447
I am using the Custom Post Type feature in Wordpress 3 to create an Events section for my website. I am able to get this to display by using the relevante template files, but I also want the event listings visible on other pages.
I've done this with the normal post types but these are based on category IDs which Custom Post Types don't have.
Each event has a taxonomy for country and I want to be able to loop through the events and display only events for specific countries so I want to be able to filter that.
I've had a look on the Codex and came up with the following but it doesn't display anything:
<?php $args = array(
'post_type'=> 'events',
'country' => 'england'
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
endif;
// Reset Post Data
wp_reset_postdata();
?>
I am using the Custom Post Type feature in Wordpress 3 to create an Events section for my website. I am able to get this to display by using the relevante template files, but I also want the event listings visible on other pages.
I've done this with the normal post types but these are based on category IDs which Custom Post Types don't have.
Each event has a taxonomy for country and I want to be able to loop through the events and display only events for specific countries so I want to be able to filter that.
I've had a look on the Codex and came up with the following but it doesn't display anything:
<?php $args = array(
'post_type'=> 'events',
'country' => 'england'
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
endif;
// Reset Post Data
wp_reset_postdata();
?>
Share
Improve this question
edited May 24, 2011 at 10:20
thisisready
asked May 12, 2011 at 10:39
thisisreadythisisready
2171 gold badge4 silver badges10 bronze badges
3 Answers
Reset to default 2I've figured out the answer using the Wordpress forum. Solution below:
<?php query_posts( array( 'country' => 'event-england' ) ); ?>
<?php if( is_tax() ) {
global $wp_query;
$term = $wp_query->get_queried_object();
$title = $term->name;
} ?>
<ul>
<span class="tax-title"><?php echo($title); ?></span>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a><?php the_excerpt() ?></li>
<?php endwhile; else: ?>
<?php endif; ?>
</ul>
Every taxonomy (built in like 'tags', 'post-formats', 'category', as well as custom) have an ID, so you can get this one as well.
As far as i can see your query should work if you just add the usual if ( $the_query->have_posts() ) :
before your loop and endif;
after it.
This Worked For Me
I hope it helps you or anyone else coming across this "issue"...
So, assuming you registered your Custom Post Type as well as your Taxonomy(I registered mine in functions.php) --> How to register your CPT and Taxonomy
On the page I wanted to display the posts from that taxonomy I used this code...
<div class="the-div-you-want-to-loop-in">
<?php $query = new WP_Query(array(
'post_type' => 'your-post-type', // Your Post Type
'posts_per_page' => -1, // To Display All Posts
'tax_query' => array(
array(
'taxonomy' => 'your-taxonomy',
'field' => 'slug',
'terms' => 'your-filter', // (the name of what you want to filter by (latest or whatever))
)
))); ?>
<?php if($query->have_posts()): while($query->have_posts()): $query->the_post(); ?>
<div class="my-post">
<?php responsive_img(get_post_thumbnail_id(), 'large'); ?>
<h2><?php the_title(); ?></h2>
</div>
<?php wp_reset_postdata(); ?>
<?php endwhile; endif; ?>
</div>
本文标签: Loop for custom post types filtered by a taxonomy
版权声明:本文标题:Loop for custom post types filtered by a taxonomy 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742213888a2434212.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论