从 wp_get_archives 中排除类别?

发布于 2024-08-31 21:08:43 字数 210 浏览 4 评论 0原文

有什么方法可以从 wp_get_archives 中排除某个类别吗?我试图在侧边栏中显示月份,但我想排除不是博客条目的帖子。

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

Is there any way to exclude a category from wp_get_archives? I'm trying to show the months in the sidebar, but I want to exclude the posts that are not blog entries.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

不必在意 2024-09-07 21:08:47

您可以在 pre_get_posts 上使用过滤器挂钩吗?

我知道这样的东西适用于 is_author、is_home 和 is_feed...

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

add_filter('pre_get_posts', 'exclude_stuff');

取决于您是否可以对 is_archive 或 is_monthly 之类的东西执行此操作

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

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

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

Can you use a filter hook on pre_get_posts instead?

I know something like this works for is_author, is_home, and is_feed...

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

add_filter('pre_get_posts', 'exclude_stuff');

depends on whether you can do it for something like is_archive or is_monthly

You would drop that in a php file with a plugin header:

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

Then upload it to your Plugins directory and activate it.

云淡风轻 2024-09-07 21:08:47

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

    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)";
}

将 your_category_id 替换为您的帖子类别的原始 ID,代码将起作用。

There isn't an official way of doing this. But i tried and tested a lot of code block and only this one worked for me.

    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)";
}

Replace your_category_id with an original id of your post category and the code will work.

木森分化 2024-09-07 21:08:46

您可以在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)";

}

You can write a filter in your functions.php file which will change wp_get_archive function's default behavior.

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)";

}
与风相奔跑 2024-09-07 21:08:46

在一个项目中遇到这个问题,但从未在网上找到解决方案——我的不是最漂亮的 PHP,但它可以解决问题。

这是凯蒂建议的过滤器的一个发挥,我也在一些支持论坛上遇到过。这位于您的 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' );
?>

Ran into this problem in a project but never found a solution online -- mine isn't the prettiest PHP, but it does the trick.

This is a play off the filter suggested by Katie, which I ran across in few support forums as well. This goes in your 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)";

}

In the second function, swap taxonomy-name for the name of your actual custom taxonomy.

All the IDs of terms in your custom taxonomy are captured in a string; the rest operates the same as the original function -- only that list of categories from your custom taxonomy are included in the wp_get_archives() list. You can also tweak the code to exclude them as well (first example above).

If you only want one instance of the wp_get_archives() list to do this, just skip the top two lines of code in your functions.php that apply the filters. Then, when you use the wp_get_archives() tag, apply the filters before it, and remove them afterwards:

<?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' );
?>
无远思近则忧 2024-09-07 21:08:46

wp_get_archives() 没有基于类别排除的机制-- 它纯粹用于基于时间的存档(每年、每月、每日、每周)或“每个帖子”存档:postbypost 或按帖子标题排序的逐个帖子。

wp_get_archives() does not have a mechanism to exclude based on category -- it's purely for time-based archives (yearly, monthly, daily, weekly) or "every post" archives: postbypost or post by post ordered by post title.

如果没有你 2024-09-07 21:08:46

处理类别档案有不同的方法: WordPress › 支持 » 将档案限制为类别/日期类别档案

我使用 Clean Archives Reloaded WordPress › Clean Archives Reloaded « WordPress插件并排除第 200 行周围的类别:

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

There are different ways to work with category archives: WordPress › Support » Limit archives to category / date archives for category

I use Clean Archives Reloaded WordPress › Clean Archives Reloaded « WordPress Plugins and exclude categories around line 200:

// Get a simple array of all posts
$rawposts = get_posts( 'numberposts=-1&category=-4,-6,-7,-9' );
财迷小姐 2024-09-07 21:08:46

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

You might want to look in to get_categories and lean towards your own custom solution. While this may cost you a little more time and work; you will indeed get the upshot of having full control over what you're trying to achieve.

路弥 2024-09-07 21:08:46

将下面的代码放在后面
这段代码已经对我有用了:)

<?php
if ( $wp_query->is_archive ){
$wp_query->query_vars["cat"] = 14; // only the category that you want to inlcude
$wp_query->query_vars["posts_per_page"] = 10; // for number of posts you want
$wp_query->get_posts();}
?>

Place the code below just after
This code is working for me already :)

<?php
if ( $wp_query->is_archive ){
$wp_query->query_vars["cat"] = 14; // only the category that you want to inlcude
$wp_query->query_vars["posts_per_page"] = 10; // for number of posts you want
$wp_query->get_posts();}
?>
独夜无伴 2024-09-07 21:08:44

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

}

Use this if you want to include only specific categories for wp_get_archive function in your functions.php of your theme directory

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'";

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文