好的,这可能很简单。但由于某种原因,我似乎无法弄清楚。
我有一个自定义帖子类型,称为:海滩事件。我有几个活动。我还有一个自定义分类法,叫做:Thema。
在制作我的海滩活动页面(不是帖子)时,我创建了某些类型的主题(主题)。比如:Strand Spellen(slug是strand-spellen)。
现在我想做一个循环,显示唯一的带有缩略图和所有这些东西的链拼写。
有谁知道我是怎么做到的?
我尝试了一些这样的代码,但没有做到这一点。
$args = array(
'post_type' => 'beachevents',
'posts_per_page'=> -1,
'tax_query' => array(
array(
'taxonomy' => 'strand-spellen',
'field' => 'slug',
'terms' => 'all'
)
)
);
$products = new WP_Query( $args );
if( $products->have_posts() ) {
while( $products->have_posts() ) {
$products->the_post();
?>
<div class='content'>
<h2><?php the_title(); ?></h2>
</div>
<?php
}
}
else {
echo 'There seems to be a problem, please try searching again or contact customer support!';
}
谢谢!
你很接近!
在您的tax_query中,taxonomy
需要提及"海滩事件",terms
需要提及"strand-spellen"。
因此,您的代码将如下所示:
'tax_query' => array(
array(
'taxonomy' => 'thema',
'field' => 'slug',
'terms' => 'strand-spellen'
)
)
有关构建查询的详细信息,您可能会发现WP_Query文档很有用 - 其中有一节是关于分类查询的。
感谢蒂姆的帮助。这是我为遇到相同问题的人提供的完整代码。
<?php $args = array(
'post_type' => 'beachevents',
'posts_per_page'=> -1,
'orderby' => 'title',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'thema',
'field' => 'slug',
'terms' => 'strand-spellen'
)
)
);
$products = new WP_Query( $args );
if( $products->have_posts() ) {
while( $products->have_posts() ) {
$products->the_post();
?>
<div class='content'>
<h2><?php the_title(); ?></h2>
</div>
<?php
}
}
else {
echo 'There seems to be a problem, please try searching again or contact customer support!';
} ?>
包括按标题和ASC排序。希望我编码正确...