我希望能够过滤我的侧边栏小部件(标记云、类别、归档),以便仅在作者模板上显示适用于给定作者的标记/类别/月份。
过滤或修改这些小部件的正确方法是什么?我发现this page 但似乎任何小部件的本机函数都不能接受作者id作为参数。
我希望能够过滤我的侧边栏小部件(标记云、类别、归档),以便仅在作者模板上显示适用于给定作者的标记/类别/月份。
过滤或修改这些小部件的正确方法是什么?我发现this page 但似乎任何小部件的本机函数都不能接受作者id作为参数。
仅仅抓取一个作者的所有帖子,然后过滤它们,只返回一个类别列表,这有点笨拙。如果您希望使用侧边栏中的默认小部件,并让作者仅在作者模板上对其进行过滤,那么我建议使用以下方法。
首先,过滤标签和类别小部件查询中的类别和标签。
add_filter( \'widget_tag_cloud_args\', \'filter_categories_by_author\' );
add_filter( \'widget_categories_dropdown_args\', \'filter_categories_by_author\' );
add_filter( \'widget_categories_args\', \'filter_categories_by_author\' );
function filter_categories_by_author( $args ){
// only process if on the author template
if( is_author() ){
$author_id = get_the_author_meta( \'ID\' );
$taxonomy = !empty( $args[\'taxonomy\'] ) ? $args[\'taxonomy\'] : \'category\';
// filter by including only IDs associated to the author
$args[\'include\'] = get_taxonomy_ids_by_author( $author_id, $taxonomy );
}
return $args;
}
然后使用dependent方法检索由用户ID过滤的分类ID。function get_taxonomy_ids_by_author( $user_id, $taxonomy = \'category\' ){
global $wpdb;
return $wpdb->get_col( $wpdb->prepare( "
SELECT DISTINCT(terms.term_id) as ID
FROM $wpdb->posts as posts
LEFT JOIN $wpdb->term_relationships as relationships ON posts.ID = relationships.object_ID
LEFT JOIN $wpdb->term_taxonomy as tax ON relationships.term_taxonomy_id = tax.term_taxonomy_id
LEFT JOIN $wpdb->terms as terms ON tax.term_id = terms.term_id
WHERE 1=1 AND (
posts.post_status = \'publish\' AND
posts.post_author = %d AND
tax.taxonomy = \'%s\' )
ORDER BY terms.name ASC
", $user_id, $taxonomy ) );
}
资料来源:https://gist.github.com/codearachnid/9655690没有现成的方法可以做到这一点,我也不知道以前是否尝试过。也就是说,工具都在那里,你必须把它们放在一起。
您可以使用获取当前用户ID$userid = wp_get_current_user()
然后,您可以使用$posts = WP_Query(\'author=\'.$userid)
然后由作者遍历所有帖子,并将每个帖子的类别附加到一个数组中。例如
$allCategories = Array();
foreach($posts as $post){
array_merge($allCategories, wp_get_post_categories($post->ID))
}
然后使用array_unique()
函数删除重复项。最后,浏览每个类别,并通过get_category_link()
我还没有测试过,但你可以试试
add_action(\'pre_get_posts\', callback);
function callback( &$query )
{
if ( is_author() ) {
$query->set(\'author\', get_the_author_meta(\'ID\') );
}
}
在我的Wordpress网站(仅限于页面,无帖子)中,我需要在侧边栏中放置一些静态blob。我们可以将这些“blob”称为小部件。至少他们会有一个固定的html内容,如“摘要1”、“摘要2”、“免责声明”、“foo策略”。。。我需要的是,在每一页上都有不同的安排。因此,这不仅仅是“单一”对“归档”,而是真正的“产品页面A”对“产品页面B”对“关于我们”对“服务条款”。。。我在考虑自定义元框,以便在单个编辑器页面上打开和关闭它们wp-admin/post.php?post=196&action=ed