When you are creating a website, you may want to have a feature to link your posts to certain pages on the site. For example, in an employee listing, you may want to link employees to their articles or their bio pages.
To link between pages and posts, I recommend using the Advanced Custom Fields plugin.
Get The Plugin
Advanced Custom Fields
Once you have installed the plugin, a new menu item will be created in the main navigation of your WordPress dashboard area called “Custom Fields”. Go to this menu item.
To add a new Custom field, enter a title (such as “employees” -> this will just be used so you know which pages the custom field will be added to) then select + Add Field. Next, give the field a field label of “Related blogs”. The field name will automatically be generated (“related_blogs” in this case).
For the Field Type, select “Relationship”. A new field will show up called “Post Type” -> select “Post”. Next, scroll down to rules and enter your criteria for what pages you would like this option to appear on. In this example, I would select “Page Parent” is equal to “Employees”. Now all the pages with Employees as a page parent will have this option available to them. What this does is tell the field to list all posts while you are viewing an Employee page in the Dashboard Area as a selectable list.
Now, on pages listed as children of the ‘Employee’ page, you will be able to choose articles related to that specific employee.
The PHP Code Used To Get Related Blog Posts per Page
The first step with the code will be to set the related blogs to an array in a variable so we can test if it exists or not. Next, we perform a Boolean test to make sure the array exists (i.e., make sure that there are, indeed, blogs linked with the page). Once it is established as true, a heading will appear with the start of a list. Next, we loop through each result in the array echoing the title linked over to the actual post in individual list items: