Wordpress - 按 post 类型获取自定义页面模板

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 />";
    }
 ?>

但是,我只想显示特定 post 类型可用的自定义模板。有什么办法吗?

试试下面这个编码你的活动主题 functions.php 文件

$page_query = new WP_Query(
    array(
        'post_type' => 'page',
        'meta_key' => '_wp_page_template'
    )
);

我想通了...

<?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 />";
    }
 ?>