如何测试帖子是否为自定义帖子类型?
6 个回答
- 投票数
-
- 2011-01-11
您在这里:
get_post_type()
,然后是if ( 'book' == get_post_type() ) ...
根据Codex中的条件标签>帖子类型./p>Here you are:
get_post_type()
and thenif ( 'book' == get_post_type() ) ...
as per Conditional Tags > A Post Type in Codex.-
[`is_singular()`](http://codex.wordpress.org/Function_Reference/is_singular)更加紧凑[有条件的标签>单页,单个帖子或附件](http://codex.wordpress.org/Conditional_Tags#A_Single_Page.2C_Single_Post_or_Attachment)[`is_singular()`](http://codex.wordpress.org/Function_Reference/is_singular) is bit more compact [Conditional Tags > A Single Page, Single Post or Attachment](http://codex.wordpress.org/Conditional_Tags#A_Single_Page.2C_Single_Post_or_Attachment)
- 26
- 2011-01-11
- Rarst
-
- 2012-06-12
if ( is_singular( 'book' ) ) { // conditional content/code }
当查看自定义帖子类型的帖子:
true
时,以上为book
.if ( is_singular( array( 'newspaper', 'book' ) ) ) { // conditional content/code }
当查看以下自定义帖子类型的帖子时,以上为
book
:newspaper
或true
.这些和更多条件标签可以在此处查看.
if ( is_singular( 'book' ) ) { // conditional content/code }
The above is
true
when viewing a post of the custom post type:book
.if ( is_singular( array( 'newspaper', 'book' ) ) ) { // conditional content/code }
The above is
true
when viewing a post of the custom post types:newspaper
orbook
.These and more conditional tags can be viewed here.
-
- 2011-07-06
将其添加到您的
functions.php
中,就可以在循环内或循环外使用该功能:function is_post_type($type){ global $wp_query; if($type == get_post_type($wp_query->post->ID)) return true; return false; }
因此您现在可以使用以下内容:
if (is_single() && is_post_type('post_type')){ // Work magic }
Add this to your
functions.php
, and you can have the functionality, inside or outside of the loop:function is_post_type($type){ global $wp_query; if($type == get_post_type($wp_query->post->ID)) return true; return false; }
So you can now use the following:
if (is_single() && is_post_type('post_type')){ // Work magic }
-
这已经停止为其他人工作了吗?我已经使用了很久了,但是突然之间,它对我停止了工作.但是,使用相同的方法** without **全局$ wp_query始终有效:`if('post-type'==get_post_type()){}`Has this stopped working for anyone else? I've used this for ages, but suddenly this stopped working for me. However, using the same method **without** global $wp_query always works: `if ( 'post-type' == get_post_type() ) {}`
- 0
- 2017-01-13
- turtledropbomb
-
is_post_type()已贬值.is_post_type() is depreciated.
- 0
- 2017-12-21
- Lisa Cerilli
-
- 2013-04-15
要测试帖子是否为任何个自定义帖子类型,请获取所有非内置帖子类型的列表,并测试该帖子的类型是否在该列表中.
功能:
/** * Check if a post is a custom post type. * @param mixed $post Post object or ID * @return boolean */ function is_custom_post_type( $post = NULL ) { $all_custom_post_types = get_post_types( array ( '_builtin' => FALSE ) ); // there are no custom post types if ( empty ( $all_custom_post_types ) ) return FALSE; $custom_types = array_keys( $all_custom_post_types ); $current_post_type = get_post_type( $post ); // could not detect current type if ( ! $current_post_type ) return FALSE; return in_array( $current_post_type, $custom_types ); }
用法:
if ( is_custom_post_type() ) print 'This is a custom post type!';
To test if a post is any custom post type, fetch the list of all not built-in post types and test if the post’s type is in that list.
As a function:
/** * Check if a post is a custom post type. * @param mixed $post Post object or ID * @return boolean */ function is_custom_post_type( $post = NULL ) { $all_custom_post_types = get_post_types( array ( '_builtin' => FALSE ) ); // there are no custom post types if ( empty ( $all_custom_post_types ) ) return FALSE; $custom_types = array_keys( $all_custom_post_types ); $current_post_type = get_post_type( $post ); // could not detect current type if ( ! $current_post_type ) return FALSE; return in_array( $current_post_type, $custom_types ); }
Usage:
if ( is_custom_post_type() ) print 'This is a custom post type!';
-
这应该是公认的答案.This should be the accepted answer.
- 1
- 2018-03-21
- aalaap
-
-
- 2014-01-30
如果您想要通配符检查所有自定义帖子类型:
if( ! is_singular( array('page', 'attachment', 'post') ) ){ // echo 'Imma custom post type!'; }
这样,您无需知道自定义帖子的名称.即使以后更改自定义帖子的名称,该代码也仍然有效.
If you want a wild card check for all your custom post types:
if( ! is_singular( array('page', 'attachment', 'post') ) ){ // echo 'Imma custom post type!'; }
This way you don't need to know the name of your custom post. Also the code still work even if you change the name of your custom post later.
我正在寻找一种测试帖子是否为自定义帖子类型的方法.例如,在侧边栏,我可以这样输入代码:
我只想对自定义帖子类型进行代码测试.