• Resolved alexleonard

    (@alexleonard)


    Right, I’ve been having some fun and games with a templating issue for the last while and I have a feeling I’m just missing something really obvious..

    I am helping a mate out developing a record label website. The way I’ve created it is that every artist on the record label is a contributor user and this has presented the additional advantage of having some nice ways of laying out the artist pages (using the author template page).

    The artist page itself needs a bit of work yet, but you can see from the below that I’m going to be showing their releases (posts) at the end of their page:

    https://devel.invisibleagent.com/author/corrugated/

    The idea is to repeat this listing of their releases on each release page (eg: Other Releases By This Artist).

    This should appear before the comments area, but everything I do seems to result in some crazy php neverending looping.

    Here’s a sample of the release page:
    https://devel.invisibleagent.com/2006/02/22/corrugated-tunnel-we-are-electronix/

    I know that I can use the_author_posts and the_author_posts_link to display how many posts the author has made, but what I want to do is actually create a loop accessing items published by an author with a specific ID which I’ll grab from the single post loop.

    The way I have things setup is that this would allow me to grab custom meta fields (eg album covers) and just display them.

    Any thoughts?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter alexleonard

    (@alexleonard)

    Woo! I sorted it out.

    It all depended on use of WP_Query which I wasn’t very familiar with.

    This opens up a whole realm of possibilities ??

    Of course, as usual, if anyone has a suggestion of tidier code, that’d be legend. I might turn this into a function to make my template files a little neater (and give me the option to use this elsewhere – such as on the author page)

    <?php
    $otherReleases = new WP_query();
    
    $otherReleases->query(array('orderby'=>date,'cat'=>15,'author'=>$post->post_author,'post__not_in'=>array($post->ID)));
    
    if ($otherReleases->have_posts()) :
    ?>
    <h3>Other releases by this artist</h3>
    <ul class="other-releases">
    <?php while ($otherReleases->have_posts()) : $otherReleases->the_post(); ?>
     <li>
     //get other release info
     </li>
    <?php endwhile; ?>
    </ul>
    <?php endif; ?>

    In action here: https://devel.invisibleagent.com/2006/02/22/corrugated-tunnel-we-are-electronix/

    As you can see it displays all my normal loop stuff, and then at the end, as there are other releases (cat15) by this ‘author’ it displays what they are. Still needs styling etc, but the camel’s back is broken now..

    Thread Starter alexleonard

    (@alexleonard)

    Right, after messing around with things for the whole night, I’ve now written a function to allow me to access the same sort of display on the author page.

    I thought I’d throw it in here just in case anyone had any suggestions for improvement.

    The function call is set up like this:

    <?php get_author_releases($post->post_author, $post->ID); ?>

    on single.php such as: https://devel.invisibleagent.com/artist/corrugated/

    and

    <?php get_author_releases($post->post_author); ?>
    on the author.php template such as: https://devel.invisibleagent.com/2006/02/22/corrugated-tunnel-we-are-electronix/

    and the function looks like:

    function get_author_releases($author, $current=NULL) {
    
    global $post;
    global $curauth;
    
    $user_info = get_userdata($author);
    $releasecover = "No Cover";
    $releaseformat = "Format Unknown";
    
    $otherReleases = new WP_query();
    
    // Check if the $current parameter is assigned
    // if it is then the function is being used on a release page and should not display the current release - only any other releases
    // otherwise display all releases
    if ( !empty ($current) ) {
      $otherReleases->query(array('orderby'=>date,'cat'=>15,'author'=>$author,'post__not_in'=>array($current)));
      $intro = "Other releases by ";
    } else {
      $otherReleases->query(array('orderby'=>date,'cat'=>15,'author'=>$author,));
      $intro = "Releases by ";
    }
    
    // If there are any posts to display, display heading and start UL
    if ($otherReleases->have_posts()) :
      echo "<h2>" . $intro . $user_info->nickname . "</h2>\r\n";
      echo "<ul class='other-releases'>\r\n";
    
    // Begin loop
    while ($otherReleases->have_posts()) : $otherReleases->the_post();
    
      echo "<li>\r\n";
      echo "<a href='" . get_permalink() . "' rel='bookmark' title='Link to " .  $post->post_title . "'>";
      $releasecover = get_post_meta($post->ID, "release-cover", true);
      if (!empty($releasecover)) {echo $releasecover;}
      echo "</a>";
      echo "<h3 class='post-title'>" .  $post->post_title . "</h3>\r\n";
      echo "<p>";
      $releaseformat = get_post_meta($post->ID, "release-format", true);
      if (!empty($releaseformat)) {echo $releaseformat . " / ";}
      echo the_time('Y') . "</p>";
      echo "<p class='release-link'><a href='" . get_permalink() . "'  rel='bookmark' title='Link to " . $post->post_title . "'>View Details</a></p>\r\n";
      echo "</li>\r\n";
      endwhile;
      echo "</ul>\r\n";
      endif;
    }

    At first I tried to assign all my content to a variable and just return it, but that was a disaster. I had some fun and games then trying to find out what I needed to call as $post->etc but anyway, got there eventually. I got to get me some sleep!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Listing “other” Author posts in single.php loop’ is closed to new replies.