If you’re using that template for only one page, you can try this function:
/**
* @param string $template Base name of the template.
*/
function wpse_233924_get_page_link($template)
{
$query = new \WP_Query;
// Get page which is using the $template.
$page = $query->query([
'post_type' => 'page',
'meta_key' => '_wp_page_template',
'meta_value' => $template,
'no_found_rows' => 1,
'posts_per_page' => 1 // Retrieve only one result.
]);
return isset($page[0]) ? esc_url( get_page_link($page[0]) ) : false;
}
Then:
$page_url = wpse_233924_get_page_link('template-portfolio.php') ? : '#';
?><a href="https://wordpress.stackexchange.com/questions/233924/<?= $page_url ?>">View portfolios</a><?php
Note that WordPress recognizes page-templates
folder by default. So, if you put the template-porfolio.php
inside that folder, the value of $template
must be page-templates/template-portfolio.php
.