0

I have an author box outside of the loop that is supposed to grab the name of the author, but the correct author isn't being displayed most likely because these tags only work inside the loop. What can I change in this code to make it work outside of the loop?

    <div id="author-bio">
        <div class="inner">
            <h3>About <?php echo get_the_author(); ?></h3>
            <?php echo get_avatar( get_the_author_meta( 'user_email' ), 70 ); ?>
            <div class="author-info">
                <p><?php the_author_meta( 'description' ); ?></p>
                <p><a class="author-link" href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>" rel="author">View all posts by <?php echo get_the_author(); ?> <span class="meta-nav">&rarr;</span></a></p>
            </div>
        </div>
    </div>

Full code for single.php below:

<?php get_header(); ?>

    <!--BEGIN #content -->
    <div id="content">

        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

        <!--BEGIN .hentry -->
        <div <?php post_class(); ?> id="post-<?php the_ID(); ?>">

            <!--BEGIN .post-header-->
            <div class="post-header">

                <div class="inner">

                    <span class="meta-category"><?php the_category(', '); ?></span>

                    <h1 class="post-title">

                        <?php the_title(); ?>

                        <?php $format = get_post_format(); ?>
                        <?php if ($format == "image" || $format == "gallery" || $format == "video") : ?>
                        <span class="icon"><img src="<?php echo get_template_directory_uri(); ?>/images/icon-<?php echo $format; ?>.png" alt="<?php echo $format; ?>" /></span>
                        <?php endif; ?>

                    </h1>

                    <span class="meta-published"><?php _e('Posted', 'engine') ?> <?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' '.  __('ago', 'engine'); ?></span>
                    <span class="meta-author"><?php _e('by', 'engine') ?> <?php the_author_posts_link(); ?> <a class="post-twitter" href="http://twitter.com/<?php the_author_meta('twitter') ?>" target="_blank"><img src="<?php echo get_stylesheet_directory_uri(); ?>/images/twitter-icon.png" /></a></span>

                </div>

            <!--END .post-header -->
            </div>

            <!--BEGIN .featured-image -->
            <div class="featured-image <?php echo get_post_format(); ?>">

                <?php if (get_post_format() == 'video' && get_post_meta(get_the_ID(), 'dt_video', true) != '') : ?>

                    <?php
                    global $wp_embed;
                    $video_url = get_post_meta(get_the_ID(), 'dt_video', true);
                    $video_embed = $wp_embed->run_shortcode('[embed width="700"]'.$video_url.'[/embed]');
                    ?>
                    <div id="video-<?php the_ID(); ?>"><?php echo $video_embed; ?></div>

                <?php elseif (get_post_format() == 'gallery') : ?>

                    <!--BEGIN #slides -->
                    <div id="single-slides" class="clearfix">

                        <?php

                        $args = array(
                            'orderby'        => 'menu_order',
                            'order'          => 'ASC',
                            'post_type'      => 'attachment',
                            'post_parent'    => get_the_ID(),
                            'post_mime_type' => 'image',
                            'post_status'    => null,
                            'numberposts'    => -1,
                        );

                        $attachments = get_posts($args);

                        ?>

                        <?php if ($attachments) : ?>

                        <div class="slides_container">

                        <?php foreach ($attachments as $attachment) : ?>

                            <?php
                                $format = get_post_format();
                                $src = wp_get_attachment_image_src( $attachment->ID, array( '9999','9999' ), false, '' );
                                $src = $src[0];
                                $image = dt_resize($attachment->ID, $src, 700, '', true);
                                $caption = $attachment->post_excerpt;
                            ?>

                            <div>

                                <span class="overlay-icon overlay-<?php echo $format; ?>"><a rel="group-<?php the_ID(); ?>" title="<?php echo $attachment->post_title; ?>" class="colorbox-<?php echo $format; ?>" href="<?php echo $src; ?>"></a></span>

                                <img
                                height="<?php echo $image['height']; ?>"
                                width="<?php echo $image['width']; ?>"
                                alt="<?php echo apply_filters('the_title', $attachment->post_title); ?>"
                                src="<?php echo $image['url']; ?>"
                                />

                                <?php if($caption != '') : ?>
                                <span class="caption"><?php echo $caption; ?></span>
                                <?php endif; ?>

                            </div>

                        <?php endforeach; ?>

                        </div>

                        <!--BEGIN .slide-cntrols-->
                        <div id="slide-controls">

                            <a href="#" class="next">Next</a>
                            <a href="#" class="prev">Prev</a>

                        <!--END .slide-cntrols-->
                        </div>

                        <?php endif; ?>

                    <!--END #slides -->
                    </div>

                <?php elseif (has_post_thumbnail() && get_option('dt_blog_image') != 'false') : ?>

                    <?php 
                    $thumb = get_post_thumbnail_id(get_the_ID());
                    $image = dt_resize( $thumb, '', 700, '', true );
                    echo '<img src="'.$image['url'].'" width="'.$image['width'].'" height="'.$image['height'].'" alt="" />';
                    ?>

                <?php endif; ?>

            <!--END .featured-image -->
            </div>

            <!--BEGIN .post-content -->
            <div class="post-content">
                <?php the_content(); ?>
            <!--END .post-content -->
            </div>

            <!--BEGIN .post-footer-->
            <div class="post-footer">

                <span class="meta-published"><?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' '.  __('ago', 'engine'); ?></span>

                <span class="meta-comments"><?php comments_number(__('No Comments', 'engine'), __('1 Comment', 'engine'), __('% Comments', 'engine')); ?></span>

            <!--END .post-footer-->
            </div>

        <!--END .hentry-->
        </div>

        <div id="next-prev-posts">
            <?php
                $prevPost = get_previous_post(); 
                if(isset($prevPost->ID))
                $prevthumbnail = get_the_post_thumbnail($prevPost->ID, array(100,100) );
                $nextPost = get_next_post(); 
                if(isset($nextPost->ID))
                $nextthumbnail = get_the_post_thumbnail($nextPost->ID, array(100,100) ); 
                $default = '<img src="'.get_stylesheet_directory_uri().'/images/isa.jpg" />';
            ?>

            <div class="inner">
                <div class="nav-next">
                    <?php
                        if ($nextPost && '' != get_the_post_thumbnail($nextPost->ID, array(100,100))) { 
                            next_post_link('%link', $nextthumbnail, false); 
                            next_post_link('%link', '<span class="meta-nav">«</span> %title', false); 
                        } elseif ($nextPost) {
                            next_post_link('%link', $default, false); 
                            next_post_link('%link', '<span class="meta-nav">«</span> %title', false);
                        } else {
                            print '<p>' . __( 'You are at the beginning!' ) . '</p>';
                        }   
                    ?>
                </div>
                <div class="nav-previous">
                    <?php
                        if ($prevPost && '' != get_the_post_thumbnail($prevPost->ID, array(100,100))) { 
                            previous_post_link('%link', '%title <span class="meta-nav">»</span> ', false);
                    previous_post_link('%link', $prevthumbnail, false); 
                        } elseif ($prevPost) {
                    previous_post_link('%link', '%title <span class="meta-nav">»</span> ', false);
                            previous_post_link('%link', $default, false);      
                        } else {
                            print '<p>' . __( 'You are at the end!' ) . '</p>';
                        }   
                    ?>
                </div>
            </div>
        </div> 

        <!--BEGIN #related--><?php $related = get_option('dt_related');

        if($related == 'true') : ?>


        <div id="related" class="clearfix">
            <div class="inner">
                <h3>Recommended</h3>

                <?php

                $related = dt_get_posts_related_by_taxonomy(get_the_ID(), 'category', get_the_ID());

                ?>

                <ul>

                    <?php  if($related->have_posts()) : while ($related->have_posts()) : $related->the_post(); ?>

                    <!--BEGIN li -->
                    <li id="featured-post-<?php the_ID(); ?>">

                        <div class="featured-image">
                            <div class="da-hover">
                                <span class="da-wrap">
                                    <span class="title"><?php the_title(); ?></span>
                                    <?php dt_overlay_icon(); ?>
                                </span>
                            </div>
                            <a href="<?php the_permalink(); ?>"><?php dt_image(200, ''); ?></a>
                        <!--END .featured-image -->

                    <!--END li-->
                    </li>

                    <?php endwhile;  else: ?>
                        <p><?php _e('There are no related posts.', 'engine'); ?></p>
                    <?php endif; ?>

                </ul>

            </div>
        </div>

        <?php endif; ?><!--END #related-->

        <div id="author-bio">
            <div class="inner">
                <h3>About <?php echo get_the_author(); ?></h3>
                <?php echo get_avatar( get_the_author_meta( 'user_email' ), 70 ); ?>
                <div class="author-info">
                    <p><?php the_author_meta( 'description' ); ?></p>
                    <p><a class="author-link" href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>" rel="author">View all posts by <?php echo get_the_author(); ?> <span class="meta-nav">&rarr;</span></a></p>
                </div>
            </div>
        </div>

        <div id="comments">
            <div class="inner">
                <div id="disqus_thread"></div>
                    <script type="text/javascript">
                    /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
                    var disqus_shortname = 'isatv'; // required: replace example with your forum shortname

                    /* * * DON'T EDIT BELOW THIS LINE * * */
                    (function() {
                        var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
                        dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
                        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
                    })();
                    </script>
                    <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
            </div>
        </div>

        <?php endwhile; else : ?>

            <p><?php _e('No posts found', 'engine'); ?></p>

        <?php endif; ?>

    </div><!-- #content -->

<?php get_sidebar(); ?>

<?php get_footer(); ?>
  • Where are you using this code – Pieter Goosen Jun 26 '14 at 7:22
  • @PieterGoosen On my single page template, outside of the loop. – Desi Jun 26 '14 at 7:24
  • Why don't you move it to inside the loop as it should be – Pieter Goosen Jun 26 '14 at 7:26
  • @PieterGoosen I moved it into the loop, but it's grabbing the wrong author. The post was written by A but in the author box it's showing B. I included the full code in the post in case you'd like to see. – Desi Jun 26 '14 at 7:42
  • 1
    Remember to reset your related posts' query with wp_reset_postdata(); – Pieter Goosen Jun 26 '14 at 15:45
1

For your author box to work, it needs to be inside the loop. You have one fundamental flaw though with your second loop. You are currently using two different loops in one, the first one being the main loop, and the second one a custom loop to fetch related posts

As it stand, and where you placed your author box, it is getting its information from the second loop, and not the main loop, that is why your author that is returned is wrong

To solve this, you will need to reset your second loop. Always reset a custom query (like the one you've used) which you've created with get_posts or WP_Query. To do this, you'll have to call wp_reset_postdata after your loop is done. Your second loop should look like this

<!--BEGIN #related--><?php $related = get_option('dt_related');

    if($related == 'true') : ?>


    <div id="related" class="clearfix">
        <div class="inner">
            <h3>Recommended</h3>

            <?php

            $related = dt_get_posts_related_by_taxonomy(get_the_ID(), 'category', get_the_ID());

            ?>

            <ul>

                <?php  if($related->have_posts()) : while ($related->have_posts()) : $related->the_post(); ?>

                <!--BEGIN li -->
                <li id="featured-post-<?php the_ID(); ?>">

                    <div class="featured-image">
                        <div class="da-hover">
                            <span class="da-wrap">
                                <span class="title"><?php the_title(); ?></span>
                                <?php dt_overlay_icon(); ?>
                            </span>
                        </div>
                        <a href="<?php the_permalink(); ?>"><?php dt_image(200, ''); ?></a>
                    <!--END .featured-image -->

                <!--END li-->
                </li>

                <?php endwhile;  wp_reset_postdata(); else: ?>
                    <p><?php _e('There are no related posts.', 'engine'); ?></p>
                <?php endif; ?>

            </ul>

        </div>
    </div>

    <?php endif; ?><!--END #related-->
| improve this answer | |
  • I appreciate the explanation! – Desi Jun 28 '14 at 6:50

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.