Reputation: 11
I am using this code to bring the content out of the page.
<?php
$page = get_page_by_title( 'page-title' );
$title = $page->post_title;
echo "<h3>" . $title . "</h3>";
$content = apply_filters('the_content', $page->post_content);
echo $content;
?>
Now, i want to get the content and title by using page ID.
If any one has the answer?
Upvotes: 1
Views: 175
Reputation: 10643
You can use the get_post()
function
$myPost = get_post(17);
echo $myPost->post_title;
Per your comment:
Is there any way i can avoid putting id in the direct code. eg. just making a page and content from back-end it reflects in front-end. i am making a onepage website. can it will be done by any kind of loop or something like that.
I bolded that word because if this is the case there's a very simple solution, but there are many ways to reach it.
Option 1:
If you're familiar with the WordPress Template Hierarchy you'll know you can create a PHP paged called front-page.php
. Then you can create 1 single page under Pages
, once that's done you can go to Settings -> Reading
and click the radio button which allows you to
Set a static page (select below)
You can then select the page you created. This will automatically use the front-page.php
template versus page.php
.
Option 2:
You can copy your page.php
template and rename it to use a pages slug. For instance, if I create a page called "Contact Us" WordPress will automatically make a slug that looks like contact-us
which is part of the web URL to view the page. I can then copy page.php
and rename it page-contact-us.php
so when you view the "Contact Us" page, it will automatically use this page template.
Option 3:
Finally you can directly create a Page Template where you select it from a drop down in the Admin Panel when you create a new page. All you have to do is copy your page.php
template file and call it whatever you want, something standard like page_template-contact
or something of the sort. Then you can add this direct above your get_header()
call:
/**
Template Name: My Custom Page
**/
Upvotes: 1