• It’s a common practice to append a brief biographical profile about an author at the end of an article. Here are some code snippets that model ways to do this.

    As discussed in this thread, here is Matt’s (corrected) code for displaying byline descriptions in a list below a post:

    <?php $terms = get_the_terms($post->ID, 'byline');
                         $count = count($terms);
                         if ( $count > 0 ){
                             echo "<ul>";
                             foreach ( $terms as $term ) {
                               echo "<h6>About " . $term->name . "</h6>";
                               echo "<p>" . $term->description . "</p>";
                             }
                             echo "</ul>";
                         }
    ?>

    The following snippet works a little differently. It first checks if the actual user-author has a bio filled out in their user profile. If they do, it’s displayed. Then if there are any byline co-authors with bios, these are put into an array separated by a tag (or whatever you want) and then displayed. This is useful if you’re just using Byline for Co-Authors and still want to use the WP user-author.

    <?php if(is_single()) : ?>
    	<div class="entry-author-bio">
    	<!-- If Author is User with Bio, display it -->
    	<?php if ( is_singular() && $author_bio = get_the_author_meta( 'description' ) ) : ?>
    		<!-- Insert your code for displaying normal user-author bylines here. -->
    	<?php endif; ?>
    	<!-- Now get and display any guest-author bylines. -->
    		<?php global $post;
    			$terms = get_the_terms($post->id, 'byline');
    			$guest_author_bio = array();
    			foreach ($terms as $term) {
    				$guest_author_bio[] = $term->description;
    				}
    			$guest_author_bio = join( "<br> ", $guest_author_bio);
    			echo $guest_author_bio;
    			?>
    		</div>
    	<?php endif;>

    https://wordpress.org/plugins/byline/

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter Dan Knauss

    (@dpknauss)

    FYI, appending these code snippets to theme template files means placing the author bio content after the main post content and not as part of it. For this reason the bios will not be included in the post XML output for feeds.

    If you need to inject the Byline term and description fields into the actual post content, you will need to use this filter:
    http://codex.wordpress.org/Plugin_API/Filter_Reference/the_content

    Thread Starter Dan Knauss

    (@dpknauss)

    Some additional helpful resources if you are trying to display your Byline description…

    Discussion of how this works in WP: http://wordpress.org/support/topic/get-the-current-category-taxonomy-term-name?replies=12

    WP Codex function reference for the key funtion you need — get_the_terms:
    http://codex.wordpress.org/Function_Reference/get_the_terms

    Plugin Author mattdu

    (@mattdu)

    Great point Dan. I did not consider feed output with this solution.

    Thread Starter Dan Knauss

    (@dpknauss)

    Really? Your code ends with a couple of checks that are intended to handle feeds. I couldn’t figure out why they don’t work, but I came up with a way to make them work:

    //Display the byline by replacing instances of the_author throughout the site.
    add_filter( 'the_author', 'byline' );
    add_filter( 'get_the_author_display_name', 'byline' );
    
    function byline( $name ) {
    	global $post;
    	// $guest_author_link is the guest author name linked to their byline (term archive) page.
    	$guest_author_link = get_the_term_list( $post->ID, 'byline', '', ', ', '' );
    	// $guest_author is the guest author name and nothing else.
    	$guest_author = strip_tags( $guest_author_link );
    
    	if (is_feed() ) {
    		$name = $guest_author;
    	} else {
    		$name = $guest_author_link;
    	}
    	return $name;
    
    }
    Thread Starter Dan Knauss

    (@dpknauss)

    Sorry, that was wrong. This should be closer to right:

    function byline( $name ) {
    	global $post;
    	// If one exists, get the guest author name+link to their byline (term archive) page.
    	$guest_author_link = get_the_term_list( $post->ID, 'byline', '', ', ', '' );
    	// Get guest author name without link/stripped of markup.
    	$guest_author = strip_tags( $guest_author_link );
    	// If there is a guest author, we're on the front end, and we're not requesting a feed, then return the linked name/s.
    	if ( $guest_author_link && !is_admin() && !is_feed() ) {
    		$name = $guest_author_link;
    	// If we're requesting a feed and there is at least one guest author, then use the plain text name/s.
    	} elseif ( $guest_author_link && is_feed() ) {
    		$name = $guest_author;
    	}
    	return $name;
    
    }
    Thread Starter Dan Knauss

    (@dpknauss)

    The only problem I see with this is that it’s not valid markup for the feed. It results in something like this with more than one author:
    <dc:creator><![CDATA[Another Author, First Author]]></dc:creator>

    To fix this I think you’d have change how the feed is generated and make it loop through the Byline terms, adding a separate <dc:creator> tags for each.

    Thread Starter Dan Knauss

    (@dpknauss)

    Here’s a better version of the crappy code I first posted in this thread for listing authors, as explained in the comments. It shows the normal User-Author name and bio below the post if they exist, followed by any Guest Author/s (Byline terms) if they exist. So if you want to use the Byline names as co-authors in addition to what WP considers the actual post author, this is one way to do it — just give each post author a bio in their profile.

    <div class="entry-author-bio">
    	<?php global $post;
                   // Get the User-Author biographical information from their profile, an optional field.
    	$user_author_bio = get_the_author_meta( 'description' );
    	// If the User-Author biographical information field is not empty, get the user nickname, a required field, and display both.
    	if ( !empty ($user_author_bio) ) {
    		$user_author_name = get_the_author_meta( 'nickname' );
    		echo "<h6 class=\"author-bio-header\">About " . $user_author_name . "</h6>";
    		echo "<p class=\"author-bio\">" . wpautop( ttfmake_sanitize_text( $user_author_bio ) ) . "</p>";
    	}
    	// Get the Guest Author (Byline) terms.
    	$terms = get_the_terms($post->ID, 'byline');
    	// Get the number of Guest Author (Byline) terms that exist.
    	$count = count($terms);
    	// If there are one or more Guest Authors (Byline terms), display them.
    	if ( $count > 0 ){
    		foreach ( $terms as $term ) {
    			echo "<h6 class=\"author-bio-header\">About " . $term->name . "</h6>";
    			echo "<p class=\"author-bio\">" . $term->description . "</p>";
    		}
    	}
    	?>
    </div>

    Including this code at the end or just below the post content is where it makes sense. Any listing of authors at the top of the post for a traditional byline needs to follow the same format to make sense to readers.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to display Byline term descriptions as author biographies.’ is closed to new replies.