Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I guess there's a simple solution for this but couldn't find any.

I'm trying to fetch 3 pages and show them on footer. The problem is that every page is shown as text without formatting which means that shortcodes are printed as text as well.

Here's the code I'm using:

global $wpdb;
$sql            = "SELECT post_title, post_content FROM ...";
$footerElements = $wpdb->get_results( $sql );

foreach ( $footerElements as $footerElement ) {
    echo '<div class="four columns">';
    echo '<h5>' . $footerElement->post_title . '</h5>';
    echo $footerElement->post_content;
    echo '</div>';      
}

The reason I'm using SQL and not WP_Query is I have a plugin with pre_get_posts which is explicitly setting post_types (excluding page) when in category or tag page is shown. So I can't use WP_Query for that.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

You need to run the post_content filters. It is a one line change from your current code.

This...

echo $footerElement->post_content;

... should be ...

echo apply_filters('the_content',$footerElement->post_content);

That will give you all the same formatting as with normal posts.

You can of course pick and choose which filters you want to run, instead of running them all, and you may want to do that in case a plugin adds something to the post content via a filter (for example, a G+ button). Take a look at the default filters for the_content and check the Codex for the functions of the various callbacks.

To elaborate on that last idea, filter callbacks are basically functions that take input and return a (usually) modified version of that input. That means you can pretty much call them directly if you need to. They are functions like any other. For example...

Take this filter from default-filters.php :

add_filter( 'the_content', 'wptexturize'            );

The callback function is wptexturize. If you wanted to run that "filter" and only that filter you could write:

echo wptexturize($footerElement->post_content);

I can't swear that will work with every filter out there, since sometimes people do oddball things with filters, but it should work with most-- especially the core filters.

Another thing you can do is remove particular filters if you know which ones you don't want to run. It could be easier than typing out the ones you do want to run.

remove_filter( 'the_content', 'wptexturize' );
// some code maybe
echo apply_filters('the_content',$footerElement->post_content);
// more code maybe
// add the filter back in case it is used later in the page
add_filter('the_content','wptexturize');
share|improve this answer
    
OMG! That was fast, and what's the best... It f'king works! Thank you so much. Been struggling with my footer's WP_Query for 3 days now, until I realised that one plugin was causing pages not showing in the footer when the user is on tag/category page (has pre_get_posts). Appreciated! –  micadelli Feb 21 '13 at 15:22
    
What do you mean by pick and choose which filters you want to run, instead of running them all... isn't that just picking the the_content filter? –  micadelli Feb 21 '13 at 15:26
    
thanks for sharing that clear explanation of filters. –  micadelli Feb 22 '13 at 7:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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