Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress 是否从wp_get_存档中排除类别?_Wordpress_Categories_Archive - Fatal编程技术网

Wordpress 是否从wp_get_存档中排除类别?

Wordpress 是否从wp_get_存档中排除类别?,wordpress,categories,archive,Wordpress,Categories,Archive,有没有办法从wp_get_档案中排除类别?我试图在侧边栏中显示月份,但我想排除不是博客条目的帖子 $catID = get_cat_id('Projects'); $variable = wp_get_archives('type=monthly&show_post_count=1); echo $variable; 没有基于类别排除的机制--它纯粹用于基于时间的存档(每年、每月、每天、每周)或“每篇文章”存档:按文章标题排序的逐篇文章或逐篇文章。使用类别存档有不同的方法: 我使用重新

有没有办法从wp_get_档案中排除类别?我试图在侧边栏中显示月份,但我想排除不是博客条目的帖子

$catID = get_cat_id('Projects');
$variable = wp_get_archives('type=monthly&show_post_count=1);
echo $variable;

没有基于类别排除的机制--它纯粹用于基于时间的存档(每年、每月、每天、每周)或“每篇文章”存档:按文章标题排序的逐篇文章或逐篇文章。

使用类别存档有不同的方法:

我使用重新加载的干净归档文件并排除第200行周围的类别:

// Get a simple array of all posts
$rawposts = get_posts( 'numberposts=-1&category=-4,-6,-7,-9' );

您可能希望查看并倾向于自己的自定义解决方案。虽然这可能会花费你更多的时间和工作;你确实可以完全控制你想要实现的目标。

你可以在pre\u get\u posts上使用过滤器挂钩吗

我知道这样的东西适用于is_的作者,is_的家,is_的饲料

function exclude_stuff($query) { 
    if ( $query->is_author) {
        $query->set('cat', '-4, -142');
    }
    return $query;
}

add_filter('pre_get_posts', 'exclude_stuff');
这取决于你是否可以为像is_存档或is_月刊这样的东西做

您可以将其放入带有插件头的php文件中:

<?php
/*
 * Plugin Name: exclude some stuff
 * Description: blah
 * Author: blah
 * Plugin URI: blah
 * Version: 0.9
 * =======================================================================
*/
   Put the function here
?>


然后将其上传到插件目录并激活。

您可以在functions.php文件中编写一个过滤器,该过滤器将更改wp\u get\u 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)";

}

如果您只想在主题目录的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'";

}

将下面的代码放在后面 此代码已适用于我:)


在一个项目中遇到了这个问题,但从未在网上找到解决方案——我的不是最漂亮的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\u get\u archives()
列表中。您还可以调整代码以排除它们(上面的第一个示例)

如果您只需要一个
wp\u get\u archives()
列表实例来执行此操作,只需跳过
functions.php
中应用过滤器的前两行代码即可。然后,当您使用
wp\u get\u 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' );
?>

没有一种官方的方法可以做到这一点。但是我尝试并测试了很多代码块,只有这一个对我有效

    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;
    $include = 'your_category_id'; // category id to include
    return $x . " AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id IN ($include)";
}

用帖子类别的原始id替换你的类别id,代码就会工作。

这对主循环有效,而不是6年后粘贴到my functions.php中的wp_get_archives()函数copy,它仍然有效!事实上,我收回了。只有当文章只有该排除类别,而没有其他类别时,这才有效。
    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;
    $include = 'your_category_id'; // category id to include
    return $x . " AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id IN ($include)";
}