如果我创建模板公文包,如何获取此模板的链接?例如:
<a href="<?php the_link"> View portfolios </a>
这里的功能或链接是什么?如果我创建模板公文包,如何获取此模板的链接?例如:
<a href="<?php the_link"> View portfolios </a>
这里的功能或链接是什么?如果仅将该模板用于一个页面,则可以尝试此功能:
/**
* @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
.我希望永久链接像youtube一样,由9位数的字母和数字生成我修改了这个代码add_filter( \'wp_unique_post_slug\', \'unique_slug_108286\', 10, 4 ); function unique_slug_108286( $slug) { $n=4; $slug = bin2hex(random_bytes($n)); //just an example return $slug; }&