我相信您正在使用以下代码this pastebin link
function posts_by_year() {
// array to use for results
$years = array();
// get posts from WP
$posts = get_posts(array(
\'numberposts\' => -1,
\'orderby\' => \'post_date\',
\'order\' => \'ASC\',
\'post_type\' => \'my-custom-post-type\',
\'post_status\' => \'publish\'
));
// loop through posts, populating $years arrays
foreach($posts as $post) {
$years[date(\'Y\', strtotime($post->post_date))][] = $post;
}
// reverse sort by year
krsort($years);
return $years;
}
此函数使用
get_posts
返回帖子列表。要排除类别,可以传递
category
参数。只有一个音符
Note: category参数需要是类别的ID,而不是类别名称。
Note: category参数可以是以逗号分隔的类别列表,如get_posts()
函数将“category”参数直接传递到WP_Query
像cat
.
EDIT
请注意,要排除类别,需要使用减号(
-
) 在类别ID前签名。类似的操作即可。只要换一下
-13
到
-ID
其中ID是要排除的类别的ID
function posts_by_year() {
// array to use for results
$years = array();
// get posts from WP
$posts = get_posts(array(
\'numberposts\' => -1,
\'orderby\' => \'post_date\',
\'order\' => \'ASC\',
\'post_type\' => \'my-custom-post-type\',
\'post_status\' => \'publish\',
\'category\' => -13
));
// loop through posts, populating $years arrays
foreach($posts as $post) {
$years[date(\'Y\', strtotime($post->post_date))][] = $post;
}
// reverse sort by year
krsort($years);
return $years;
}
EDIT 2
按原样复制并粘贴此代码。你有一个语法错误,这就是为什么你得到了空白页
function posts_by_year() {
// array to use for results
$years = array();
// get posts from WP
$posts = get_posts(array(
\'numberposts\' => -1,
\'orderby\' => \'post_date\',
\'order\' => \'ASC\',
\'post_type\' => \'project\',
\'post_status\' => \'publish\',
\'category\' => -27
));
// loop through posts, populating $years arrays
foreach($posts as $post) {
$years[date(\'Y\', strtotime($post->post_date))][] = $post;
}
// reverse sort by year
krsort($years);
return $years;
}
EDIT 3
似乎您正在使用自定义分类法。如果是这种情况,请使用
tax_query
有关可用参数,请参见
WP_Query
. 您只需在
taxonomy
参数
function posts_by_year() {
// array to use for results
$years = array();
// get posts from WP
$posts = get_posts(array(
\'numberposts\' => -1,
\'orderby\' => \'post_date\',
\'order\' => \'ASC\',
\'post_type\' => \'project\',
\'post_status\' => \'publish\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'NAME OF YOUR CUSTOM TAXONOMY\',
\'field\' => \'term_id\',
\'terms\' => \'27\',
\'operator\' => \'NOT IN\'
),
),
));
// loop through posts, populating $years arrays
foreach($posts as $post) {
$years[date(\'Y\', strtotime($post->post_date))][] = $post;
}
// reverse sort by year
krsort($years);
return $years;
}