Reputation: 175
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...
Upvotes: 0
Views: 2593
Reputation: 445
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)
the_content()
instead of echo get_the_content()
'<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!
Upvotes: 4
Reputation: 102
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 :/
Upvotes: 0