Site logo

Archived topic

Add link to blog post on archive/home for post format "link"

16 replies · Started by Michael on March 23, 2021

Viewing posts 1–15 of 17

Hi, I am using post formats on my blog mkln.org. I have quite a lot of link-posts (post format = "link").

On all pages except is_single, the title of such a post is linked to the first link available in the post_content.

How can I add a small icon or a text-link to my post (best would be next to the date or in the footer meta), so that I can get a link to the actual post on my site and not the external link, when clicking it? (To be precise: The title can still link outside, but there should be an option for my readers to open the single-entry, too.)

Hi there,

try adding this PHP Snippet:

add_action('generate_after_entry_title', function(){
  if ( has_post_format('link') && !is_single() ) {
    echo '<a href="'. get_post_permalink() .'">Link to post</a>'; 
  }
},5);

It will add the post permalink below the archive title for any Post Format Links.

Adding PHP: https://docs.generatepress.com/article/adding-php/

Thanks David, I have added your snippet to a plugin. However, no matter what, the link does not appear. I have checked with wp_debug on, but nothing... (I have changed the link text to simply "#", though.)

Which plugin did you add the code to ?

Have you tried using the Code Snippets plugin to add the snippet?

I have a self-made utilities-plugin for all the stuff that usually goes to functions.php. All other hooks work nicely, just not this one.

Can you try a simple test to see if the hook is working with this snippet:

add_action('generate_after_entry_title', function(){
  ?><p>i am after entry title</p><?php
},5);

Nope. Did just that without the condition with the previous snippet.

Can you try moving that snippet to the top of your plugin functions ?

Hi David! Moved it up, it worked. I found the issue with another function (a ";" was missing 🤦‍♂️). Thanks for your help and sorry for bothering that much!

One thing, perhaps though: How could I get the link to the post inside the div.entry-meta or h2?

Instead of using the action hook, you could use a filter hook the_title like so:

function append_post_format_link($title, $id) {
  $post_link = '<a href="'. get_post_permalink() .'"> #</a>';
	if ( has_post_format('link') && !is_single() && !is_admin() ) {
		$title .= $post_link;
	}
	return $title;
}

add_filter('the_title', 'append_post_format_link', 10, 2);

This will add the link within the H2.

Beautiful! Thank you, David!

Glad to be of help!

Hi David - just a minor addition in case someone else has the same issue and is wondering, why suddenly the menu items (!) have links attached. One needs to add the post-id to has_post_format to prevent this like so:

function append_post_format_link($title, $id) {
  $post_link = '<a href="'. get_post_permalink() .'"> #</a>';
	if ( has_post_format( 'link', $id ) && !is_single() && !is_admin() ) {
		$title .= $post_link;
	}
	return $title;
}

add_filter('the_title', 'append_post_format_link', 10, 2);

Ah yes, my bad - the_title() filter is used in lots of places..... thanks for updating !

I will add a new topic if need-be, but my issue is related.

I'm just trying to figure out how to have posts with the "link" post format open in a new window.

So a filter (I think) where I add target="_blank" within the a tag.

Thanks in advance guys!

This archived topic is closed to new replies.