微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何修复警告:使用未定义的常量 active - 假定为“active”这将在未来版本的 PHP 中引发错误

如何解决如何修复警告:使用未定义的常量 active - 假定为“active”这将在未来版本的 PHP 中引发错误

我目前正在使用 Bootstrap v5.0.2 和 PHP 7.3.28 进行 WirdPress 主题项目。 我正在尝试在自定义小部件中插入 Bootstrap Carousel 组件,但是当我调试代码时,我收到此“警告:使用未定义的常量 active - 假定为“active”(这将在未来版本的PHP) in ... on line 70"

这是给我错误的行:

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',)
    );
  }

  /**
  * 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;
}

解决方法

您必须回显“活动”和行尾:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。