- This topic has 12 replies, 3 voices, and was last updated 4 years, 3 months ago by
Fernando.
-
AuthorPosts
-
February 7, 2022 at 12:38 pm #2108719
Dong
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.
February 7, 2022 at 12:46 pm #2108723Ying
StaffCustomer SupportHi Dong,
Not sure I understand what you are trying to achieve.
Can you explain a bit more?
February 7, 2022 at 1:11 pm #2108750Dong
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. 🙂
February 7, 2022 at 1:29 pm #2108764Ying
StaffCustomer SupportI 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!
February 7, 2022 at 1:40 pm #2108782Dong
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.
February 7, 2022 at 4:02 pm #2108857Ying
StaffCustomer SupportI 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 class
current-page-linkwill be added to the link.February 7, 2022 at 4:08 pm #2108863Dong
That’s perfect, thanks, Ying. ❤️
February 7, 2022 at 4:20 pm #2108871Ying
StaffCustomer SupportNo problem 🙂
February 7, 2022 at 8:05 pm #2108939Dong
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.
February 8, 2022 at 1:22 am #2109099Dong
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.
February 8, 2022 at 1:26 am #2109106Fernando Customer Support
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! 🙂
February 8, 2022 at 2:06 am #2109147Dong
Looks like that fixed it, Fernando. I’ll test a round some more and will confirm. Thanks.
February 8, 2022 at 2:54 am #2109193Fernando Customer Support
Glad to have helped! Feel free to reach out anytime if any further help is needed! 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.