ABoooo
ABoooo

Reputation: 171

Get post content from a page with some ID

I have a one pager where some sections have posts and in some sections I just have one content input (simple text block). If I have just one content input I do not want to use posts but pages as it makes more sense. So when I have a page I would like to include a content from this page in some section. I have tried this

<?php
  $page = get_post( 58 );
  echo '<h2>';
  $title = $page->post_title;
  echo '</h2>';
  $content =  apply_filters( 'the_content', $page->post_content ); ?>

But it doesn't work. I don't want to use some plugin for this and I just cannot find a solution that actually work. Thanks!

UPDATE

The answer from Francesco works and I also found some other solution. Here it is

<?php
                function show_post($path) {
                    $post = get_page_by_path($path);
                    echo '<h1 class="section-heading text-center">';
                    $title = apply_filters('the_title', $post->post_title);
                    echo $title;
                    echo '</h1>';
                    $content = apply_filters('the_content', $post->post_content);
                    echo $content;
                }

                show_post('path');
                ?>

Where 'path' is the last part of your page url: www.yourdomain.com/path. Hope it helps.

Here is the reference page

Upvotes: 4

Views: 18481

Answers (1)

FrancescoCarlucci
FrancescoCarlucci

Reputation: 1590

I am not totally sure, what you mean, but you could try this:

echo get_post_field('post_content', 58);

Here, the number 58 is the ID of the post/page you want to retrieve the content.

Upvotes: 7

Related Questions