类别页面上未显示作者姓名

时间:2018-09-26 作者:Varsha Dhadge

在我的分类页面上,未显示文章作者姓名

这就是我正在使用的

<?php echo get_the_author_meta(\'display_name\', $author_id); ?>
这是我的循环

<div class="a-article-wrapper row">
            <?php 
              $cat_name = \'category\';
              $categories = get_the_terms( $post->ID, $cat_name );
              $top_level_terms = get_the_terms( $post->ID, $cat_name );
              foreach( $top_level_terms as $top_term ) 
              {
                if(!($top_term->parent))
                {
                  $child_terms = get_terms( $cat_name, array( \'child_of\' => $top_term->term_id ));
                  //Parent Category Name
                  //echo \'<div class="maincat-HC">\'.$top_term->name .\'</div>\'; 
                  $top_id=$top_term->term_id;
                  if(count($child_terms)>0)
                  {
                    foreach ( $child_terms as $child_term ) 
                    {
                      $id=$child_term->term_id;
                      //Child category name
                      //echo \'<ul class="hc-accordion"><li><div class="hc-subcat">\' .$child_term->name .\'</div>\';

                      $myposts=get_posts(array(
                        \'post_type\' => \'post\',
                        \'nopaging\' => true,
                        \'tax_query\' => array(
                        array(
                        \'taxonomy\' => $cat_name,
                        \'order\' => \'ASC\',
                        \'field\' => \'term_id\',
                        \'terms\' => $id)
                        ))
                      );

                      foreach ($myposts as $mypost) { ?>




                        <div class="col-md-4 a-article-row">
                                    <?php 
                                      echo get_the_author_meta(\'display_name\', $author_id); 
                                    ?>
                            <?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($mypost->ID), \'full\' );?>
                            <?php if(has_post_thumbnail()) { ?>
                                <div class="archive-thumb-img" style="background-image: url(\'<?php echo $thumb[\'0\'];?>\')"></div>
                            <?php  } else { ?>
                                <div class="archive-thumb-img"></div>
                            <?php } ?>

                           <div class="article-detail">
                               <div class="category">
                                    <?php

                                        foreach($categories as $category) {
                                            if($category->parent){
                                                echo $category->name;
                                            }
                                        }  
                                    ?> 

                                </div>
                                <div class="a-article-title">
                                    <a href="<?php the_permalink($mypost);?>" title="<?php the_title(); ?>">
                                        <?php echo mb_strimwidth( get_the_title(), 0, 45, \'...\' );  ?> 
                                    </a>
                                </div>
                                <div class="author">
                                    <?php echo get_the_author_meta(\'display_name\', $author_id); ?>
                                </div>
                           </div>
                        </div>

                      <?php } wp_reset_postdata();
                    } 
                  }
                }
              }
            ?>
          </div>

1 个回复
最合适的回答,由SO网友:Jrod 整理而成

您需要正确地传入作者ID。

该行:

<?php echo get_the_author_meta(\'display_name\', $author_id); ?>

应该是这样的:

<?php echo get_the_author_meta( \'display_name\', $mypost->post_author ); ?>

或者,您可以使用setup_postdata() 在foreach循环中,设置全局$post变量并使用模板标记函数,而无需传入ID。

global $post;

foreach ( $myposts as $post ) { setup_postdata( $post ); ?>

    <div class="col-md-4 a-article-row">
                <?php 
                  echo get_the_author_meta(\'display_name\'); 
                ?>
        <?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($mypost->ID), \'full\' );?>
        <?php if(has_post_thumbnail()) { ?>
            <div class="archive-thumb-img" style="background-image: url(\'<?php echo $thumb[\'0\'];?>\')"></div>
        <?php  } else { ?>
            <div class="archive-thumb-img"></div>
        <?php } ?>

       <div class="article-detail">
           <div class="category">
                <?php

                    foreach($categories as $category) {
                        if($category->parent){
                            echo $category->name;
                        }
                    }  
                ?> 

            </div>
            <div class="a-article-title">
                <a href="<?php the_permalink($mypost);?>" title="<?php the_title(); ?>">
                    <?php echo mb_strimwidth( get_the_title(), 0, 45, \'...\' );  ?> 
                </a>
            </div>
            <div class="author">
                <?php echo get_the_author_meta(\'display_name\', $author_id); ?>
            </div>
       </div>
    </div>

<?php } wp_reset_postdata();

结束

相关推荐

Can you set a role as author?

我是WordPress的新手,我正在尝试找出如何最好地设置权限。我想限制人员组只编辑某些页面(IT部门只编辑IT页面,人力资源部门只编辑人力资源页面等等)。据我所知,您取消了角色“编辑其他页面”的权限,然后将页面的作者设置为您要编辑的人。是否有一种设置多个作者的标准方法,或者是否有一种使角色成为作者而不是单个用户的方法?谢谢