Dvir Levy
Dvir Levy

Reputation: 8128

wordpress post/page content in code

i am trying to insert a post/page into one of my themes files and it wont display shortcodes or php

i have created a page called home in the wordpress admin-paned and inserted into my code the following:

<div id="home_page"> <!-- echos the content from the page "home" id:43 -->
            <?php $home_id = 43;
            $home_page = get_page( $home_id );
            ?>  
            <?php echo $home_page->post_content; ?>
    </div> <!-- end #home_page -->

and non of the shortcodes that i have in the page work. i installed a php in post or page and tried useing php and it doesnt work.

when i insert

echo do_shortcode('[youtube_sc url=http://www.youtube.com/watch?v=Db3XGpt6nNU]');

directly into the code it works.

does anyone know y this happens?

thank you.

Upvotes: 0

Views: 963

Answers (2)

Dvir Levy
Dvir Levy

Reputation: 8128

I got an answer in wordpress.stackexchange.com

I Quote:

You need to apply the filter the_content e.g.:

<?php echo apply_filters('the_content',$home_page->post_content); ?> Also, you don't need custom shortcodes for youtube, just put the URL in the content (but not a hyperlink), and it'll be swapped out for a youtube player at runtime. No plugins or extra code needed thanks to oembed.

Thank You Tom J Nowell

Upvotes: 1

dave jesch
dave jesch

Reputation: 21

The $home_page->post_content value is the exact post content as stored in the database. Echoing this does not give any of your shortcodes a chance to run.

You should use a "WordPress Loop" to display the content, as this sets things up to allow you to use template tags such as the_title() and the_content() - this will call the processing functions for shortcodes and other functions like wpautop() that "massage" post content for output.

If you don't want to use a Loop, you could output the content using echo do_shortcode($home_page->post_content);

This will run the post content through the shortcode processor, giving the shortcodes a chance to run.

For more on how WordPress "massages" post content, you can look here: http://codex.wordpress.org/How_WordPress_Processes_Post_Content

Upvotes: 0

Related Questions