Support

Account

Home Forums General Issues Add a special global link on top of post title

Solving

Add a special global link on top of post title

  • Hello guys,

    Is it possible to use this plugin to add a special subtitle (that links to another page on my site) on the top of post titles? Here’s an image for illustration; https://i.imgur.com/qKPwrvZ.png

    The closest I have come to doing this using ACF is through this article on WP Beginner. However, it appends the subtitle on the post title. Also, it doesn’t not include a hyperlink.

    add_filter( 'the_title', 'wps_sponsored' );
    function wps_sponsored( $title ) {
       global $post;
       $sponsored = get_post_meta($post->ID, 'sponsored', true);
       if( is_single() && $sponsored == 'true' ){
           return 'Sponsored post: '.$title;
       }
       return $title;
    }

    Is there a way to modify the code and make it work as per my wish or do you have an idea of how I can achieve what I need?

  • The problem with filtering the_title is that your sub title will be inside of whatever heading tag the theme is using (h1, h2, h3, whatever)

    
    add_filter('the_title', 'wps_sponsored', 10, 2);
    function wps_sponsored($title, $post_id) {
      $sub_title = get_field('sub-title-field-name', $post_id);
      if ($sub_title) {
        $url = get_field('sub-title-url-field-name, $post_id);
        if ($url) {
          $sub_title = '<a href="'.$url.'">'.$sub_title.'</a>';
        }
        $sub_title = '<span class="subtitle">'.$sub_title.'</span>';
        $title = $sub_title.$title;
      }
      return $title;
    }
    
  • Hello

    Thanks for replying. The example given by Beginner only applies to post titles when using ACF. I have tried it and doesn’t affect any other title on the website.

    Is there any way I can add it. It’s not a must for it to be a sub-title. It can be a label with a link as well.

  • If you want to be able to set the text and link then you need somewhere to set them. That is what ACF is for.

    Not exactly sure what you’re looking for if you’re not using ACF to set the values.

  • I want to use ACF to achieve what I have explained above. I have included an image on my main post of what I want it to look like.

    I just want to include a subtitle or label on top of the post title that links to another page (link) on my website.

    Hope you understand now. I’ll be glad if you can help me do that.

    Thanks

  • The code I supplied will do what you are looking for, you need to add fields and then change the code to match your field names

    
    add_filter('the_title', 'wps_sponsored', 10, 2);
    function wps_sponsored($title, $post_id) {
      $sub_title = get_field('sub-title-field-name', $post_id);
      if ($sub_title) {
        $url = get_field('sub-title-url-field-name', $post_id);
        if ($url) {
          $sub_title = '<a href="'.$url.'">'.$sub_title.'</a>';
        }
        $sub_title = '<span class="subtitle">'.$sub_title.'</span>';
        $title = $sub_title.$title;
      }
      return $title;
    }
    
  • Thanks for that…I will experiment when I’m back at my workstation tomorrow and give you feed back.

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.