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
|
2 Answers
Reset to default 1you 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
版权声明:本文标题:categories - Show category only if post type is post_three 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736659312a1946336.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
post_three
? Also, note thattype
isn't a valid argument toget_categories()
-- you can see the valid arguments here. – Pat J Commented Feb 7, 2024 at 20:45