1

I have a custom taxonomy called "Sources" (using Custom Post Type UI), where the sources are the names of publications where articles were originally published. Each post is an article, with a source selected. What I need to do is to show the source in the metadata under the post title. I'm using the Avada theme. Currently the title area of a post looks like this:

Article Title

By Author Name | Month, Year

What I want is this:

Article Title

Author Name, Source, Month, Year

Here's an example post I'm working with in my dev environment:

http://dev.universaltheosophy.com/articles/the-logos-and-meditation/

I'd love to be able to do this with add_action in my child theme's functions.php, and hook it before the date or after the author name. But I'm not sure how to do that.

All I see in the theme's single post file file is:

<?php if ( 'below' === Avada()->settings->get( 'blog_post_title' ) ) : ?>
            <?php if ( 'below_title' === Avada()->settings->get( 'blog_post_meta_position' ) ) : ?>
                <div class="fusion-post-title-meta-wrap">
            <?php endif; ?>
            <?php $title_size = ( false === avada_is_page_title_bar_enabled( $post->ID ) ? '1' : '2' ); ?>
            <?php echo avada_render_post_title( $post->ID, false, '', $title_size ); // phpcs:ignore WordPress.Security.EscapeOutput ?>
            <?php if ( 'below_title' === Avada()->settings->get( 'blog_post_meta_position' ) ) : ?>
                <?php echo avada_render_post_metadata( 'single' ); // phpcs:ignore WordPress.Security.EscapeOutput ?>
                </div>
            <?php endif; ?>
        <?php endif; ?>

And I see in the theme's files:

            // Render author meta data.
        if ( $settings['post_meta_author'] ) {
            ob_start();
            the_author_posts_link();
            $author_post_link = ob_get_clean();

            // Check if rich snippets are enabled.
            if ( fusion_library()->get_option( 'disable_date_rich_snippet_pages' ) && fusion_library()->get_option( 'disable_rich_snippet_author' ) ) {
                /* translators: The author. */
                $metadata .= sprintf( esc_html__( 'By %s', 'Avada' ), '<span class="vcard"><span class="fn">' . $author_post_link . '</span></span>' );
            } else {
                /* translators: The author. */
                $metadata .= sprintf( esc_html__( 'By %s', 'Avada' ), '<span>' . $author_post_link . '</span>' );
            }
            $metadata .= '<span class="fusion-inline-sep">|</span>';
        } else { // If author meta data won't be visible, render just the invisible author rich snippet.
            $author .= fusion_render_rich_snippets_for_pages( false, true, false );
        }

        // Render the updated meta data or at least the rich snippet if enabled.
        if ( $settings['post_meta_date'] ) {
            $metadata .= fusion_render_rich_snippets_for_pages( false, false, true );

            $formatted_date = get_the_time( fusion_library()->get_option( 'date_format' ) );
            $date_markup    = '<span>' . $formatted_date . '</span><span class="fusion-inline-sep">|</span>';
            $metadata      .= apply_filters( 'fusion_post_metadata_date', $date_markup, $formatted_date );
        } else {
            $date .= fusion_render_rich_snippets_for_pages( false, false, true );
        }

Other than this, I'm not sure what other info I need. Any help will be much appreciated! Keep in mind I'm very amateur at this.

2 Answers 2

0

You might be able to use the the_author_posts_link filter to do this:


add_filter( 'the_author_posts_link', functoin ($link) {

  global $post_id;

  // get "source" tax terms
  $sources = get_the_terms( $post_id, 'source' );

  // build html
  $html = '';

  foreach ($sources as $source) {
    $html += '<a href="'. get_term_link($source) .'">'. $source->name .'</a>';
  }

  // pre-pend html to author link

  $link = $link . $html;

  return $link;
});

More info: https://developer.wordpress.org/reference/functions/the_author_posts_link/

1
  • Thanks. Seems to be something not quite right though. All I'm getting is a 0 instead of the source name, i.e. Author Name0 | Month, Year. Not sure what that means.
    – Jon Fergus
    Commented Mar 30, 2020 at 18:56
0

Sorted this out with the help of several tutorials. Here's the code I'm using now:

function jwf_single_post_meta() {

// CATEGORY
$categories = get_the_category();
$catoutput = '';
if ( ! empty( $categories ) ) {
    foreach( $categories as $category ) {
        $catoutput .= '<a target="_blank" href="' . esc_url( get_category_link( $category->term_id ) ) . '" alt="' . esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ) . '">' . esc_html( $category->name ) . '</a>';
    }
}

// AUTHOR
$author = get_the_author_meta( 'display_name' );
$authorlink = get_author_posts_url( get_the_author_meta( 'ID' ) );

// SOURCE   
$sources = get_the_terms( $post->ID, 'source' );
if ( ! empty( $sources ) && ! is_wp_error( $sources ) ){
foreach ( $sources as $source ) {
    $sourcename = $source->name;    
    }
$sourcelink = get_term_link( $source );
$sep = __( ',' );
}   

// DATE
$original_date = get_the_time( 'F, Y' );
// $modified_date = get_the_modified_time( 'F, Y' );
// [Last Modified: $modified_date]</span>

echo "<span class='jwf-single-meta'>$catoutput by <a href='$authorlink'>$author</a>, <em><a href='$sourcelink'>$sourcename</a></em>$sep $original_date";

}

End result is: Category by Author, Source, Month, Year

Examples: Article by Joe Smith, Magazine Name, October, 1975

Translation by Some Guy, Series Name, January, 2010

I've tested two methods for inserting this, both include putting the above code in my child theme's functions.php file. Option one is to use add_filter to override the theme's single post meta function add_filter( 'the_author_posts_link', 'jwf_single_post_meta' );. Option two is to override the theme's single.php and insert the function there <?php echo jwf_single_post_meta(); ?>. Option 2 is what I'm going with for now.

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.