phpwordpresstaxonomycustom-taxonomy

How Display All Posts from the "Services" Taxonomy on the Service page?


I'm currently building my first wordpress theme and I want to display a custom post type's taxonomy (but only the name, not the link) on its preview as well as the date it was published. I've been looking around but only found that I should use the get_terms() functions but than displays all taxonomies, and I want to display the selected category for my post. Please keep in mind that I'm only a beginner at this so you'll have to be pretty clear :)

Here is Service Taxonomy


function create_services_taxonomy() {
    // Services Categories
    register_taxonomy('service_category', 'services', array(
        'labels' => array(
            'name' => 'Service Categories',
            'singular_name' => 'Service Category',
            'search_items' => 'Search Service Categories',
            'all_items' => 'All Service Categories',
            'edit_item' => 'Edit Service Category',
            'update_item' => 'Update Service Category',
            'add_new_item' => 'Add New Service Category',
            'new_item_name' => 'New Service Category Name',
            'menu_name' => 'Service Categories',
        ),
        'hierarchical' => true, // Acts like categories
        'show_admin_column' => true,
        'show_in_rest' => true, // For Gutenberg support
        'rewrite' => array('slug' => 'service-category'),
    ));

    // Services Tags
    register_taxonomy('service_tag', 'services', array(
        'labels' => array(
            'name' => 'Service Tags',
            'singular_name' => 'Service Tag',
            'search_items' => 'Search Service Tags',
            'all_items' => 'All Service Tags',
            'edit_item' => 'Edit Service Tag',
            'update_item' => 'Update Service Tag',
            'add_new_item' => 'Add New Service Tag',
            'new_item_name' => 'New Service Tag Name',
            'menu_name' => 'Service Tags',
        ),
        'hierarchical' => false, // Acts like tags
        'show_admin_column' => true,
        'show_in_rest' => true, // For Gutenberg support
        'rewrite' => array('slug' => 'service-tag'),
    ));
}
add_action('init', 'create_services_taxonomy');

function create_services_post_type() {
    register_post_type('services', array(
        'labels' => array(
            'name' => 'Services',
            'singular_name' => 'Service',
            'add_new' => 'Add New',
            'add_new_item' => 'Add New Service',
            'edit_item' => 'Edit Service',
            'new_item' => 'New Service',
            'view_item' => 'View Service',
            'search_items' => 'Search Services',
            'not_found' => 'No services found',
            'not_found_in_trash' => 'No services found in Trash',
        ),
        'public' => true,
        'has_archive' => true,
        'supports' => array('title', 'editor', 'thumbnail', 'custom-fields'), // You can add more supported features if needed
        'show_in_rest' => true, // Important for Gutenberg support
        'rewrite' => array('slug' => 'services'),
    ));
}
add_action('init', 'create_services_post_type');
<?php
// Get all terms in the 'services' taxonomy
$terms = get_terms(array(
    'taxonomy' => 'services',
    'hide_empty' => false, // Set to true if you only want terms with posts
));

if (!empty($terms) && !is_wp_error($terms)) {
    foreach ($terms as $term) {
        echo '<h2>' . esc_html($term->name) . '</h2>';
        echo '<p>' . esc_html(term_description($term)) . '</p>';

        // Query posts for each term
        $term_posts = new WP_Query(array(
            'post_type' => 'services', // Replace with your custom post type
            'tax_query' => array(
                array(
                    'taxonomy' => 'services',
                    'field' => 'term_id',
                    'terms' => $term->term_id,
                ),
            ),
        ));

        if ($term_posts->have_posts()) {
            echo '<ul>';
            while ($term_posts->have_posts()) {
                $term_posts->the_post();
                echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
            }
            echo '</ul>';
            wp_reset_postdata();
        } else {
            echo 'No posts found in this service.';
        }
    }
} else {
    echo 'No services found.';
}
?>

Solution

  • The get_the_terms() function returns a list of taxonomy terms assigned to a Post. The second parameter allows you to limit which taxonomy for which terms are returned (untested):

    $terms = get_the_terms( get_the_ID(), 'service_category' );
    
    if ( ! is_array( $terms ) ) {
        $terms = array();
    }
    
    $term = array_pop( $terms );
    
    if ( ! empty( $term ) ) {
        echo esc_html( $term->name );
    }