0

How can i only show categories if post type is "post_three"

  <aside class="sidebar">
                 <div class="widget widget-filter">
                     <button class="widget-title">Explore Topics</button>
                     <svg class="separator" viewBox="0 0 238 11" preserveAspectRatio="none" xmlns="http://www.w3.org/2000/svg">
                <line opacity="0.3" y1="0.5" x2="101.942" y2="0.5" stroke="#3CD5AF"></line>
                <line opacity="0.3" y1="10.5" x2="237.5" y2="10.5" stroke="#3CD5AF"></line>
              </svg>
            <?php 
             $categories = get_categories(array(
    'type' => 'post_three',
    'hide_empty' => true,
   ) ); ?>
            <ul class="filter-list">
                <?php foreach ( $categories as $category ) :
                
                // Skip "Uncategorized" category
             if ( ! empty( $categories ) ) :   ?>    
                   <li  :class=" category == <?php echo $category->term_id; ?> ? '' : ''">
            <a @click="filterPosts(<?= $category->term_id; ?>)"><?= esc_html( $category->name ); ?></a>
        </li> <?php
      endif;

              endforeach; ?>
                
            </ul>
            </div>
</aside>
1
  • Please edit your question to clarify it. Are you asking how to get the categories for a custom post type post_three? Also, note that type isn't a valid argument to get_categories() -- you can see the valid arguments here.
    – Pat J
    Feb 7 at 20:45

2 Answers 2

1

you can modify your get_categories function call to include the tax_query parameter to specify the taxonomy and terms you want to retrieve.

I have added the taxonomy parameter to specify that we are querying categories from the category taxonomy.

The object_type parameter restricts the query to only categories associated with the post_three post type

<aside class="sidebar">
    <div class="widget widget-filter">
        <button class="widget-title">Explore Topics</button>
        <svg class="separator" viewBox="0 0 238 11" preserveAspectRatio="none" xmlns="http://www.w3.org/2000/svg">
            <line opacity="0.3" y1="0.5" x2="101.942" y2="0.5" stroke="#3CD5AF"></line>
            <line opacity="0.3" y1="10.5" x2="237.5" y2="10.5" stroke="#3CD5AF"></line>
        </svg>
        <?php 
        // Retrieve categories associated with the 'post_three' post type
        $categories = get_categories(array(
            'taxonomy' => 'category', // Specify the taxonomy
            'object_type' => array('post_three'), // Specify the post type
            'hide_empty' => true,
        )); ?>
        <ul class="filter-list">
            <?php foreach ($categories as $category) : ?>
                <li>
                    <a href="<?= get_category_link($category->term_id); ?>"><?= esc_html($category->name); ?></a>
                </li>
            <?php endforeach; ?>
        </ul>
    </div>
</aside>
1
  • thank you very much
    – Beep
    Feb 8 at 12:12
1

Try this

<?php
    // Get the current post ID
    $current_post_id = get_the_ID();
    
    // Get the post type of the current post
    $current_post_type = get_post_type($current_post_id);
    
    // Check if the current post type is "post_three"
    if ($current_post_type === 'post_three') {
          foreach ( $categories as $category ) :
          #your other code here
    
    } 
    ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.