0

I have an author list that I include on every page template. This author list shows the authors details and photo. I also show the author images on many different place on the different page templates. This use to work well until I changed the way I get the author details.

I reverted back to the old method I used that worked but my author images just doesn't show up. It's still works in the author list but the image on the template pages gives me the error variable currentauth doesn't exist.

What am I missing?

Debug Info:

NOTICE: wp-content/themes/ewarch/single-project_post.php:81 - Undefined variable: curauth

NOTICE: wp-content/themes/ewarch/single-project_post.php:81 - Trying to get property of non-object

NOTICE: wp-content/themes/ewarch/single-project_post.php:82 - Undefined variable: curauth

NOTICE: wp-content/themes/ewarch/single-project_post.php:82 - Trying to get property of non-object –

AUTHOR_LIST:

<div class="author-list">
<div class="author-list-inner">

    <?php

        // Get the authors from the database ordered randomly
        global $wpdb;
        $auth_query = "SELECT ID, user_nicename from $wpdb->users WHERE ID != '1' ORDER BY 'ASC' LIMIT 20";
        $author_ids = $wpdb->get_results($auth_query);

        // Loop through each author
        foreach($author_ids as $author) :

            // Get user data
            $curauth = get_userdata($author->ID);

            // Get link to author page
            $user_link = get_author_posts_url($curauth->ID);
            $post_link = get_permalink($curauth->ID);


            // Set default avatar (values = default, wavatar, identicon, monsterid)
            $main_profile = get_the_author_meta('mainProfile', $curauth->ID);
            $hover_profile = get_the_author_meta('hoverProfile', $curauth->ID);
            $award_profile = get_the_author_meta('awardProfile', $curauth->ID);

    ?>

            <div class="da-author">

                <div class="original-image">
                    <img src="<?php echo $main_profile; ?>" alt="<?php echo $curauth->display_name; ?>">
                </div>

                <div class="hover-image">
                    <a href="<?php echo $user_link; ?>">
                        <img src="<?php echo $hover_profile; ?>">
                    </a>
                </div>

            </div>

    <?php endforeach; ?>
    <?php wp_reset_postdata(); ?>

</div>

PAGE_TEMPLATE:

<div id="projects-single" class="<?php if (get_field('awards')): ?> award-winner <?php endif; ?> js-height">
    <div class="author-container hide-for-medium-down start-closed">
        <?php get_template_part( 'author_list' ); ?>
    </div>
    <div class="small-12 large-12 columns">


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

                <div class="large-set">
                    <header>
                        <h2><?php the_title(); ?></h2>

                        <?php if (get_field('awards')): ?>
                            <h3><?php the_field('award_title'); ?></h3>
                        <?php endif; ?>

                        <div id="author-meta" class="show-for-small-only">
                            <p><?php the_author_posts_link(); ?> was involved with <?php the_title(); ?></p>
                        </div>
                    </header>

                    <div class="show-for-small-only">
                        <hr>
                    </div>

                    <div class="entry-content">
                        <?php the_content(); ?>
                    </div>

                    <div class="author-set hide-for-small">
                        <div class="author-project-image">
                            <img src="<?php echo get_the_author_meta('mainProfile', $curauth->ID); ?>" class="no-award">
                            <img src="<?php echo get_the_author_meta('awardProfile', $curauth->ID); ?>" class="award">
                        </div>
                        <div class="author-project-blurb">
                            <h3><?php the_author_posts_link(); ?></h3>
                            <p>I was part of the design team on the <?php the_title(); ?> project.</p>
                        </div>
                    </div>
                </div>
            </article>


        <?php endwhile; ?>


    </div>
</div>

THIS IS THE PART WHERE I SHOW THE IMAGES:

<img src="<?php echo get_the_author_meta('mainProfile', $curauth->ID); ?>" class="no-award">
                            <img src="<?php echo get_the_author_meta('awardProfile', $curauth->ID); ?>" class="award">
  • I would suggest to set debug to true in wp-config.php. This usually helps a lot in determining the cause of a problem. – Pieter Goosen Mar 26 '14 at 7:18
  • Hi Pieter, My debug is set to true and I have the debug toolbar installed as well. It gives me these warnings. NOTICE: wp-content/themes/ewarch/single-project_post.php:81 - Undefined variable: curauth NOTICE: wp-content/themes/ewarch/single-project_post.php:81 - Trying to get property of non-object NOTICE: wp-content/themes/ewarch/single-project_post.php:82 - Undefined variable: curauth NOTICE: wp-content/themes/ewarch/single-project_post.php:82 - Trying to get property of non-object – wind_kind Mar 26 '14 at 7:25
  • I've edited your question to show the debug info. It is always a plus if you add such info in your question – Pieter Goosen Mar 26 '14 at 7:31
0

Your problem is totally related with pure PHP, not Wordpress. You must understand that includes DO NOT break the variable scope. So, the $curauth is only life in author_list.php file and, more specifically, only life inside the foreach($author_ids as $author) loop of that file.

In your page template you could use the_author_meta() to get the author meta fields of current post inside the loop instead of trying to access to a non-defined variable:

 <img src="<?php the_author_meta('mainProfile'); ?>" class="no-award">
 <img src="<?php the_author_meta('awardProfile'); ?>" class="award">

I also want to recommend you to use WP_User_Query cloass and methods in your author_list instead of write a plain SQL query. Also, you should remove the unnecesary <?php wp_reset_postdata(); ?>.

  • Thanks cybnet, your answer works. I'm actually trying to get the author_list to do a lot more than just show the images. I have another question here that I'm trying to get sorted and although I'm sure that should work, it doesn't and I don't know why. wordpress.stackexchange.com/questions/138923/… – wind_kind Mar 26 '14 at 8:41

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.