0

(WP Post Title)Malwarebytes Anti-Malware (Custom field value)3.5.1.2522

How to add custome meta value in wordpress after wp post title in same hyperlink

1

It is a simple 3 step process:

  1. Look for your post title markup that should look something like:
<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
  1. Now, get your post custom meta value through:
<?php $bannerContent = get_post_meta($post->ID, 'bannerContent', true); ?>
  1. Next add this value to your title where ever you need:
<h1><a href="<?php the_permalink(); ?>"><?php the_title();  echo $bannerContent; ?></a></h1>

This is it. If you need any further assistance, please let us know! Happy coding

0

add_filter( 'post_type_link', 'append_query_string', 10, 2 ); function append_query_string( $url, $post ) { return $url.'?var_name='.get_post_meta($post->ID,'meta_key',true); }

0

It's possible to change displayed title in template file (e.g. content.php):

$custom_field = get_post_meta($post->ID, $custom_meta_key, true);
if ( is_single() ) {
    the_title( '<h1 class="entry-title">', '</h1>' );
} elseif ( is_front_page() && is_home() ) {
    the_title( '<h3 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">',
        ' ' . $custom_field . '</a></h3>' );
} else {
    the_title( '<h2 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', 
        ' ' . $custom_field . '</a></h2>' );
}

You can also use the_title filter hook (code goes to functions.php):

add_filter( 'the_title', 'modify_post_title' );

function modify_post_title( $title ) {
    global $post;
    //
    // --- show on list and single post view
    // if ( $post->post_type == 'post' && in_the_loop() ) {
    // --- show only in single post view
    // if ( is_singular() && $post->post_type == 'post' && in_the_loop() ) {

    // when to apply custom field suffix
    // --- when type is "post" and when it's not a single post view
    if ( !is_singular() && $post->post_type == 'post' && in_the_loop() ) {
        // $custom_field = get_post_meta($post->ID, $custom_meta_key, true);
        $title .= $custom_field;
    }
    return $title;
}

It depends on what you want to achieve.

  • please showing $custom_field in single post title (remove is_singular) – Wakil DJ Aug 23 '18 at 19:56
  • do you want to append custom field to title in single post view? or title in single post is changed also, but you don't want change title there? @WakilDJ – nmr Aug 23 '18 at 20:14
  • not showing custom field value in single post? please add this $custom_field in the_title( '<h1 class="entry-title">', '</h1>' ); thank you – Wakil DJ Aug 23 '18 at 20:53
  • to show $custom_field on list and single post view use: if ( $post->post_type == 'post' && in_the_loop() ), to show only in single post view use: if ( is_singular() && $post->post_type == 'post' && in_the_loop() ). @WakilDJ – nmr Aug 23 '18 at 21:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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