应注意,如果has_archive
注册自定义帖子类型、帖子类型存档时设置为true/cptslug/
将内部重写为?post_type=cptslug
. 所以这也意味着is_post_type_archive()
将返回true。
不幸的是,当注册的重写段塞与post类型不同时,您实际上无法可靠地获得post_type
. 如果你的帖子类型是myplugin_cars
你的重写弹头是cars
你需要myplugin_cars
即使这样(如果当前查询的对象不是自定义post类型,为了防止出错),仍然会失败:
$queryobject = get_queried_object();
if (has_property(\'rewrite\',$queryobject)) {
if (isset($queryobject->rewrite[\'slug\'])) {
$posttype = $queryobject->rewrite[\'slug\'];
}
}
但是因为
is_post_type_archive
是的,这更可靠:
if (is_post_type_archive()) {
$posttype = get_query_var(\'post_type\');
// which is basically the same as:
// global $wp_query;
// $posttype = $wp_query->query_vars[\'post_type\'];
}
else ($posttype = \'post\';}
但等一下,还有更多。。。经过一点测试,结果也不是那么简单。。。如果您在分类法存档页面上,分类法中有多个帖子类型,该怎么办。。?或者将贴子标记指定给自定义贴子类型而不是贴子?或者在作者存档页上?日期存档页。。。甚至有一个复杂的
tax_query
或
meta_query
对于
WP_Query
?
唯一可靠的答案(没有对每个可能的归档案例进行测试)是在查询中循环实际帖子。。。以下是我在单数页和归档页上使用的完整功能,允许您有选择地传递自定义查询对象(或单数帖子的帖子对象/帖子ID):
function get_current_post_types($object=null) {
// if a numeric value passed, assume it is a post ID
if ( ($object) && (is_numeric($object)) ) {$object = get_post($object);}
// if an object is passed, assume to be a post object
if ( ($object) && (is_object($object)) ) {return get_post_type($object);}
// standard single post type checks
if (is_404()) {return \'\';}
// update: removed this check, handled by is_singular
// if (is_single()) {return \'post\';}
if (is_page()) {return \'page\';}
if (is_attachment()) {return \'attachment\';}
if (is_singular()) {return get_post_type();}
// if a custom query object was not passed, use $wp_query global
if ( (!$object) || (!is_object($object)) ) {
global $wp_query; $object = $wp_query;
}
if (!is_object($object)) {return \'\';} // should not fail
// if the post_type query var has been explicitly set
// (or implicitly set on the cpt via a has_archive redirect)
// ie. this is true for is_post_type_archive at least
// $vqueriedposttype = get_query_var(\'post_type\'); // $wp_query only
if (property_exists($object,\'query_vars\')) {
$posttype = $object->query_vars[\'post_type\'];
if ($posttype) {return $posttype;}
}
// handle all other cases by looping posts in query object
$posttypes = array();
if (method_exists($object,\'found_posts\')) {
if ($object->found_posts > 0) {
$queriedposts = $object->posts;
foreach ($queriedposts as $queriedpost) {
$posttype = $queriedpost->post_type;
if (!in_array($posttype,$posttypes)) {$posttypes[] = $posttype;}
}
if (count($posttypes == 1)) {return $posttypes[0];}
else {return $posttypes;}
}
}
return \'\'; // nothin to see here
}
这将是可靠的(我说过吗?)如果存在多个post类型,则返回一个post类型数组;如果只有一种类型,则返回一个具有单个post类型的字符串。您需要做的只是:
$posttypes = get_current_post_types();
// or pass a post ID
$posttypes = get_current_post_types($postid);
// or pass a post object
$posttypes = get_current_post_types($post);
// or pass a custom query - that has been run
$posttypes = get_current_post_types($query);
用法示例(仅供参考):
add_filter(\'the_posts\',\'myplugin_fading_thumbnails\',10,2);
function myplugin_fading_thumbnails($posts,$query) {
if (!is_archive()) {return $posts;}
$cptslug = \'myplugin_slug\'; $dosomethingcool = false;
$posttypes = get_current_post_types($query);
if ( (is_array($posttypes)) && (in_array($cptslug,$posttypes)) ) {$dosomethingcool = true;}
elseif ($cptslug == $posttypes) {$dosomethingcool = true;}
if ($dosomethingcool) {
global $fadingthumbnails; $fadingthumbnails = $cptslug;
if (!has_action(\'wp_footer\',\'myplugin_cpt_script\')) {
add_action(\'wp_footer\',\'myplugin_cpt_script\');
}
}
function myplugin_cpt_script() {
global $fadingthumbnails;
echo "<script>var thumbnailclass = \'img.thumbtype-".$fadingthumbnails."\';
function fadeoutthumbnails() {jQuery(thumbnailclass).fadeOut(3000,fadeinthumbnails);}
function fadeinthumbnails() {jQuery(thumbnailclass).fadeIn(3000,fadeoutthumbnails);}
jQuery(document).ready(function() {fadeoutthumbnails();});
</script>";
}
return $posts;
}
要查看效果,请将代码中的自定义帖子类型更改为
post
, 并添加
thumbtype-post
将属性设置为帖子缩略图。。。