admin管理员组

文章数量:1125333

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=";>
                <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>

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>
Share Improve this question asked Feb 7, 2024 at 16:31 BeepBeep 297 bronze badges 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 Commented Feb 7, 2024 at 20:45
Add a comment  | 

2 Answers 2

Reset to default 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>

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
    
    } 
    ?>

本文标签: categoriesShow category only if post type is postthree