如何修复警告:使用未定义的常量处于活动状态 - 假设 'active'(这将在 PHP 的未来版本中引发错误)

How to fix Warning: Use of undefined constant active - assumed 'active' (this will throw an Error in a future version of PHP)

我目前正在使用 Bootstrap v5.0.2 和 PHP 7.3.28 开发 WirdPress 主题项目。 我正在尝试在自定义小部件中插入 Bootstrap 旋转木马组件,但是当我调试代码时,我收到此消息“警告:使用未定义的常量活动 - 假定 'active'(这将在第 70 行 "

中的 PHP) 的未来版本中抛出错误

这是给我错误的行:

echo ' class="';if ( $the_query->current_post == 0 ) : active ;endif; '"> ';

我尝试在 $active'active' 中更改 active 但是它不起作用。

因为我是 PHP 的新手,所以我不明白我做错了什么。 我希望有人能帮我解决这个问题。 谢谢

这是完整的代码:

    <?php
// Register and load the widget
function tabulas_slider_widget() {
  register_widget( 'Tabulas_Slider_Widget' );
}
add_action( 'widgets_init', 'tabulas_slider_widget' );

// Creating the widget
class Tabulas_Slider_Widget extends WP_Widget {

  /**
  * Register widget with WordPress.
  */
  public function __construct() {
    parent::__construct(
      // Base ID of widget
      'Tabulas_Slider_Widget',

      // Widget name will appear in UI
      esc_html__('Last Post Slider', 'tabulas'),

      // Widget description
      array( 'description' => esc_html__( 'Display the 3 last post published with featured image in the form of a Slider', 'tabulas'), )
    );
  }

  /**
  * Front-end display of widget.
  *
  * @see WP_Widget::widget()
  *
  * @param array $args     Widget arguments.
  * @param array $instance Saved values from database.
  */
  public function widget( $args, $instance ) {

    // WP_Query arguments
    $args = array(
      'post_type' => 'post',
      'posts_per_page' => 3,

    );

    // The Query
    $the_query = new WP_Query ( $args );
    echo ' <div id="carouselExampleCaptions" ';
    echo ' class="carousel slide" ';
    echo ' data-bs-ride="carousel" ';
    echo ' data-bs-interval="10000"> ';
    echo ' <div class="carousel-indicators"> ';
    if ( $the_query->have_posts() ) :
      echo $args['before_widget'];
      if ( $title ){
        echo $args['before_title'] . $title . $args['after_title'];
      }

      echo ' <ol class="carousel-indicators"> ';
      while ( $the_query->have_posts() ) : $the_query->the_post();
      echo ' <li data-target="#ExampleCarouselID" ';
      echo ' data-slide-to="';$the_query->current_post;'" ';
      echo ' class="';if ( $the_query->current_post == 0 ) : active ;endif; '"> ';
      echo ' </li> ';
    endwhile;
    echo ' </ol> ';

    rewind_posts();
    echo ' <button type="button" ';
    echo ' data-bs-target="#carouselExampleCaptions" ';
    echo ' data-bs-slide-to="0" ';
    echo ' class="active" ';
    echo ' aria-current="true" ';
    echo ' aria-label="Slide 1">';
    echo ' </button> ';
    echo '<button type="button" ';
    echo ' data-bs-target="#carouselExampleCaptions" ';
    echo ' data-bs-slide-to="1" ';
    echo ' aria-label="Slide 2">';
    echo ' </button> ';
    echo '<button type="button" ';
    echo ' data-bs-target="#carouselExampleCaptions" ';
    echo ' data-bs-slide-to="2" ';
    echo ' aria-label="Slide 3">';
    echo '</button>';
    echo '</div>';


    echo ' <div class="carousel-inner"> ';
    while ( $the_query->have_posts() ) : $the_query->the_post();
    $thumbnail_id   = get_post_thumbnail_id();
    $thumbnail_url  = wp_get_attachment_image_src( $thumbnail_id, 'full', true );
    $thumbnail_meta = get_post_meta( $thumbnail_id, '_wp_attatchment_image_alt', true );

    echo ' <div class="carousel-item ';
    if ( $the_query->current_post == 0 ) : active ;endif; '"> ';


    if ( has_post_thumbnail() ) {
      echo '<a href="';the_permalink(); echo '">';
      the_post_thumbnail('full');
      echo '</a>';
    }
    echo '<div class="carousel-caption d-none d-md-block">';
    echo '<h5>';
    the_title();
    echo '</h5>';
    echo '</div>';
    echo '</div>';
  endwhile;
  wp_reset_query();
  echo '</div>';

  echo '<button class="carousel-control-prev" type="button" data-bs-target="#carouselExampleCaptions" data-bs-slide="prev">';
  echo '<span class="carousel-control-prev-icon" aria-hidden="true">';
  echo '</span>';
  echo' <span class="visually-hidden">';esc_html_e( 'Previous', 'tabulas' );
  echo '</span>';
  echo '</button>';
  echo '<button class="carousel-control-next" type="button" data-bs-target="#carouselExampleCaptions" data-bs-slide="next">';
  echo '<span class="carousel-control-next-icon" aria-hidden="true">';
  echo '</span>';
  echo '<span class="visually-hidden">';esc_html_e( 'Next', 'tabulas' );
  echo '</span>';
  echo '</button>';
  echo '</div>';
  echo $args['after_widget'];

  wp_reset_postdata();
endif;
}

您必须回显 'active' 和行尾:

echo ' class="';if ( $the_query->current_post == 0 ) : echo 'active' ;endif; echo '"> ';