0

I'm quite new to WordPress and wonder if there is a way to take the content in a normal page created in WP and show this content in a div tag through a page template?

The setup is two divs next to each other showing different content based on the two WP pages. When I change a page's content, the output in the corresponding div tag should change as well.

1

Assuming you have the following pages:

  1. Home
  2. About us that has a slug of about-us

And you want to display the content of the About us page into the Home page.

<?php 
    $about_us = get_page_by_path('about-us'); // supply the page slug; i.e. about-us

    $about_us_content = get_the_content($about_us->ID);
?>

<div><?php echo $about_us_content ; ?></div>

Alternatively, you can use the get_page_by_title to retrieve the post using its page title

  • Thanks, I`ll try this in the morning and get back to you!👍😎 – Fredrik Feb 14 '18 at 21:14
  • Hello again, thank you for tip, I've tried but the only thing that shows is the contents from home, not the about us page. Because your code should be in the template. Must I add something to the functions file? Thank you in advance!! – Fredrik Feb 16 '18 at 10:30
  • The code snippet above should be on the right page template; i.e. home.php, or index.php. You can take a look at this Template Hierarchy for reference. – Den Isahac Feb 16 '18 at 12:08
  • <?php $klubbstadgar = get_page_by_title('klubbstadgar'); // supply the page slug; i.e. about-us $klubbstadgar_content = get_the_content($klubbstadgar->ID); ?> <div> <?php echo $klubbstadgar_content ; ?></div>, any thoughts?Now I have two templates, home and klubbstadgar (in swedish), in the left i have placed this code, i e home, with the intention to present the content in klubbstadgar in a div in the home page. the only thing that appears is the content of home!! so I am really a newbie this so I appreciate your help a lot!!! cheers – Fredrik Feb 19 '18 at 11:26
  • You should apply the_content before echoing it. "An important difference from the_content() is that get_the_content() does not pass the content through the 'the_content' filter. This means that get_the_content() will not auto-embed videos or expand shortcodes, among other things." – Krzysiek Dróżdż Mar 13 '18 at 11:28
0

you can get page data by id.

$pagecontent = get_post('hereispageid'); 
echo $pagecontent ->post_content;
  • You should apply filters in there. Raw post_content shouldn't be printed (shortcodes, and so on). – Krzysiek Dróżdż Mar 13 '18 at 11:29

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.