如果仅将该模板用于一个页面,则可以尝试此功能:
/**
 * @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;
}
 然后:
$page_url = wpse_233924_get_page_link(\'template-portfolio.php\') ? : \'#\';
?><a href="<?= $page_url ?>">View portfolios</a><?php
 注意WordPress可以识别
page-templates 默认情况下为文件夹。所以,如果你把
template-porfolio.php 在该文件夹中
$template 必须是
page-templates/template-portfolio.php.