有没有办法从wp_get_archives中排除一个类别?我试图在侧边栏中显示月份,但我想排除不是博客条目的帖子。
$catID = get_cat_id('Projects');
$variable = wp_get_archives('type=monthly&show_post_count=1);
echo $variable;
发布于 2011-05-12 16:22:30
如果只想在主题目录的functions.php中包含wp_get_archive函数的特定类别,请使用此选项
add_filter( 'getarchives_where', 'customarchives_where' );
add_filter( 'getarchives_join', 'customarchives_join' );
function customarchives_join( $x ) {
global $wpdb;
return $x . " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)";
}
function customarchives_where( $x ) {
global $wpdb;
$includes= '14'; // category id to include
return $x . " AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id = '$includes'";
}
发布于 2011-05-10 13:50:43
您可以在您的functions.php文件中编写一个过滤器,它将更改wp_get_archive函数的默认行为。
add_filter( 'getarchives_where', 'customarchives_where' );
add_filter( 'getarchives_join', 'customarchives_join' );
function customarchives_join( $x ) {
global $wpdb;
return $x . " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)";
}
function customarchives_where( $x ) {
global $wpdb;
$exclude = '1'; // category id to exclude
return $x . " AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id NOT IN ($exclude)";
}
发布于 2012-07-12 01:27:33
在一个项目中遇到了这个问题,但从未在网上找到解决方案--我的PHP不是最漂亮的,但它可以解决这个问题。
这是Katie建议的过滤器,我也在一些支持论坛中遇到过。这将放入你的functions.php
中
add_filter( 'getarchives_where', 'customarchives_where' );
add_filter( 'getarchives_join', 'customarchives_join' );
function customarchives_join( $x ) {
global $wpdb;
return $x . " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)";
}
function customarchives_where( $x ) {
global $wpdb;
$categories = get_terms( 'taxonomy-name', 'orderby=id' );
$includeIds;
$i = 0;
foreach($categories as $category) {
if($i != 0) $includeIds .= ',';
$includeIds .= $category->term_id;
$i++;
}
return $x . " AND $wpdb->term_taxonomy.taxonomy = 'taxonomy-name'
AND $wpdb->term_taxonomy.term_id IN ($includeIds)";
}
在第二个函数中,将taxonomy-name
替换为实际的自定义分类法的名称。
定制分类中的所有术语ID都被捕获在一个字符串中;其余的操作与原始函数相同--只有定制分类中的类别列表包含在wp_get_archives()
列表中。您还可以调整代码以排除它们(上面的第一个示例)。
如果只想让wp_get_archives()
列表的一个实例执行此操作,只需跳过functions.php
中应用过滤器的前两行代码。然后,当您使用wp_get_archives()
标记时,在它之前应用滤镜,然后删除它们:
<?php
add_filter( 'getarchives_where', 'customarchives_where' );
add_filter( 'getarchives_join', 'customarchives_join' );
wp_get_archives();
remove_filter( 'getarchives_where', 'customarchives_where' );
remove_filter( 'getarchives_join', 'customarchives_join' );
?>
https://stackoverflow.com/questions/2789228
复制相似问题