首页 > 解决方案 > WordPress v5.0.3:无法检查图库短代码

问题描述

我对 WordPress 开发比较陌生。最近,我安装了 WordPress 5.0.3,从那时起就遇到了一个奇怪的问题。我要做的就是检查帖子是否包含使用has_shortcode函数的画廊并将画廊作为标签传递给它。

这是代码的样子,代码是自定义page.php文件的摘录。

<div class="container">
<?php
    $wpb_all_query = new WP_Query(array('post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => -1));?>

  <?php if ($wpb_all_query->have_posts()): ?>

  <?php while ($wpb_all_query->have_posts()): $wpb_all_query->the_post();?>

    <div class="row">
      <div class="col-12">
        <div class="d-flex justify-content-center">
          <?php esc_html(the_title());?>:&nbsp;<?php
if (has_shortcode(the_content(), 'gallery')):
    echo "TRUE";
else:
    echo "FALSE";
endif;?>
        </div>
      </div>

    </div>

    <?php endwhile;?>

<?php wp_reset_postdata();?>

<?php endif;?>

</div>

has_shortcode即使帖子中嵌入了画廊,该函数也始终返回 false。

不确定,这种检查帖子是否包含画廊的方式在当前版本中是否仍然有效。

我知道代码的格式很差,但由于时间限制,我不想在这里花时间格式化它。

标签: phpwordpresswordpress-theming

解决方案


推荐阅读