开发者

Why isn't apply_filter('the_content') outputting anything?

开发者 https://www.devze.com 2023-01-13 13:42 出处:网络
I\'ve tried so many combinations of php 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 s

I've tried so many combinations of php 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 isn't outputting anything. Any ideas?

(it's 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">&开发者_运维知识库lt;/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(); ?>


As far as I know, the function that applies the main 'formatting' to the content body is wpautop(). That function should be hooked into 'the_content' by wordpress. The function does do annoying things (like mess up embed code) though and there are a lot of plugins that will unhook it from the filter stack. Try replacing your line:

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

with

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

If that helps then you probably have an issue of the wpautop getting unhooked somewhere.


I had the same problem. It turned out there was a function in my theme, that also filtered the content, but had a bug 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 for example you can do a quick "find in files" with ⌘/CTRL + + F to find possible culprits.


man86,

I see you are getting the post data via $wpdb->get_results(). The thing about it is that the data is returned raw, so you need to "prepare it" before you are able to use common post functions such as the_content() (which will return the content already formatted, like you'd like it to).

How about trying this (see comments on code):

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

Reference: http://codex.wordpress.org/Class_Reference/wpdb#Examples_5 ("Get all information on the Drafts by User 5")

Thanks, I hope that helps!

Vq.


You need to echo the results of the apply_filters call:

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

Or, as you've got it coded:

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


Sorry if this is too basic, but it might help if you echoed the content:

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


How are you adding the filter? You can use add_filter that specifies a function that will receive $content. You can do any filtering that you need via this function.

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


hmm.. for some reason, i can get content to display when I removed the top line and bottom line.

maybe the problem is the query_posts call .. and not the apply_filters() call.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消