【问题标题】:Display only author posts in author page in Wordpress在 Wordpress 的作者页面中仅显示作者帖子
【发布时间】:2018-07-02 08:19:12
【问题描述】:

我应该怎么做才能在我的 author.php 页面中只显示某个作者的帖子?我不是指特定的作者(使用作者 ID),而是作者的默认页面。使用当前代码,它显示来自所有其他作者的帖子。我应该使用特定查询吗?

<?php   $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    $custom_args = array(
    'post_type' => 'post',
    // 'order' => 'DESC',
    'posts_per_page' => 10,
        'paged' => $paged
    );

// custom query
$recent_posts = new WP_Query($custom_args);

// check that we have results
if($recent_posts->have_posts()) : ?>

<ul class="article_list">

    <?php
    // start loop
    while ($recent_posts->have_posts() ) : $recent_posts->the_post(); ?>

    <li class="regular">
        <a href="<?php echo get_permalink(); ?>">
            <div class="text">
                <p class="category"><?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?></p>
                <p class="autor"><?php the_author(); ?></p>
                <h3 class="article_title"><?php echo mb_strimwidth(get_the_title(), 0, 80, '...'); ?></h3>
                <p class="date"><?php echo get_the_date( 'Y-m-d' ); ?></p>
            </div>
            <div class="mask">
              <?php
              $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
              echo '<div class="art_img" style="background: url('. $url.')"></div>';
              ?>
            </div>
        </a>
    </li>
<?php endwhile; ?>
</ul>
<?php endif;
echo '<div class="pagination">';
if(function_exists('wp_pagenavi')) { wp_pagenavi(array( 'query' => $recent_posts )); }
echo '</div>';
wp_reset_postdata();
?>

【问题讨论】:

    标签: wordpress author


    【解决方案1】:

    你可以使用wordpress的get_posts功能。

    这里是参考: https://codex.wordpress.org/Template_Tags/get_posts

    在参数列表中传递所需的作者 ID。

    【讨论】:

      【解决方案2】:

      您好,只需在您提供的 sn-p 顶部的 $paged 变量上方添加 global $current_user; 并在您的 $custom_args 数组中添加 'author' =&gt; $current_user-&gt;ID,,这样它就会在作者页面中显示特定用户的帖子

      【讨论】:

      • 它只对当前登录的用户有效,对其他作者无效。
      【解决方案3】:

      将 author__in 添加到您的查询中

      试试这个

      <?php $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
          $custom_args = array(
          'post_type' => 'post',
          'author__in'=> array(2,4,6), //Authors's id's you like to include
          // 'order' => 'DESC',
          'posts_per_page' => 10,
          'paged' => $paged
      );
      

      【讨论】:

      • 我是否总是必须使用作者 ID?有没有办法使用查询来调用我点击的作者的帖子?
      猜你喜欢
      • 1970-01-01
      • 2021-04-11
      • 1970-01-01
      • 2012-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-11
      • 1970-01-01
      相关资源
      最近更新 更多