Why doesn't apply_filter ('the_content') output anything?

I tried so many php combinations to get wordpress to output $ post-> post_content as formatted text (as opposed to the raw formatting that echo $post->post_content gives me. This combination seems to be the most promising, but it doesn't print anything. Any ideas ?

(this line: <?php $content = apply_filters('the_content', $s->post_content); ?> )

 <?php query_posts('orderby=menu_order&order=asc&posts_per_page=-1&post_type=page&post_parent='.$post->ID); if(have_posts()) { while(have_posts()) { the_post(); ?> <div class="page"> <?php global $wpdb; $subs = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent='$post->ID' AND post_type='page' AND post_status='publish'"); if($subs) { ?> <div class="navi"></div> <a class="naviNext"><img src="<?php bloginfo('template_url'); ?>/images/navi-next.png" alt="" /></a> <div class="scrollable"> <div class="items"> <?php foreach($subs as $s) { ?> <div class="item"> <h2><?php echo $s->post_title; ?></h2> <?php $content = apply_filters('the_content', $s->post_content); echo $content; ?> </div> <?php } ?> </div> </div> <?php } else { the_content(); } ?> </div> <?php } } wp_reset_query(); ?> 
+4
source share
7 answers

As far as I know, a function that applies the basic “formatting” to the content body is wpautop () . This function must be connected to the_content wordpress. The function does cause annoying things (e.g. messing up the embed code), although there are many plugins that would untie it from the filter stack. Try replacing the line:

 <?php $content = apply_filters('the_content', $s->post_content); echo $content; ?> 

from

 <?php $content = wpautop($s->post_content); echo $content; ?> 

If this helps, you probably have a problem with wpautop not being unhooked somewhere.

+9
source

I had the same problem. It turned out that in my topic there is a function that also filtered the content , but had an error in it, causing the filter to return an empty string.

So, check your theme and plugins for functions that filter the_content . In Sublime Text 2, you can quickly find “find in files” with ⌘ / CTRL + + F to find possible culprits.

+2
source

man86,

I see that you are receiving message data through $ wpdb-> get_results (). The point is that the data is returned to its original state, so you need to “prepare it” before you can use general message functions such as the_content () (which will return already formatted content as you would like it to be).

How to try this (see code comments):

 <?php query_posts('orderby=menu_order&order=asc&posts_per_page=-1&post_type=page&post_parent='.$post->ID); if(have_posts()) { while(have_posts()) { the_post(); ?> <div class="page"> <?php global $wpdb; $subs = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent='$post->ID' AND post_type='page' AND post_status='publish'"); if($subs) { ?> <div class="navi"></div> <a class="naviNext"><img src="<?php bloginfo('template_url'); ?>/images/navi-next.png" alt="" /></a> <div class="scrollable"> <div class="items"> <?php foreach($subs as $post) { // <-- changed $s to $post setup_postdata($post) // <--use setup_postdata to prepare post ?> <div class="item"> <h2><?php the_title(); // <-- use "the_title() now that the data has been prepared ?></h2> <?php the_content(); // <-- use "the_content() now that the data has been prepared ?> </div> <?php } ?> </div> </div> <?php } else { the_content(); } ?> </div> <?php } } wp_reset_query(); ?> 

Link: http://codex.wordpress.org/Class_Reference/wpdb#Examples_5 ("Get all information about drafts by user 5")

Thanks, I hope this helps!

Beats

+1
source

You need to echo results of calling apply_filters :

 <?php echo apply_filters('the_content', $s->post_content); ?> 

Or, as you encoded it:

 <?php $content = apply_filters('the_content', $s->post_content); echo $content; ?> 
0
source

Sorry if this is too easy, but it might help if you echoed the contents:

 <?php $content = apply_filters('the_content', $s->post_content); echo $content; ?> 
0
source

How do you add a filter? You can use add_filter, which defines a function that will receive $ content. You can perform any filtering you need with this function.

http://codex.wordpress.org/Plugin_API#Create_a_Filter_Function

0
source

hmm .. for some reason I can get the content to display when I deleted the top row and bottom row.

perhaps the problem is calling query_posts .. rather than calling apply_filters ().

I can switch the display mode depending on whether I use apply_filters () or not. which, I believe, is what you are after.

0
source

All Articles