个性化的WordPress原创主题

WordPress文章企业网站主题模板
WordPress主题建站 > WordPress标签 > WordPress判断是否文章类型页面is_single()

WordPress判断是否文章类型页面is_single()

责任编辑:程程 更新时间: 关注:867

WordPress判断文章类型页面要用到的标签:

  1. <?php is_single(); ?>

文章类型标签is_single()有多种用法,默认下是区分当前打开页面是否是文章的页面,当然,您还可以用来判断:

1根据某个ID判断是否是文章类型;

  1. <?php
  2. if(is_single('10026')){
  3. echo true;
  4. }
  5. ?>

2根据某个标题判断是否是文章类型;

  1. <?php
  2. if(is_single('SingleTitle')){
  3. echo true;
  4. }
  5. ?>

3根据slug判断是否是文章类型;

  1. <?php
  2. if(is_single('single-slug-122658'))
  3. {
  4. echo true;
  5. }
  6. ?>

WordPress判断文章类型页面帮助主题开发

以下实践方法可供参考:

WordPress判断文章类型对不同的文章类型页面使用不同模板;

WordPress判断文章类型共用某个共用模板区分调用数据;

二次开发或者功能集成,判断文章类型拓展插件;

is_single()源代码:

  1. is_single() 位于 /wp-includes/query.php 行728[5.9.3版本]
  1. /**
  2.  * Determines whether the query is for an existing single post.
  3.  *
  4.  * Works for any post type, except attachments and pages
  5.  *
  6.  * If the $post parameter is specified, this function will additionally
  7.  * check if the query is for one of the Posts specified.
  8.  *
  9.  * For more information on this and similar theme functions, check out
  10.  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  11.  * Conditional Tags} article in the Theme Developer Handbook.
  12.  *
  13.  * @since 1.5.0
  14.  *
  15.  * @see is_page()
  16.  * @see is_singular()
  17.  * @global WP_Query $wp_query WordPress Query object.
  18.  *
  19.  * @param int|string|int[]|string[] $post Optional. Post ID, title, slug, or array of such
  20.  *                                        to check against. Default empty.
  21.  * @return bool Whether the query is for an existing single post.
  22.  */
  23. function is_single( $post = '' ) {
  24. global $wp_query;

  25. if ( ! isset( $wp_query ) ) {
  26. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  27. return false;
  28. }

  29. return $wp_query->is_single( $post );
  30. }
  1. public function is_single() 位于 /wp-includes/class-wp-query.php 行4215[5.9.3版本]
  1. /**
  2.  * Is the query for an existing single post?
  3.  *
  4.  * Works for any post type excluding pages.
  5.  *
  6.  * If the $post parameter is specified, this function will additionally
  7.  * check if the query is for one of the Posts specified.
  8.  *
  9.  * @since 3.1.0
  10.  *
  11.  * @see WP_Query::is_page()
  12.  * @see WP_Query::is_singular()
  13.  *
  14.  * @param int|string|int[]|string[] $post Optional. Post ID, title, slug, path, or array of such
  15.  *                                        to check against. Default empty.
  16.  * @return bool Whether the query is for an existing single post.
  17.  */
  18. public function is_single( $post = '' ) {
  19. if ( ! $this->is_single ) {
  20. return false;
  21. }

  22. if ( empty( $post ) ) {
  23. return true;
  24. }

  25. $post_obj = $this->get_queried_object();

  26. $post = array_map( 'strval', (array) $post );

  27. if ( in_array( (string) $post_obj->ID, $post, true ) ) {
  28. return true;
  29. } elseif ( in_array( $post_obj->post_title, $post, true ) ) {
  30. return true;
  31. } elseif ( in_array( $post_obj->post_name, $post, true ) ) {
  32. return true;
  33. } else {
  34. foreach ( $post as $postpath ) {
  35. if ( ! strpos( $postpath, '/' ) ) {
  36. continue;
  37. }
  38. $postpath_obj = get_page_by_path( $postpath, OBJECT, $post_obj->post_type );

  39. if ( $postpath_obj && ( $postpath_obj->ID == $post_obj->ID ) ) {
  40. return true;
  41. }
  42. }
  43. }
  44. return false;
  45. }

以上是协典筒对is_single()的用法总结,可以帮助您用于判断是否是文章类型页面;其使用方法总结如下:

  1. <?php
  2. if(is_single){
  3. echo "WordPress文章类型页面";
  4. }
  5. ?>
  1. <?php
  2. if(is_single('我是文章标题')){
  3. echo "WordPress文章类型页面";
  4. }
  5. ?>
  1. <?php
  2. if(is_single('我是文章ID')){
  3. echo "WordPress文章类型页面";
  4. }
  5. ?>
  1. <?php
  2. if(is_single('我是文章slug')){
  3. echo "WordPress文章类型页面";
  4. }
  5. ?>
  • 获赞21
  • 声明:内容版权归原作者所有,未经授权不得任意转载
  • 本文标题和链接:
    WordPress判断是否文章类型页面is_single()
    https://e.69525.com/f/c242799a663c1b62/

相关阅读

Copyright © 2023 WordPress主题. All rights reserved.Powered by e.69525.com.

本站基于WordPress主题搭建,正在以新的版本流畅运行;由69525提供主题免费升级支持