简体   繁体   中英

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

I'm trying to fix an issue with a custom WordPress theme that is rendering image captions inline on posts. My WP knowledge is spotty, so, I'm not quite sure where I should be looking.

Here's the code that's showing posts:

<?


$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();

?>

Here's a screenshot showing what's on the front-end...

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

Shyamin's Answer is a potential solution, but I would like to explain more in depth about what is really going on.

In the docs for the the_content() function you can see Wordpress is actually using get_the_content() then using a handy function called apply_filters( 'the_content', $content) Docs here .

This function is the reason the_content() renders your editor content correctly as compared to get_the_content() . It is applying all of the filters added for the_content hook. Long story short, one of the filters added to that hook is do_shortcode() which renders the shortcodes.

Here is a neat blog post I found talking a little more about this as well as, how to create your own apply_filters hook and adding your own filters to mimic what the_content() does to the raw content.

So your 2 solutions: (Given that you have no other issues preventing the shortcode from rendering)

  1. If your code can handle it, simply use the_content() instead of echo get_the_content()
  2. If you require more advanced control of the content editor data, then store your '<p>' .get_the_content().'</p>'; in a variable like $content_data for example. and then in your echo statement use: echo apply_filters('the_content', $content_data);

This should solve your issues!

I had the same problem with my custom theme. Dunno why is this happening but I found a way to make it work. So putting this comment to inform others :)

just try load post content using the_content() function if you are using get_the_content() :) the_content() will render the correct html tags for the caption short codes in WordPress. Dunno why is it happening :/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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