[英]Wordpress - Get custom page templates by post type
我有這段代碼可以在主題目錄中顯示所有自定義頁面模板...
<?php
$templates = wp_get_theme()->get_page_templates();
foreach ( $templates as $template_name => $template_filename ) {
echo "$template_name ($template_filename)<br />";
}
?>
但是,我只想顯示可用於特定帖子類型的自定義模板。 有什么辦法嗎?
我想到了...
<?php
$templates = wp_get_theme()->get_page_templates($post = null, $post_type = 'page');
foreach ( $templates as $template_name => $template_filename ) {
echo "$template_name ($template_filename)<br />";
}
?>
請在下面嘗試此代碼,以編寫活動主題的functions.php文件
$page_query = new WP_Query(
array(
'post_type' => 'page',
'meta_key' => '_wp_page_template'
)
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.