wordpress判断当前文章是否为自定义内容类型
先来看WordPress自定义类型实例
除了以上的保留文章类型外,为了满足多样化需求,我们可以自定义一些文章类型,例如:公告、视频、专题等等,自定义文章类型的实际用途很广,可以制作出复杂多变的表现形式,先来看看一个简单自定义类型的例子:
实例代码如下:
add_action( 'init' , 'create_post_type' ); function create_post_type() { register_post_type( 'acme_product' , array ( 'labels' => array ( 'name' => __( 'Products' ), 'singular_name' => __( 'Product' ) ), 'public' => true, 'has_archive' => true, ) ); }在这个例子中我们创建了一个名为acme_product的文章类型,从上面可知道自定义文章类型主要是用了一个函数register_post_type,这个函数为注册文章类型函数,通过它可以注册新的文章类型,其基本用法如下:
<?php register_post_type( $post_type, $args ); ?>
其中的$post_type为必需项,定义文章类型的名称,$args为可选项,用来配置一些数组,关于$args的数组,参数非常多.
判断当前文章是不是自定义内容类型
其实这样的功能实在非常的简单,在根据当前内容的id就可以使用get_post等等函数返回这个内容的对象,对象中就有一个post_type的方法,但是在老外的博客看到了,我想还是翻译一下,代码如下:
function is_custom_post_type() { global $wp_query ; $post_types = get_post_types( array ( 'public' => true, '_builtin' => false), 'names' , 'and' ); foreach ( $post_types as $post_type ) { if (get_post_type( $post_type ->ID) == get_post_type( $wp_query ->post->ID)) { return true; } else { return false; } } }把上面的代码放到主题的functions.php文件中就可以使用如下的函数判断,代码如下:
if (is_custom_post_type()) { //如果内容类型为自定义类型则返回true否则返回false }查看更多关于wordpress判断当前文章是否为自定义内容类型 - W的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did8598