Php 将类添加到当前Wordpress帖子标题

Php 将类添加到当前Wordpress帖子标题,php,wordpress,custom-post-type,Php,Wordpress,Custom Post Type,我正在一个服务cpt上工作,希望在同一cpt内的单个职位上显示其他职位的列表 我使用的代码是: <?php $loop = new WP_Query( array( 'post_type' => 'services', 'posts_per_page' => 6 )); ?> <ul> <?php while ( $loop->have_posts() ) : $loop->the

我正在一个服务cpt上工作,希望在同一cpt内的单个职位上显示其他职位的列表

我使用的代码是:

<?php 
    $loop = new WP_Query( array( 
        'post_type' => 'services', 
        'posts_per_page' => 6
    ));
?>
<ul>
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
        <li>
            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                <h4><?php the_title(); ?></h4>
            </a>
        </li>
    <?php endwhile; wp_reset_query(); ?>
</ul>


现在我的问题是,有没有办法在当前帖子中添加一个类?其目的是使其样式与列表中的其他帖子不同。

是,首先获取当前帖子id,并将该id与循环id匹配,如果该id与只添加类相同,则只需使用下面的代码即可

<?php 
$current_post_id = get_the_ID();
$loop = new WP_Query( array( 
    'post_type' => 'services',
    'posts_per_page' => 6, ));
?>
<ul>
   <?php 
   while ( $loop->have_posts() ) : $loop->the_post();
       $class = ($current_post_id == $loop->ID) ? "current-post" : '';
   ?>
   <li class="<?php echo $class; ?>">
     <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
         <h4><?php the_title(); ?></h4>
     </a>
   </li>
   <?php endwhile; wp_reset_query(); ?>
</ul>


    这非常简单,您可以始终在之前和之后添加类

    阅读本文档

    使用函数

    例如:

    
    

    我只希望它显示在当前帖子上,而不是所有帖子上。
    <style>
    li{
    /*all posts*/
    }
    li.current-post{
    /*specific for the current post*/
    }
    </style>
    
    <?php the_title( '<div class="wrapper">', '</div>' ); ?>
    
    <?php 
      $loop = new WP_Query( array( 
        'post_type' => 'services', 
        'posts_per_page' => 6
      ));
    ?>
    <ul>
      <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
        <li <?php post_class(); ?>>
          <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
            <h4><?php the_title(); ?></h4>
          </a>
        </li>
      <?php endwhile; wp_reset_query(); ?>
    </ul>