meta query - How to show the a custom taxonomy term on single post metadata

I have a custom taxonomy called "Sources" (using Custom Post Type UI), where the sources are the names of publ

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:

/

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.

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/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.

Share Improve this question asked Mar 29, 2020 at 0:33 Jon FergusJon Fergus 791 silver badge10 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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/reference/functions/the_author_posts_link/

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744576106a4581749.html

相关推荐

  • meta query - How to show the a custom taxonomy term on single post metadata

    I have a custom taxonomy called "Sources" (using Custom Post Type UI), where the sources are the names of publ

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信