In WordPress a static front page can be created by using the file front-page.php
, we can give this front page a template name in following way:
/**
* Template Name: Front Page
*/
Then when we create a page for example Home
, configure it as a static page and select the template Front Page
. This will then include the Home
page in the front page and call the post content by simply doing this:
<?php
//title
echo the_title();
//content
echo the_content();
?>
This all works fine for one page, but in case we want to include content from multiple pages in the Front Page
this doesn’t work.
However it can be done by adding some custom code to the front-page.php
file.
For this example we create a new page in WordPress with the title About
, after that we can add a featured image, an excerpt and the post content itself.
In order to get this page into our frontpage we need to add the following code:
<?php
$aboutPage = get_page_by_title("About");
?>
This will fetch the code by the title of the page, if you create a new page with a different title then you will need to change this statement accordingly.
The function will return null
if the page can not be found otherwise it will return an array.
The array will contain everything you need to display the post details, below are some examples.
<?php
//Show page title
echo $aboutPage->post_title;
?>
<?php
//Show page excerpt
echo $aboutPage->post_excerpt;
?>
<?php
//Show page content
echo $aboutPage->post_content;
?>
<?php
//Show page featured image
$aboutThumbnail = get_the_post_thumbnail_url($aboutPage->ID);
if($aboutThumbnail) { ?>
<img src="<?php echo $aboutThumbnail; ?>" alt="<?php echo $aboutPage->post_title;?>">
<?php } ?>
Except for displaying properties we can also generate the permalink so it will show a link to page.php
where we can show the page in its full potential and minimising the content on the frontpage.
<a href="<?php echo get_the_permalink($aboutPage->ID); ?>">Read more</a>
There are more properties available in the array of the page, if you want to explore all of them you can simply dump the variable to the screen.
<?php
var_dump($aboutPage);
?>
Leave a Reply