我想知道是否有可能获得具有特定模板的页面ID。这是否可以获取分配给“page special.php”的页面的ID?
Get page id by template
4 个回复
最合适的回答,由SO网友:Pieter Goosen 整理而成
创建页面时,分配给该页面的模板将以与自定义字段相同的方式保存为自定义帖子元。这个meta_key
是_wp_page_template
以及meta_value
将是页面模板
你可以简单地利用get_pages
检索具有meta_value
指定模板的
$pages = get_pages(array(
\'meta_key\' => \'_wp_page_template\',
\'meta_value\' => \'page-special.php\'
));
foreach($pages as $page){
echo $page->ID.\'<br />\';
}
编辑2015年7月23日get_posts
然后就过去page
像post_type
和IDas
字段值。这将确保更快、更优化的查询,因为我们只返回数据库中的post id列,而不是所有给定页面的post id列(需要PHP 5.4+)
$args = [
\'post_type\' => \'page\',
\'fields\' => \'ids\',
\'nopaging\' => true,
\'meta_key\' => \'_wp_page_template\',
\'meta_value\' => \'page-special.php\'
];
$pages = get_posts( $args );
foreach ( $pages as $page )
echo $page . \'</br>\';
SO网友:Sushil Adhikari
如果页面模板位于子文件夹中,请选择主题文件夹/页面模板/页面模板。php然后您可以使用下面的查询:
$page_details = get_pages( array(
\'post_type\' => \'page\',
\'meta_key\' => \'_wp_page_template\',
\'hierarchical\' => 0,
\'meta_value\' => \'page-templates/page-template.php\'
));
上述代码也显示子页。谢谢
SO网友:Luca Reghellin
如果需要的话,下面是一个更加清晰的脚本,它考虑了一种语言。注意,它假定使用的是Polylang,而不是WPML。
function get_post_id_by_template($template,$lang_slug = null){
global $wpdb;
$wh = ($lang_slug) ? " AND t.slug = %s" : "";
$query = $wpdb->prepare(
"SELECT DISTINCT p.ID
FROM $wpdb->posts p
INNER JOIN $wpdb->postmeta meta ON meta.post_id = p.ID
INNER JOIN $wpdb->term_relationships tr ON meta.post_id = tr.object_id
INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN $wpdb->terms t ON tt.term_id = t.term_id
WHERE p.post_status = \'publish\' AND meta.meta_key = %s AND meta.meta_value = %s" . $wh,
\'_wp_page_template\',
$template,
$lang_slug
);
$ids = $wpdb->get_results($query);
if($ids && isset($ids[0])){
$p = $ids[0];
return $p->ID;
} else {
return false;
}
}// get_post_id_by_template
SO网友:Alexis Vandepitte
下面是一个可用于WPML和Polylang的完整函数。贷记至https://github.com/cyrale/
/**
* Search for a page with a particular template.
*
* @param string $template Template filename.
* @param array $args (Optional) See also get_posts() for example parameter usage.
* @param bool $single (Optional) Whether to return a single value.
*
* @return Will be an array of WP_Post if $single is false. Will be a WP_Post object if the page is find, FALSE otherwise
*/
if (!function_exists(\'get_page_by_template\')) {
function get_page_by_template($template, $args = array(), $single = true) {
$pages_by_template = wp_cache_get(\'pages_by_template\', \'cyrale\');
if (empty($pages_by_template) || !is_array($pages_by_template)) {
$pages_by_template = array();
}
if (!isset($pages_by_template[$template])) {
$args = wp_parse_args(array(
\'posts_per_page\' => -1,
\'post_type\' => \'page\',
\'suppress_filters\' => 0,
\'meta_query\' => array(
array(
\'key\' => \'_wp_page_template\',
\'value\' => $template,
),
),
), $args);
$pages = get_posts($args);
$pages_by_template[$template]= array(
\'single\' => !empty($pages) && is_array($pages) ? reset($pages) : false,
\'pages\' => $pages,
);
}
wp_cache_set(\'pages_by_template\', $pages_by_template, \'cyrale\');
return $pages_by_template[$template][$single ? \'single\' : \'pages\'];
}
}
结束