简体   繁体   English

从自定义帖子类型中获取类别

[英]Fetch category from custom post type

I bought a Wordpress Theme hoping that after some code customisation it should achieve what I want.我买了一个 Wordpress 主题,希望经过一些代码定制后它应该能达到我想要的效果。

Now, I have (what I believe it is) a custom post type called 'Portofolio'.现在,我有(我认为是)一个名为“Portofolio”的自定义帖子类型。 As you can see in the picture below, it has the portofolio entries (at all portofolio) and categories for the aforementioned portofolio entries.如下图所示,它具有投资组合条目(在所有投资组合中)和上述投资组合条目的类别。

在此处输入图片说明

What I am trying to achieve is listing on a custom template page all the categories of the portofolio.我想要实现的是在自定义模板页面上列出投资组合的所有类别。 So far I have this code but all it does is to fetch the entries of the portofolio not the categories.到目前为止,我有这段代码,但它所做的只是获取投资组合的条目而不是类别。

    <?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().
?>

As you can see in the code, as the first line, I tried to fetch from the custom post type, but I had the same result.正如您在代码中看到的,作为第一行,我尝试从自定义帖子类型中获取数据,但得到了相同的结果。

I figured out the name/slug of the post type/taxonomy by checking the link in the admin panel while adding a category (check the picture below).我通过在添加类别的同时检查管理面板中的链接来找出帖子类型/分类法的名称/slug(检查下图)。 在此处输入图片说明

I haven't looked too much into the code, but I can see from the start that this line is not right.我没有过多地研究代码,但从一开始我就可以看出这一行是不对的。

$term_ids = get_terms( 'tm_portfolio_category', ['fields' => 'ids'] );

it should be "id"应该是“id”

eg例如

$term_ids = get_terms( 'tm_portfolio_category', ['fields' => 'id'] );

EDIT编辑

Sorry my bad,对不起这是我的错,

You can try this approach instead你可以试试这个方法

$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();

I would not recommend using the native WordPress Loop in this case for the sake of flexibility.在这种情况下,为了灵活性,我不建议使用本机 WordPress Loop。

I have tested this on my end and it seems to be working.我已经对此进行了测试,它似乎有效。 You may need to relook at what is returned when you use get_terms as the array that is returned might be indexed in a different way to how the query arguments are recieved.您可能需要重新查看使用 get_terms 时返回的内容,因为返回的数组的索引方式可能与接收查询参数的方式不同。

EDIT编辑

Sorry I feel like I keep missing the initial question.对不起,我觉得我一直错过最初的问题。

$terms = get_terms( 'tm_portfolio_category' );

is going to give you a list of terms.会给你一个术语列表。

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 
}

?>

Below that should give you the desired result without having to create another query.下面应该会给你想要的结果,而不必创建另一个查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM