1

I tried to filter only the text in get_the_content for my home page, but its including my shortcode also, because in that page first row google map 2nd row text box i am using visual composer page builder.

My code

$content = get_the_content();
$content = apply_filters('the_content', substr(get_the_content(), 0, 60) );
$content = str_replace(']]>', ']]>', $content);
echo $content

My result

[vc_row][vc_column][vc_gmaps link=”#E-8_JTNDaWZyYW1lJTIwc3Jj

screenshot

enter image description here

  • What are you trying to do exactly with the filter? At the moment your code is only grabbing the first 60 characters of the post, which is resulting in what looks like a broken shortcode. You'd probably be better off just replacing all of your code with the_content() – Tim Malone Apr 24 '16 at 7:30
  • I just want to skip the sortcodes from the content. i want to display the 2nd row text. – Mohamed Rihan Apr 24 '16 at 7:38
  • Ah got it. Just posted an answer for you :) – Tim Malone Apr 24 '16 at 7:56
1

Based on your comment clarifying what you want to achieve, it appears you want to display the first 60 characters of your post not including any shortcodes.

To do this you can use the strip_shortcodes() function. Rewritten with that, your code will look like this:

$content = strip_shortcodes(get_the_content());
$content = apply_filters('the_content', substr($content, 0, 60) );
$content = str_replace(']]>', ']]>', $content);
echo $content;

All we're doing here is running the content (from get_the_content()) through strip_shortcodes() before applying Wordpress filters on the first 60 characters.

Depending on what you want to achieve and whether you're relying on any other plugins modifying this content for you, you actually might be fine to skip the apply_filters() call as well - in which case all you'd need to do is set $content to the substr() you want to have.

  • not working anyway thanks, for now i will use get_the_excerpt(). – Mohamed Rihan Apr 24 '16 at 8:32
  • That's probably a better option if you're happy with it :) – Tim Malone Apr 24 '16 at 21:50
-2

You can use

do_shortcode(get_the_content($id)); 

instead

  • 2
    Please file an edit] and explain why. Please first read How to Answer to get tips on how to write a good answer – Pieter Goosen Jul 15 '16 at 7:26

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.