从自定义 post 类型中获取类别
Fetch category from custom post type
我买了一个 Wordpress 主题,希望经过一些代码定制后它能达到我想要的效果。
现在,我有(我相信它是)一个名为 'Portofolio' 的自定义 post 类型。正如您在下图中看到的,它具有投资组合条目(在所有投资组合中)和上述投资组合条目的类别。
我想要实现的是在自定义模板页面上列出投资组合的所有类别。到目前为止,我有这段代码,但它所做的只是获取投资组合的条目而不是类别。
<?php
//$args = array('post_type' => 'tm_portfolio');
$term_ids = get_terms( 'tm_portfolio_category', ['fields' => 'ids'] );
$args = [
'tax_query' => [
[
'taxonomy' => 'tm_portfolio_category',
'terms' => $term_ids
]
]
];
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo 'List of categories';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
正如您在代码中看到的,作为第一行,我尝试从自定义 post 类型中获取,但结果相同。
我在添加类别时通过检查管理面板中的 link 找出了 post type/taxonomy 的 name/slug(查看下图)。
我没有仔细研究代码,但从一开始我就看出这一行不对。
$term_ids = get_terms( 'tm_portfolio_category', ['fields' => 'ids'] );
应该是"id"
例如
$term_ids = get_terms( 'tm_portfolio_category', ['fields' => 'id'] );
编辑
对不起我的错,
您可以试试这个方法
$term_ids = get_terms( 'tm_portfolio_category', ['fields' => 'ids'] );
$posts = query_posts( array(
'post_type' => 'tm_portfolio',
'tax_query' => array(
array(
'taxonomy' => 'tm_portfolio_category',
'terms' => $term_ids,
)
)
));
foreach ($posts as $post) {
echo 'List of categories';
?>
<p><a href="<?php echo get_permalink($post->ID); ?>" title="Permanent Link to <?php echo the_title_attribute(array('post'=>$post->ID)); ?>">
<?php echo get_the_title($post->ID); ?>
</a></p>
<?php
}
wp_reset_query();
为了灵活性,我不建议在这种情况下使用本机 WordPress 循环。
我已经对此进行了测试,似乎可以正常工作。您可能需要重新查看使用 get_terms 时返回的内容,因为返回的数组的索引方式可能与接收查询参数的方式不同。
编辑
抱歉,我一直想念最初的问题。
$terms = get_terms( 'tm_portfolio_category' );
将为您提供一个术语列表。
foreach ($terms as $term) {
?>
List of categories
<p>
<a href="<?php echo $term->slug; ?>" title="Permanent Link to <?php echo $term->name ?>" ><?php echo $term->name ?></a>
</p>
<?php
}
?>
低于该值应该会为您提供所需的结果,而无需创建另一个查询。
我买了一个 Wordpress 主题,希望经过一些代码定制后它能达到我想要的效果。
现在,我有(我相信它是)一个名为 'Portofolio' 的自定义 post 类型。正如您在下图中看到的,它具有投资组合条目(在所有投资组合中)和上述投资组合条目的类别。
我想要实现的是在自定义模板页面上列出投资组合的所有类别。到目前为止,我有这段代码,但它所做的只是获取投资组合的条目而不是类别。
<?php
//$args = array('post_type' => 'tm_portfolio');
$term_ids = get_terms( 'tm_portfolio_category', ['fields' => 'ids'] );
$args = [
'tax_query' => [
[
'taxonomy' => 'tm_portfolio_category',
'terms' => $term_ids
]
]
];
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo 'List of categories';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
正如您在代码中看到的,作为第一行,我尝试从自定义 post 类型中获取,但结果相同。
我在添加类别时通过检查管理面板中的 link 找出了 post type/taxonomy 的 name/slug(查看下图)。
我没有仔细研究代码,但从一开始我就看出这一行不对。
$term_ids = get_terms( 'tm_portfolio_category', ['fields' => 'ids'] );
应该是"id"
例如
$term_ids = get_terms( 'tm_portfolio_category', ['fields' => 'id'] );
编辑
对不起我的错,
您可以试试这个方法
$term_ids = get_terms( 'tm_portfolio_category', ['fields' => 'ids'] );
$posts = query_posts( array(
'post_type' => 'tm_portfolio',
'tax_query' => array(
array(
'taxonomy' => 'tm_portfolio_category',
'terms' => $term_ids,
)
)
));
foreach ($posts as $post) {
echo 'List of categories';
?>
<p><a href="<?php echo get_permalink($post->ID); ?>" title="Permanent Link to <?php echo the_title_attribute(array('post'=>$post->ID)); ?>">
<?php echo get_the_title($post->ID); ?>
</a></p>
<?php
}
wp_reset_query();
为了灵活性,我不建议在这种情况下使用本机 WordPress 循环。
我已经对此进行了测试,似乎可以正常工作。您可能需要重新查看使用 get_terms 时返回的内容,因为返回的数组的索引方式可能与接收查询参数的方式不同。
编辑
抱歉,我一直想念最初的问题。
$terms = get_terms( 'tm_portfolio_category' );
将为您提供一个术语列表。
foreach ($terms as $term) {
?>
List of categories
<p>
<a href="<?php echo $term->slug; ?>" title="Permanent Link to <?php echo $term->name ?>" ><?php echo $term->name ?></a>
</p>
<?php
}
?>
低于该值应该会为您提供所需的结果,而无需创建另一个查询。