获取分配给自定义 post 类型的标签
Get a tag assigned to custom post type
我正在使用 WooCommerce
并用标签“electronic”标记了 products
之一(自定义 post 类型是 product
)。
我现在正在尝试遍历并获取分配给 post 的标签,但目前,在尝试转储数据时,我返回 bool(false)
。
这是我的方法:
<?php
$args = array(
'post_type' => 'product',
'p' => $product_name,
'posts_per_page' => 1
);
$loop = new WP_Query($args);
while ($loop->have_posts()):
$loop->the_post();
$posttags = get_the_tags();
if ($posttags)
{
foreach ($posttags as $tag)
{
echo $tag->name;
}
}
var_dump($posttags);
endwhile;
wp_reset_query();
?>
<?php
$args = array(
'post_type' => 'product',
'p' => $product_name,
'posts_per_page' => 1
);
$loop = new WP_Query($args);
while ($loop->have_posts()):
$loop->the_post();
$posttags = get_the_tags();
if ($posttags)
{
foreach ($posttags as $tag)
{
echo $tag->name;
}
}
var_dump($posttags);
endwhile;
wp_reset_query();
?>
尝试过:
<?php
global $post;
$args = array(
'post_type' => 'product',
'p' => $product_name,
'posts_per_page' => 1
);
$loop = new WP_Query($args);
$product_tags = get_the_terms($post, 'product_tag');
while ($loop->have_posts()):
$loop->the_post();
if ($product_tags){
foreach ($product_tags as $tag){
echo $tag->name;
}
}
endwhile;
wp_reset_query(); ?>
但是上面没有任何回应?
我相信 get_the_tags()
仅用于 post 标签,而不是 WooCommerce 的产品标签。对于 WooCommerce,分类应为 product_tag
。您可以使用
<?php
$product_tags = get_the_terms( $post, 'product_tag' );
?>
获取标签,然后根据需要循环处理它们。
完整代码示例:
<?php
global $post;
$args = array(
'post_type' => 'product',
'p' => $product_name,
'posts_per_page' => 1
);
$loop = new WP_Query($args);
while ($loop->have_posts()):
$loop->the_post();
$product_tags = get_the_terms($post, 'product_tag');
if ($product_tags && !is_wp_error($product_tags){
foreach ($product_tags as $tag){
echo $tag->name;
}
}
endwhile;
wp_reset_query(); ?>
我正在使用 WooCommerce
并用标签“electronic”标记了 products
之一(自定义 post 类型是 product
)。
我现在正在尝试遍历并获取分配给 post 的标签,但目前,在尝试转储数据时,我返回 bool(false)
。
这是我的方法:
<?php
$args = array(
'post_type' => 'product',
'p' => $product_name,
'posts_per_page' => 1
);
$loop = new WP_Query($args);
while ($loop->have_posts()):
$loop->the_post();
$posttags = get_the_tags();
if ($posttags)
{
foreach ($posttags as $tag)
{
echo $tag->name;
}
}
var_dump($posttags);
endwhile;
wp_reset_query();
?>
<?php
$args = array(
'post_type' => 'product',
'p' => $product_name,
'posts_per_page' => 1
);
$loop = new WP_Query($args);
while ($loop->have_posts()):
$loop->the_post();
$posttags = get_the_tags();
if ($posttags)
{
foreach ($posttags as $tag)
{
echo $tag->name;
}
}
var_dump($posttags);
endwhile;
wp_reset_query();
?>
尝试过:
<?php
global $post;
$args = array(
'post_type' => 'product',
'p' => $product_name,
'posts_per_page' => 1
);
$loop = new WP_Query($args);
$product_tags = get_the_terms($post, 'product_tag');
while ($loop->have_posts()):
$loop->the_post();
if ($product_tags){
foreach ($product_tags as $tag){
echo $tag->name;
}
}
endwhile;
wp_reset_query(); ?>
但是上面没有任何回应?
我相信 get_the_tags()
仅用于 post 标签,而不是 WooCommerce 的产品标签。对于 WooCommerce,分类应为 product_tag
。您可以使用
<?php
$product_tags = get_the_terms( $post, 'product_tag' );
?>
获取标签,然后根据需要循环处理它们。
完整代码示例:
<?php
global $post;
$args = array(
'post_type' => 'product',
'p' => $product_name,
'posts_per_page' => 1
);
$loop = new WP_Query($args);
while ($loop->have_posts()):
$loop->the_post();
$product_tags = get_the_terms($post, 'product_tag');
if ($product_tags && !is_wp_error($product_tags){
foreach ($product_tags as $tag){
echo $tag->name;
}
}
endwhile;
wp_reset_query(); ?>