请注意,您不需要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
滤器
希望您可以根据自己的需要进行调整。