0

I am facing an issue when i use get_the_content() function. It displays the post content but the image in beginning of the content has some extra text around. My code is:

<div class="thecontent" itemprop="articleBody">
   <?php echo get_the_content(); ?>
</div>

Output:

enter image description here

I also tried

<div class="thecontent" itemprop="articleBody">
   <?php $content= get_the_content();  
         echo apply_filters('the_content', $content); 
   ?>
</div>

But in this case picture isn't showing at all.

Output:

enter image description here

  • The problem is that get_the_content doesn't render shortcode (which is what you're seeing). I would check out using: echo apply_filters( 'the_content', get_post_field('post_content', $postid) ); you can find more information here: stackoverflow.com/questions/22270147/… – disinfor Oct 25 '17 at 18:05
  • It's not working in my case. :( – Noman Ahmad Oct 25 '17 at 18:13
  • What's not working? – disinfor Oct 25 '17 at 18:25
  • did you try just "the_content()" instead of "echo get_the_content()"? Alternatively, if you don't want captions, why not remove them from the image? – rudtek Oct 25 '17 at 19:06
2

To make sure the shortcode executes, save the content to a variable, then run do_shortcode on the saved variable.

<div class="thecontent" itemprop="articleBody">
    <?php $content = get_the_content();
    echo do_shortcode($content); ?>
</div>

The difference is, echoing just literally echoes whatever has been grabbed. PHP's built-in functions don't have any special way to process shortcodes so they just output as they've been told. By using a WP-specific function, WordPress parses whatever the content is and displays it. This works even if you have no shortcodes in the content.

| improve this answer | |
  • Its not showing even all post. :( – Noman Ahmad Oct 25 '17 at 18:46
  • 2
    you'll need to echo the do_shortcode... "echo do_shortcode($content);" – rudtek Oct 25 '17 at 19:04
  • Corrected above. :) – WebElaine Oct 25 '17 at 20:54
0

What you had originally, echo get_the_content(); doesn't work because the do_shortcode callback isn't applied when calling get_the_content(), but it is when calling the_content(). That's why the caption shortcode isn't being converted into its corresponding HTML.

There is almost never a reason to echo get_the_content() because that's basically what the_content() does.

<div class="thecontent" itemprop="articleBody">
  <?php the_content(); ?>
</div>

If, for whatever reason, your use can necessitates using get_the_content() instead of the_content(), you need to make sure that the do_shortcode callback is applied before printing the HTML. So there are a couple ways to fix that. One (that should work) is by applying the the_content filter to the get_the_content() function.

//* In your index.php (or wherever template file)
printf( 
  '<div class="thecontent" itemprop="articleBody">%1$s</div>',
  apply_filters( 'the_content', $content )
);

This is basically what you tried in your question. I write it like this because it's clearer, to me, what this code does. To be clear, this should work. Since it's not, there is a filter hooked to the_content that's messing it up added by your theme or a plugin. Make sure to disable all plugins and disable any other part of your theme that filters the_content.

Another way to do this is to force the shortcodes to do their thing. This is what WebElaine says in her answer. I'd do it slightly differently, but again, the same concept.

//* Again, in a template file
printf(
  <div class="thecontent" itemprop="articleBody">%1$s</div>,
  do_shortcode( get_the_content() )
);

There's also a way to do this in your functions.php, or another file included from your functions.php, or even a plugin. What you can do is hook into the_content and add the wrapper there.

//* In your functions.php
add_filter( 'the_content', 'wpse_283971_the_content', 1000 );
function wpse_283971_the_content( $content ) {
  return sprintf( '<div class="thecontent" itemprop="articleBody">%1$s</div>', $content );
}

//* In your template file
the_content();

I'm guessing that you're trying to echo get_the_content() instead of just using the_content() is because the_content() wasn't working. You need to find the filter that's causing the problem with the_content. From the looks of it, one of the filters is forgetting to return its result.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.