I bought a WordPress Theme hoping that after some code customisation it should achieve what I want.
Now, I have (what I believe it is) a custom post type called ‘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).
I haven’t looked too much into the code, but I can see from the start that this line is not right.
it should be “id”
e.g.
EDIT
Sorry my bad,
You can try this approach instead
I would not recommend using the native WordPress Loop in this case for the sake of flexibility.
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.
EDIT
Sorry I feel like I keep missing the initial question.
is going to give you a list of terms.
Below that should give you the desired result without having to create another query.