我想在侧边栏中排除特定类别

时间:2014-12-06 作者:abdul

在我的侧边栏中,我想显示archieves list month排除“横幅”类别帖子。以下代码已尝试,但对我不起作用。

  <?php echo wp_get_archives(\'type=monthly&exclude=6&limit=5\'); ?>


  <?php echo wp_get_archives( array( \'type\' => \'monthly\', \'exclude\' => 6,\'limit\' => 5) ); ?> 

   <?php $args = array(
\'type\'            => \'monthly\',
\'limit\'           => \'\',
\'format\'          => \'html\', 
\'before\'          => \'\',
\'after\'           => \'\',
\'show_post_count\' => false,
\'echo\'            => 1,
\'order\'           => \'ASC\',
\'month\'             => -6
 ); 
  echo wp_get_archives( $args ); ?>  
我在侧栏中添加了这些代码。php

3 个回复
最合适的回答,由SO网友:birgire 整理而成

请注意,您不需要echo 显示结果,因为echo=1 是的默认设置wp_get_archives().

As@PieterGoosenexplained, 这个wp_get_archives() 函数不支持exclude 参数

但我们可以使用_exclude_terms, 的自定义参数wp_get_archives() 函数,以排除具有某些给定术语的帖子。

下面是一个示例:

/**
 * Exclude terms from the wp_get_archives() function.
 */
wp_get_archives( 
    array( 
        \'type\'           => \'monthly\', 
        \'_exclude_terms\' => \'21,22\',     // <-- Edit this to your needs!
        \'limit\'          => 5
    ) 
);
我们使用以下插件来支持此自定义参数:

<?php
/**
 * Plugin Name:   Enhance the wp_get_archive() function.
 * Description:   Support the \'_exclude_terms\' parameter.
 * Plugin URI:    https://wordpress.stackexchange.com/a/170535/26350
 * Plugin Author: birgire
 * Version:       0.0.1
 */

add_action( \'init\', function() {
        $o = new WPSE_Archive_With_Exclude;
        $o->init( $GLOBALS[\'wpdb\'] );
});

class WPSE_Archive_With_Exclude
{
    private $db = null;

    public function init( wpdb $db )
    {
        if( ( $this->db = $db ) instanceof wpdb )
            add_filter( \'getarchives_where\', 
                array( $this, \'getarchives_where\' ), 10, 2 );
    }

    public function getarchives_where( $where, $r )
    {                                                               
        if( isset( $r[\'_exclude_terms\'] ) )
        {   
            $_exclude_terms = $r[\'_exclude_terms\'];

            if( is_string( $_exclude_terms ) )
                $_exclude_terms = explode( \',\', $_exclude_terms );

            if( is_array( $_exclude_terms ) )
                $where .= $this->get_excluding_sql( $_exclude_terms );   
        }
        return $where;
    }

    private function get_excluding_sql( Array $terms )
    {
        $terms_csv = join( \',\', array_map( \'absint\', $terms ) );

        return " AND ( {$this->db->posts}.ID NOT IN 
            ( SELECT object_id FROM {$this->db->term_relationships} 
            WHERE term_taxonomy_id IN ( $terms_csv ) ) )";
    }

} // end class
注意,这里我们使用_exclude_terms 参数,以防核心支持exclude 参数。

这个_exclude_terms 参数可以是字符串:

\'_exclude_terms\' => \'21,22\',             // <-- Edit this to your needs!
或阵列:

\'_exclude_terms\' => array( 21, 22 ),     // <-- Edit this to your needs!
如果要使用插件从第一个本机存档小部件中排除某些术语,可以将其用于:

/**
 * Exclude terms from the first Archive widget.
 */

add_filter( \'widget_archives_args\', \'wpse_archive_exclude_terms\' );

function wpse_archive_exclude_terms ( $args )
{
    remove_filter( current_filter(), __FUNCTION__ );
    $args[\'_exclude_terms\'] = \'21,22\';    // <-- Edit this to your needs!
    return $args;
}
或类似于带有widget_archives_dropdown_args 滤器

希望您可以根据自己的需要进行调整。

SO网友:Pieter Goosen

您的代码无法工作,因为没有exclude 中的参数wp_get_archives(). 以下是有效参数及其默认值的列表

<?php $args = array(
    \'type\'            => \'monthly\',
    \'limit\'           => \'\',
    \'format\'          => \'html\', 
    \'before\'          => \'\',
    \'after\'           => \'\',
    \'show_post_count\' => false,
    \'echo\'            => 1,
    \'order\'           => \'DESC\'
); ?>
要完成所需的操作,需要在函数内部更改SQL查询。提供了两个过滤器来更改SQL查询,getarchives_wheregetarchives_join

您可以尝试以下操作(源代码取自here

add_filter( \'getarchives_where\', \'customarchives_where\' );
add_filter( \'getarchives_join\', \'customarchives_join\' );

function customarchives_join( $join ) {

    global $wpdb;

    return $join . " 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( $where ) {

    global $wpdb;

    $exclude = \'1\'; // category id to exclude

    return $where . " AND $wpdb->term_taxonomy.taxonomy = \'category\' 
                      AND $wpdb->term_taxonomy.term_id 
                      NOT IN ($exclude)";

}
例如,您很可能希望在完成后删除这些过滤器

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\' );

SO网友:Ankit Anand

在为此研究了几个小时之后。这个代码块对我有用。

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

结束

相关推荐

Get a list of categories ids

我正在使用基于自定义帖子类型的过滤器制作一个公文包。该过滤器必须只显示公文包中显示的帖子的类别,因为用户可以在短代码中通过id指定它们-[公文包id=“1,2,3”],而我无法获得该类别id的列表。下面是一个简单的例子,说明我正在尝试做什么:来自快捷码的自定义帖子ID列表:$ids 相同ID的数组:$id_array = explode(\',\', $ids) 必须返回类别ID列表的感兴趣的变量:$cat_ids = ??? 接下来,我们只获取具有所需id的类别:$ca