{"id":61,"date":"2022-11-08T08:16:35","date_gmt":"2022-11-08T07:16:35","guid":{"rendered":"https:\/\/gerardvandenbosch.nl\/?p=61"},"modified":"2022-11-08T08:16:36","modified_gmt":"2022-11-08T07:16:36","slug":"wordpress-front-page-include-multiple-page","status":"publish","type":"post","link":"https:\/\/gerardvandenbosch.nl\/?p=61","title":{"rendered":"WordPress Front page include multiple page"},"content":{"rendered":"\n<p>In WordPress a static front page can be created by using the file <code>front-page.php<\/code>, we can give this front page a template name in following way:<\/p>\n\n\n\n<pre class=\"wp-block-code has-dark-gray-background-color has-background\"><code lang=\"php\" class=\"language-php\">\/**\n* Template Name: Front Page\n*\/<\/code><\/pre>\n\n\n\n<p>Then when we create a page for example <code>Home<\/code>, configure it as a static page and select the template <code>Front Page<\/code>. This will then include the <code>Home<\/code> page in the front page and call the post content by simply doing this:<\/p>\n\n\n\n<pre class=\"wp-block-code has-dark-gray-background-color has-background\"><code lang=\"php\" class=\"language-php\">&lt;?php \n\/\/title\necho the_title();\n\/\/content\necho the_content();\n?><\/code><\/pre>\n\n\n\n<p>This all works fine for one page, but in case we want to include content from multiple pages in the <code>Front Page<\/code> this doesn&#8217;t work. <\/p>\n\n\n\n<p>However it can be done by adding some custom code to the <code>front-page.php<\/code> file.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>For this example we create a new page in WordPress with the title <code>About<\/code>, after that we can add a featured image, an excerpt and the post content itself.<\/p>\n\n\n\n<p>In order to get this page into our frontpage we need to add the following code:<\/p>\n\n\n\n<pre class=\"wp-block-code has-dark-gray-background-color has-background\"><code lang=\"php\" class=\"language-php\">&lt;?php\n$aboutPage = get_page_by_title(\"About\");\n?><\/code><\/pre>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>The function will return <code>null<\/code> if the page can not be found otherwise it will return an array.  <\/p>\n\n\n\n<p>The array will contain everything you need to display the post details, below are some examples.<\/p>\n\n\n\n<pre class=\"wp-block-code has-dark-gray-background-color has-background\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\/\/Show page title\necho $aboutPage->post_title;\n?><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code has-dark-gray-background-color has-background\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\/\/Show page excerpt\necho $aboutPage->post_excerpt;\n?><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code has-dark-gray-background-color has-background\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\/\/Show page content\necho $aboutPage->post_content;\n?><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code has-dark-gray-background-color has-background\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\/\/Show page featured image\n$aboutThumbnail = get_the_post_thumbnail_url($aboutPage->ID);\nif($aboutThumbnail) { ?>\n    &lt;img src=\"&lt;?php echo $aboutThumbnail; ?>\" alt=\"&lt;?php echo $aboutPage->post_title;?>\">\n&lt;?php } ?><\/code><\/pre>\n\n\n\n<p>Except for displaying properties we can also generate the permalink so it will show a link to <code>page.php<\/code> where we can show the page in its full potential and minimising the content on the frontpage.<\/p>\n\n\n\n<pre class=\"wp-block-code has-dark-gray-background-color has-background\"><code lang=\"php\" class=\"language-php\">&lt;a href=\"&lt;?php echo get_the_permalink($aboutPage->ID); ?>\">Read more&lt;\/a><\/code><\/pre>\n\n\n\n<p>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.<\/p>\n\n\n\n<pre class=\"wp-block-code has-dark-gray-background-color has-background\"><code lang=\"php\" class=\"language-php\">&lt;?php\nvar_dump($aboutPage);\n?><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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: 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[16,14],"class_list":["post-61","post","type-post","status-publish","format-standard","hentry","category-wordpress","tag-frontpage","tag-wordpress","post-preview"],"_links":{"self":[{"href":"https:\/\/gerardvandenbosch.nl\/index.php?rest_route=\/wp\/v2\/posts\/61","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gerardvandenbosch.nl\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gerardvandenbosch.nl\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gerardvandenbosch.nl\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gerardvandenbosch.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=61"}],"version-history":[{"count":3,"href":"https:\/\/gerardvandenbosch.nl\/index.php?rest_route=\/wp\/v2\/posts\/61\/revisions"}],"predecessor-version":[{"id":64,"href":"https:\/\/gerardvandenbosch.nl\/index.php?rest_route=\/wp\/v2\/posts\/61\/revisions\/64"}],"wp:attachment":[{"href":"https:\/\/gerardvandenbosch.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=61"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gerardvandenbosch.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=61"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gerardvandenbosch.nl\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=61"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}