Site logo

Archived topic

Dealing with a link to the current page

12 replies · Started by Dong on February 7, 2022

Viewing posts 1–13 of 13

Hi guys,

I want to automatically deal with a href link to the current page in particular ways.

Specifically, I'd like to remove that link automatically, or better yet, add a CSS class to the link's text so I can style it differently. The trick is I only want to do that on a clean link and not those with anchors.

So, for example, in this post:

https://dongknows.com/crucial-x6-review/

I'd like to have a way to manage "Crucial X6" in the first sentence but leave all the Table of Content links on the page alone. Do you have a PHP snippet for this or something similar?

Please help! Thanks.

-Dong.

Hi Dong,

Not sure I understand what you are trying to achieve.

Can you explain a bit more?

Thanks for the response, Ying. OK, let me explain.

So when I have a page that's at this link:

https://dongknows.com/crucial-x6-review/

Within that page, there's a hyperlink text that also links to

https://dongknows.com/crucial-x6-review/

That's the case in the first sentence of the above page:

"Micron's second entry into portable solid-state drives, the Crucial X6, doesn't have the same bang as the first, the X8. But it has some novelty of its own." The text #Crucial X6# carries that link.

I want to be able to either have that link removed automatically or have a CSS class add the text "Crucial X6" so I can style it.

I know I CAN remove that link myself manually, but that's not the point. Sometimes, I have a series of related posts, each containing a reusable "related posts" block that has links to every post in the series. In this case, I want the link to the current post styled, disabled, or removed automatically only on that particular post. In other words, the link is still there, but it won't respond to a click.

Hope it makes sense this time. :)

I know I CAN remove that link myself manually

How did you add that link in the first place? As GP doesn't add any links to the post content.

Let me know!

I added that link just for a demo purpose, Ying. I want a way to automatically deal with a link to the current post on any post since that might happen via the use of reusable blocks.

Say you have a reusable block called "Related content" in which you have:

Post 1 (link)
Post 2 (link)
Post 3 (link)
Post 4 (link)
Post 5 (link)

Now, if you use that block in posts 1 to 5, in each case, you will have a redundant link to the current post, and I want that link automatically and dynamically styled WITHOUT changing the block itself since that would affect the rest of the posts.

Specifically, when I use that reusable block in post #2, I'd like to have:

Post 1 (link)
Post 2 (link) -> text styled, or link removed / disabled
Post 3 (link)
Post 4 (link)
Post 5 (link)

When used in post, say, #3:

Post 1 (link)
Post 2 (link)
Post 3 (link) -> text styled, or link removed / disabled
Post 4 (link)
Post 5 (link)

So on and so forth. I think if I can have a way to add a CSS class to the link in question, I can handle it.

I see, give this PHP snippet a try:

function add_class_to_current_page_link($text) {
    if (is_single()) {
      	$current_url = get_permalink( get_the_ID() );
	$text =  str_replace ('<a href="'. $current_url, '<a class = current-page-link href="'. $current_url , $text);
	return $text;
	}
}
add_filter('the_content','add_class_to_current_page_link');

When there's a link in the post matches the current post link, the CSS classcurrent-page-link will be added to the link.

That's perfect, thanks, Ying. ❤️

No problem :)

Sorry to bother you again, Ying.

The code works great when it works, but it doesn't with some links. Maybe there's an exception somewhere? Here's an example. Here are two examples. You'll note that some items on the Related Posts list don't work.

https://dongknows.com/best-wi-fi-6-routers/ (the last item doesn't work)

https://dongknows.com/tesla-model-y-ev-and-that-range-anxiety/ (the second and last don't work)

I checked the URL of the posts in question and didn't find anything special. Can you please take a look?

Thanks,

-Dong.

Hi Ying,

I found out just now that your snippet caused all of my pages to fail to load. They only show the headlines and that was it. That said, I have to disable it now. Hopefully, we can figure this out tomorrow, the issue with hit or miss remains.

-Dong.

Hi Dong,

Can you try modifying the current PHP snippet to this one and see if it makes a difference?:

function add_class_to_current_page_link($text) {
	 if (is_single()) {
		$current_url = get_permalink( get_the_ID() );
		$text =  str_replace ('<a href="'. $current_url, '<a class="current-page-link" href="'. $current_url , $text);
		return $text;
		}
	return $text;
}
add_filter('the_content','add_class_to_current_page_link');

Hope to hear from you soon! :)

Looks like that fixed it, Fernando. I'll test a round some more and will confirm. Thanks.

Glad to have helped! Feel free to reach out anytime if any further help is needed! :)

This archived topic is closed to new replies.