0

On my category page post author name is not getting displayed

This is what I am using

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

Here is my loop

<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
    Do you have a custom theme file for category pages? If so, can you share the code for your loop? – Pim Sep 26 '18 at 14:14
  • Updated my question – Varsha Dhadge Sep 26 '18 at 14:16
3

You need to pass in the author ID properly.

This line:

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

Should be this:

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

Alternatively you could use setup_postdata() in your foreach loop to setup the global $post variable and make use of the template tag functions without having to pass in the IDs.

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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.