在 Wordpress 中按标签获取帖子
Get Posts by Tag in Wordpress
我想在我的主页模板中获取带有特定标签的帖子 "iinluv"
我写了查询代码但失败了。
这是我的代码。
<ul class="team-list">
<?php $args = array( 'post_type' => 'product', 'tag' => 'iinluv', 'posts_per_page' => -1, );
$loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); global $product; $title = get_the_title(); $ptitle = $title;?>
<!-- TEAM THUMB -->
<li class="team-block bg-alt">
<?php echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog imageproduct'); ?>
</li>
<!-- TEAM THUMB -->
<?php endwhile;?>
</ul>
请检查我的代码并给我好的建议来完成它。
假设您正在使用 WooCommerce
,因为您已将 product
指定为 post_type
。要从特定标签或类别中获取帖子,您必须使用 tax_query
。检查以下示例。
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_tag',
'field' => 'slug',
'terms' => 'iinluv',
),
),
);
我想在我的主页模板中获取带有特定标签的帖子 "iinluv" 我写了查询代码但失败了。 这是我的代码。
<ul class="team-list">
<?php $args = array( 'post_type' => 'product', 'tag' => 'iinluv', 'posts_per_page' => -1, );
$loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); global $product; $title = get_the_title(); $ptitle = $title;?>
<!-- TEAM THUMB -->
<li class="team-block bg-alt">
<?php echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog imageproduct'); ?>
</li>
<!-- TEAM THUMB -->
<?php endwhile;?>
</ul>
请检查我的代码并给我好的建议来完成它。
假设您正在使用 WooCommerce
,因为您已将 product
指定为 post_type
。要从特定标签或类别中获取帖子,您必须使用 tax_query
。检查以下示例。
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_tag',
'field' => 'slug',
'terms' => 'iinluv',
),
),
);