繁体   English   中英

Wordpress 图像标题短代码显示在帖子上(不呈现为标题)

[英]Wordpress image caption shortcodes showing on posts (not rendering as captions)

我正在尝试解决一个自定义 WordPress 主题的问题,该主题在帖子中内联呈现图像标题。 我的 WP 知识参差不齐,所以,我不太确定应该去哪里寻找。

这是显示帖子的代码:

<?


$args = array (
    'post_type'              => array( 'post' ),
    'meta_key' => 'event_date', // name of custom field
    'orderby' => 'meta_value_num',
    'order' => 'ASC'
);

// The Query
$blogQuery = new WP_Query( $args );

  while ( $blogQuery->have_posts() ) {
      $blogQuery->the_post();
      $date = DateTime::createFromFormat('Ymd', get_field('event_date'));
      $today = DateTime::createFromFormat('Ymd', date('Ymd'));

      if($date > $today){

          echo '<div class="blog-entry">';
          echo '<h3 class="wonk green-nav">Event: ';
          echo get_the_title().'</h3>';

            if(get_field('update_type') == 'event'){

                echo '<h4>';    
                echo $date->format('l, F jS, Y. ');
                the_field('event_time');
                echo '</h4>';

            }


          echo '<p>' .get_the_content().'</p>';
          echo '</div>';
          }

      }

 wp_reset_postdata();

?>

这是显示前端内容的屏幕截图...

在帖子文本中呈现的短代码的屏幕截图

Shyamin 的答案是一个潜在的解决方案,但我想更深入地解释真正发生的事情。

the_content()函数的文档中,您可以看到 Wordpress 实际上正在使用get_the_content()然后使用一个名为apply_filters( 'the_content', $content) Docs here的方便函数。

get_the_content()相比,此函数是the_content()正确呈现编辑器内容的原因。 它正在应用为the_content钩子添加的所有过滤器。 长话短说,添加到该钩子的过滤器之一是do_shortcode() ,它呈现短代码。

这是一篇简洁的博客文章,我发现更多地讨论了这个问题,以及如何创建自己的apply_filters钩子并添加自己的过滤器来模仿the_content()对原始内容的作用。

所以你的 2 个解决方案:(假设你没有其他问题阻止短代码呈现)

  1. 如果您的代码可以处理它,只需使用the_content()而不是echo get_the_content()
  2. 如果您需要对内容编辑器数据进行更高级的控制,请存储您的'<p>' .get_the_content().'</p>'; 例如在像$content_data这样的变量中。 然后在你的 echo 语句中使用: echo apply_filters('the_content', $content_data);

这应该可以解决您的问题!

我的自定义主题也有同样的问题。 不知道为什么会发生这种情况,但我找到了一种使它起作用的方法。 所以把这个评论告诉其他人:)

如果您使用的是 get_the_content() ,请尝试使用 the_content() 函数加载帖子内容:) the_content() 将为 WordPress 中的标题短代码呈现正确的 html 标签。 不知道为什么会这样:/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM