Wordpress Custom Post Type Taxonomy - 获取特定内容
Wordpress Custom Post Type Taxonomy - Get specific content
我有一个食品网站,我有一个名为食谱的自定义 post 类型。
我需要做的是从附加到产品的食谱类别中显示 3 posts。
我已经创建自定义 post 类型并将其附加到我的产品,但我就是无法让它工作!我有点迷茫。我已经设法遍历食谱并获得 3 posts,但我不知道如何过滤掉食谱的类别。
示例:
-Recipe Categories
Sauce
Spicy
假设我有一个产品 "Noodle",我想展示酱类中的 3 个 post。我无法显示它。我总是从每个食谱类别中得到 posts。
这是我用来显示 3 post 的循环。
<?php $loop = new WP_Query( array( 'post_type' => 'recipes', 'posts_per_page' => 3 ) );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<a href="<?php the_permalink(); ?>">
<img src="<?php the_post_thumbnail_url(); ?>">
<h4><?php the_title(); ?></h4>
</a>
<?php endwhile; ?>
我尝试将分类法类别添加到我的数组参数中,但没有任何反应!
这是我尝试做的(有很多变化):
$mytaxonomy = 'recipe_category';
$myterms = get_the_terms($post, $mytaxonomy);
然后我使用与上面相同的方法在数组中添加项。
有人可以帮帮我吗?我迷路了,被困住了,但我需要知道为什么它不起作用,这样我才能提高自己。
WP_Query也支持tax_query
按类别获取post,试试看:
global $post;
$terms = get_the_terms($post->ID, 'recipe_category');
$recipe_cat_slug = array();
foreach ($terms as $term)
{
$recipe_cat_slug[] = $term->slug;
}
$args = array(
'post_type' => 'recipes',
'posts_per_page' => 3,
'tax_query' => array(
array(
'taxonomy' => 'recipe_category',
'field' => 'slug', //can be set to ID
'terms' => $recipe_cat_slug //if field is ID you can reference by cat/term number; you can also pass multiple cat as => array('sauce', 'spicy')
)
)
);
$loop = new WP_Query($args);
希望对您有所帮助!
我有一个食品网站,我有一个名为食谱的自定义 post 类型。 我需要做的是从附加到产品的食谱类别中显示 3 posts。 我已经创建自定义 post 类型并将其附加到我的产品,但我就是无法让它工作!我有点迷茫。我已经设法遍历食谱并获得 3 posts,但我不知道如何过滤掉食谱的类别。
示例:
-Recipe Categories
Sauce
Spicy
假设我有一个产品 "Noodle",我想展示酱类中的 3 个 post。我无法显示它。我总是从每个食谱类别中得到 posts。
这是我用来显示 3 post 的循环。
<?php $loop = new WP_Query( array( 'post_type' => 'recipes', 'posts_per_page' => 3 ) );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<a href="<?php the_permalink(); ?>">
<img src="<?php the_post_thumbnail_url(); ?>">
<h4><?php the_title(); ?></h4>
</a>
<?php endwhile; ?>
我尝试将分类法类别添加到我的数组参数中,但没有任何反应! 这是我尝试做的(有很多变化):
$mytaxonomy = 'recipe_category';
$myterms = get_the_terms($post, $mytaxonomy);
然后我使用与上面相同的方法在数组中添加项。 有人可以帮帮我吗?我迷路了,被困住了,但我需要知道为什么它不起作用,这样我才能提高自己。
WP_Query也支持tax_query
按类别获取post,试试看:
global $post;
$terms = get_the_terms($post->ID, 'recipe_category');
$recipe_cat_slug = array();
foreach ($terms as $term)
{
$recipe_cat_slug[] = $term->slug;
}
$args = array(
'post_type' => 'recipes',
'posts_per_page' => 3,
'tax_query' => array(
array(
'taxonomy' => 'recipe_category',
'field' => 'slug', //can be set to ID
'terms' => $recipe_cat_slug //if field is ID you can reference by cat/term number; you can also pass multiple cat as => array('sauce', 'spicy')
)
)
);
$loop = new WP_Query($args);
希望对您有所帮助!