0

Using get_template_part() I parted some of my theme's common post templates. My post template is content-general.php. Without parting thing is going fine. But just after parting the template — using WP_DEBUG, true — I discovered it's showing some errors in loading author information:

Notice: Undefined variable: authordata

Along with:

Notice: Trying to get property of non-object

I have the following code, where I have $authordata:

<a class="url fn n" href="<?php echo get_author_posts_url( false, $authordata->ID, $authordata->user_nicename ); ?>" title="<?php printf( __( 'View all posts by %s', 'your-theme' ), $authordata->display_name ); ?>">
   <?php the_author(); ?>
</a>

I followed this WPSE thread and tried globalizing $post inside the template file (content-general.php) like:

<?php global $post; ?>

and the template is called within a default WordPress loop. But the problem is not solved.

1 Answer 1

1

With this answer with good practices, by Chip Bennett, in mind just do a simple global thing — add global $authordata to your template file:

<?php global $authordata; ?>

Follow the Codex's Global Variables article for details about the global practice.

Quoting the portion specific to the Question:

$authordata (object) Returns an object with information about the author, set alongside the last $post.

2
  • Have you tried simply calling the_post() up front in the template part?
    – kaiser
    Oct 27, 2013 at 12:22
  • I have the_post() in the index file, with the loop: <?php while ( have_posts() ) : the_post(); get_template_part( 'content','general' ); endwhile; ?> - till we need to define it again, in the template part? If I do so, then last post (LIFO) got vanish. Oct 27, 2013 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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