我不知道如何获取自定义分类法的ID,我正在使用它循环遍历名为“test\\u values”的自定义post类型。
function prefix_load_term_posts () {
$term_slug = $_POST[ \'term\' ];
$args = array (
\'post_type\' => \'test_values\',
\'posts_per_page\' => 1,
\'tax_query\' => array(
array(
\'taxonomy\' => \'test_value_category\',
\'field\' => \'slug\',
\'terms\' => $term_slug ,
),
),
);
global $post;
$myposts = get_posts( $args );
ob_start ();
foreach( $myposts as $post ) : setup_postdata($post); ?>
<?php endforeach; ?>
有人对如何在循环中获取这个分类ID有什么建议吗?
最合适的回答,由SO网友:Shefali 整理而成
您可以尝试此功能get_term_by($field, $value, $taxonomy, $output, $filter )
或
$termID = [];
$terms = get_the_terms($post->ID, \'taxonomy\');
foreach ($terms as $term) {
$termID[] = $term->term_id;
}
或
get_queried_object_id()