NB: Make sure you do a back up of your theme, files and database before attempting the tutorials
Last modified : Sep 04 2022
Estimated reading time : 1 minute, 35 seconds - 163 words
Share the post "Create a Custom Link for your Template Pages"
This tutorial will show you how to create a custom link for your template page: get_page_link.
First thing we need to do is create our template page in the root folder of our WordPress theme.
Create a template page: For example: User Favorites
<?php /** * Recent User Favorites * * This file contains the markup for recent entries displayed in the * 404 pages of the Prime theme. * * @package Nomdutheme */ ?>
The next step is creating the new page in WordPress Admin and associate it to the newly created template.
We will use a query, Post type, to match with our template page with the _wp_page_template meta Key and our template name as a value (Remplace user-profile.php by your template page). We will ask to return your page on demand.
function viadirectory_userprofile_id() { $args = [ 'post_type' => 'page', 'fields' => 'ids', 'nopaging' => true, 'meta_key' => '_wp_page_template', 'meta_value' => 'user-profile.php' ]; $pages = get_posts( $args ); foreach ( $pages as $page ) return $page; }
We will now create our custom permalink in order to load up the desired template. You can also add a custom CSS styling.
We will be using the get_page_link function and include another function, viadirectory_userprofile_id(), which returns the page query.
<ul> <li style="list-style-type: none;"> <li class="<?php if (is_page_template('user-profile.php')) { echo 'active'; } else {}; ?>"></l> </ul>
You now have a dynamic link that loads the desired Template Page.