I’ve created some anchor points that relate to various Post ID’s throughout my website, which when the user clicks on, takes them to a specific post further down the page. These work fine as I can set element ID’s using
<?php the_ID(); ?> and clickable links using
<a href="#<?php the_ID(); ?>" class="scroll-to-point"></a>.
What I’m now trying to figure out is if a user wants to link to a post by inserting a link through the WYSIWYG field, with a caption of say ‘Find Out More’, how I can output that link as a Post ID instead of the full URL.
I should also point out that I’m using Custom Post Types.
I’m not sure if this is something that can be achieved through ACF or through editing the Permalinks in WordPress (which I’ve tried but had no success with).
Thanks in advance!
You can’t do this with ACF and it would be difficult using WP to convert a link url to a post id. You might be able to add a filter to “the_content” that looks for urls and tries to convert them to an ID. You might be able to build your own shortcode to put into the content and let them select pages post object ACF field and then get the ID from that, but that’s not something that ACF can do and you’d need to build the shortcode PHP yourself.
Thanks for responding so quickly! I totally understand, I think I need to figure out a new approach.
One angle I’ve thought of is adding the same class to every link that is outputted by the WYSIWYG editor. Is that possible? So say on the front-end every link looked like:
<a href="blahblah" class="scroll-to-link"></a>
I’ve had a look through the TinyMCE documentation but everything seems to be weighted towards styling the editor – I haven’t come across anything that would help me add a class.
You should look into adding a filter to the_content https://codex.wordpress.org/Plugin_API/Filter_Reference/the_content. I wouldn’t know where to begin altering the editor. If you want to dig into that you might want to look into another forum where there’s people that have that knowledge.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
📣 “ACF Chat Fridays”— Advanced Custom Fields (@wp_acf) January 31, 2023
The ACF team holds their first open office hours this Friday! Come and talk ACF, and ask questions about building sites with the plugin.
We’d love to see you there!
📆 Friday 3rd Feb - 3pm UTC
👉 Register here - https://t.co/3UtvQbE4CU pic.twitter.com/oTwW9K1XQ0
© 2023 Advanced Custom Fields.