is_single() WordPress関数 条件分岐タグ

is_single()

参照:WordPress Codex-is_single()

構文

is_single($post[オプション])

is_single()の詳細

 動 作 

個別投稿を表示しているかどうかをチェックする。

 

 返り値 

真偽値

true:個別投稿を表示している場合。

false:それ以外。

 

 引 数 

$post[オプション]

投稿のID、タイトル、スラッグ、又はそれらの配列

 

is_single()のコード

<?php
//WordPress Query API
669	/**
670	 * Determines whether the query is for an existing single post.
671	 *
672	 * Works for any post type, except attachments and pages
673	 *
674	 * If the $post parameter is specified, this function will additionally
675	 * check if the query is for one of the Posts specified.
676	 *
677	 * For more information on this and similar theme functions, check out
678	 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
679	 * Conditional Tags} article in the Theme Developer Handbook.
680	 *
681	 * @see is_page()
682	 * @see is_singular()
683	 *
684	 * @since 1.5.0
685	 *
686	 * @global WP_Query $wp_query WordPress Query object.
687	 *
688	 * @param int|string|array $post Optional. Post ID, title, slug, or array of such. Default empty.
689	 * @return bool Whether the query is for an existing single post.
690	 */
691	function is_single( $post = '' ) {
692	        global $wp_query;
693	
694	        if ( ! isset( $wp_query ) ) {
695	                _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
696	                return false;
697	        }
698	
699	        return $wp_query->is_single( $post );
700	}

//WP_Qureyクラスの定義

 /**
3803	         * Is the query for an existing single post?
3804	         *
3805	         * Works for any post type excluding pages.
3806	         *
3807	         * If the $post parameter is specified, this function will additionally
3808	         * check if the query is for one of the Posts specified.
3809	         *
3810	         * @see WP_Query::is_page()
3811	         * @see WP_Query::is_singular()
3812	         *
3813	         * @since 3.1.0
3814	         *
3815	         * @param int|string|array $post Optional. Post ID, title, slug, path, or array of such. Default empty.
3816	         * @return bool Whether the query is for an existing single post.
3817	         */
3818	        public function is_single( $post = '' ) {
3819	                if ( !$this->is_single )
3820	                        return false;
3821	
3822	                if ( empty($post) )
3823	                        return true;
3824	
3825	                $post_obj = $this->get_queried_object();
3826	
3827	                $post = array_map( 'strval', (array) $post );
3828	
3829	                if ( in_array( (string) $post_obj->ID, $post ) ) {
3830	                        return true;
3831	                } elseif ( in_array( $post_obj->post_title, $post ) ) {
3832	                        return true;
3833	                } elseif ( in_array( $post_obj->post_name, $post ) ) {
3834	                        return true;
3835	                } else {
3836	                        foreach ( $post as $postpath ) {
3837	                                if ( ! strpos( $postpath, '/' ) ) {
3838	                                        continue;
3839	                                }
3840	                                $postpath_obj = get_page_by_path( $postpath, OBJECT, $post_obj->post_type );
3841	
3842	                                if ( $postpath_obj && ( $postpath_obj->ID == $post_obj->ID ) ) {
3843	                                        return true;
3844	                                }
3845	                        }
3846	                }
3847	                return false;
3848	        }
?>

参照:wp-includes/query.php

参照:wp-includes/class-wp-query.php

 

WordPressのおすすめ-カスタマイズ編

おすすめ

 

スポンサードリンク

スポンサードリンク