【问题标题】:Display posts by one author and only from current category (wordpress)显示一位作者的帖子,并且仅来自当前类别(wordpress)
【发布时间】:2014-04-20 22:18:36
【问题描述】:

我需要你的帮助来解决我的问题。

所以,我无法做到: 例如,我有一个类别“新闻”,这个类别有一些子类别“政治”、“体育”、“生活”等。

在侧边栏中显示了作者姓名列表,如果我单击某个类别中的某个作者,我只需要显示该作者以及当前类别子类别中的帖子。

如何创建它?我想它可以通过 pre_get_posts 创建但不确定。

【问题讨论】:

  • stackoverflow.com/a/18512467/1071555 这几乎是我需要的,但我需要为某些用户动态获取当前类别。我尝试从 function.php 获取类别 ID,但我收到一个空结果。怎么了?

标签: wordpress


【解决方案1】:

我找到了解决方案。

显示所有甚至在某个类别中写过帖子的用户(我创建了它的头像/名称的轮播滑块)。然后我生成链接并在最后添加当前类别。

<ul id="carousel">                                                                                                                                                                                                      
<?php
$current_category = single_cat_title("", false);
$author_array = array();

$args = array(
'numberposts' => -1,
'category_name' => $current_category,
'orderby' => 'author',
'order' => 'ASC'
);
$cat_posts = get_posts($args);
foreach ($cat_posts as $cat_post) :
if (!in_array($cat_post->post_author,$author_array)) {
$author_array[] = $cat_post->post_author;
}
endforeach;
foreach ($author_array as $author) :
$auth = get_userdata($author)->display_name;
$langCur = pll_default_language();
$auth_link = get_userdata($author)->user_login;
$homeurl = home_url();

echo "<li>";
echo "<a href='".$homeurl."/author/".$auth_link."/?cat=".get_cat_ID( $current_category )."' title=''>";
echo get_avatar( $author, $size = '140' );
echo "<span>";
echo $auth;
echo "</span>";
echo "</a></li>";
endforeach;
?>
</ul>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-21
    • 1970-01-01
    • 2015-10-10
    相关资源
    最近更新 更多