Php 若元素ID等于post term,则向元素添加类

Php 若元素ID等于post term,则向元素添加类,php,wordpress,Php,Wordpress,我有一个元素使用术语slug作为ID <div id="<?php echo $current_term; ?>" class="collapse"> 据我所知,您的目标是将classshow添加到ID与当前帖子中的条款相同的所有元素中 如果是这样,我建议您获取当前帖子的条款(您正在阅读的这篇文章),并检查您的$current\u term是否符合当前帖子的条款 也许这会对您有所帮助:包含slug的帖子的url是什么?url遵循域/类别/帖子名的结构。这里没有slug,

我有一个元素使用术语slug作为ID

<div id="<?php echo $current_term; ?>" class="collapse">

据我所知,您的目标是将class
show
添加到ID与当前帖子中的条款相同的所有
元素中

如果是这样,我建议您获取当前帖子的条款(您正在阅读的这篇文章),并检查您的
$current\u term
是否符合当前帖子的条款


也许这会对您有所帮助:

包含slug的帖子的url是什么?url遵循域/类别/帖子名的结构。这里没有slug,你是否检查了
DOM
是否存在
current
类?更进一步,一旦你拥有了当前帖子的所有帖子术语,你就可以在数组($current\u term,$otherTerms)中执行类似
的操作了显示“:”
好的,这样就行了,这就是我所做的:
然后在应用类的地方:
如果能以更好的方式完成,我将非常感谢反馈,因为我对php非常陌生。很高兴听到这个消息!您不需要将每一行新代码放在
之间。举个例子:看一看:好吧,我很感激!复制了您的示例,并将在将来尝试清理代码。干杯
<div id="term1" class="collapse show">
<div id="term2" class="collapse">
<div id="term3" class="collapse">
<?php $current_id = $post->ID; ?>
<?php $args = array(
'orderby' => '',
'order' => 'asc'); ?>
<?php $terms = get_tags( $args ); ?>
<?php foreach ( $terms as $term ) { ?>
<?php $posts = get_posts( array( 
'category_name' => "category1", 
'posts_per_page' => -1,
'meta_key' => 'meta1',
'orderby' => 'meta_value',
'order' => 'ASC',
'tag__in' => $term->term_id )); ?> 
<?php $current_term = $term->slug; ?>        
<?php if($posts) : ?>    
<div class="sidebar-item">
    <a class="sidebar-link" data-toggle="collapse" aria-expanded="false" aria-controls="<?php echo $current_term; ?>"><?php echo $term->description; ?></a>
    <div id="<?php echo $current_term; ?>" class="collapse">
        <?php foreach($posts as $post) : ?>
        <?php setup_postdata($post); $count++; ?>         
        <?php $current_class = ( $current_id == $post->ID ) ? 'current' : ''; ?>
        <a class="<?php if ( $current_class ) echo $current_class; ?> text-nowrap overflow-hidden post-link" href="<?php echo get_permalink( $id ); ?>"><?php echo get_post_meta($post->ID, 'custom-field', true) ?></a>   
        <?php endforeach ?>
        <?php $count = 0; ?>
    </div>            
</div>         
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php } ?>
$('.current').parent().addClass('show');