WordPress判断文章类型页面要用到的标签:
<?php is_single(); ?>
文章类型标签is_single()有多种用法,默认下是区分当前打开页面是否是文章的页面,当然,您还可以用来判断:
1根据某个ID判断是否是文章类型;
<?php
if(is_single('10026')){
echo true;
}
?>
2根据某个标题判断是否是文章类型;
<?php
if(is_single('SingleTitle')){
echo true;
}
?>
3根据slug判断是否是文章类型;
<?php
if(is_single('single-slug-122658'))
{
echo true;
}
?>
WordPress判断文章类型页面帮助主题开发
以下实践方法可供参考:
WordPress判断文章类型对不同的文章类型页面使用不同模板;
WordPress判断文章类型共用某个共用模板区分调用数据;
二次开发或者功能集成,判断文章类型拓展插件;
is_single()源代码:
is_single() 位于 /wp-includes/query.php 行728[5.9.3版本]
/**
* Determines whether the query is for an existing single post.
*
* Works for any post type, except attachments and pages
*
* If the $post parameter is specified, this function will additionally
* check if the query is for one of the Posts specified.
*
* For more information on this and similar theme functions, check out
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
* Conditional Tags} article in the Theme Developer Handbook.
*
* @since 1.5.0
*
* @see is_page()
* @see is_singular()
* @global WP_Query $wp_query WordPress Query object.
*
* @param int|string|int[]|string[] $post Optional. Post ID, title, slug, or array of such
* to check against. Default empty.
* @return bool Whether the query is for an existing single post.
*/
function is_single( $post = '' ) {
global $wp_query;
if ( ! isset( $wp_query ) ) {
_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
return false;
}
return $wp_query->is_single( $post );
}
public function is_single() 位于 /wp-includes/class-wp-query.php 行4215[5.9.3版本]
/**
* Is the query for an existing single post?
*
* Works for any post type excluding pages.
*
* If the $post parameter is specified, this function will additionally
* check if the query is for one of the Posts specified.
*
* @since 3.1.0
*
* @see WP_Query::is_page()
* @see WP_Query::is_singular()
*
* @param int|string|int[]|string[] $post Optional. Post ID, title, slug, path, or array of such
* to check against. Default empty.
* @return bool Whether the query is for an existing single post.
*/
public function is_single( $post = '' ) {
if ( ! $this->is_single ) {
return false;
}
if ( empty( $post ) ) {
return true;
}
$post_obj = $this->get_queried_object();
$post = array_map( 'strval', (array) $post );
if ( in_array( (string) $post_obj->ID, $post, true ) ) {
return true;
} elseif ( in_array( $post_obj->post_title, $post, true ) ) {
return true;
} elseif ( in_array( $post_obj->post_name, $post, true ) ) {
return true;
} else {
foreach ( $post as $postpath ) {
if ( ! strpos( $postpath, '/' ) ) {
continue;
}
$postpath_obj = get_page_by_path( $postpath, OBJECT, $post_obj->post_type );
if ( $postpath_obj && ( $postpath_obj->ID == $post_obj->ID ) ) {
return true;
}
}
}
return false;
}
以上是协典筒对is_single()的用法总结,可以帮助您用于判断是否是文章类型页面;其使用方法总结如下:
<?php
if(is_single){
echo "WordPress文章类型页面";
}
?>
<?php
if(is_single('我是文章标题')){
echo "WordPress文章类型页面";
}
?>
<?php
if(is_single('我是文章ID')){
echo "WordPress文章类型页面";
}
?>
<?php
if(is_single('我是文章slug')){
echo "WordPress文章类型页面";
}
?>