Discover in this tutorial how to get and display all the links in a WordPress post using code.
In posts that have many links it can be interesting to display a list of all of them at the end of the post. This way the reader can read all the content continuously and then continue to expand the information with some of the links.
It can also be interesting when there is a lot of internal linking to show a section of “related links” or something similar.
Let’s see how you can achieve this.
Snippet to display a list of links at the end of a WordPress post
Add the following code at the end of functions.php or in your functionality plugin:
To see this and another 1091 code snippets of this website, login or subscribe here.
In this code you use the_content
filter along with the WP_HTML_Tag_Processor
class to get all the links within the post content to display them in list format at the end of the post.
You can customize the code to fit your needs. For example:
- “
is_single
“, you can change it to another conditional tag. - “Links”, you can change the text displayed before the listing.
If you prefer to display only links from a certain domain or containing a particular word, you can also do this.
For example, if you have linked to other content on the same website and you want to display a list only of these links, this would be the necessary code.
To see this and another 1091 code snippets of this website, login or subscribe here.
With this modification you indicate that only add links containing “osomcode.com” and instead of “Links” display header “Related tutorials”.
In fact, that same code is what I use to display the related tutorials here. 🙂
Conclusions
Now you know how to get and display a list of links in a WordPress post and display them at the end of the post.
Any questions? I read you in the comments.
And if you want to give me any suggestion for future tutorials, leave it in the contact form. Advantages of being a subscriber. 😉