admin管理员组文章数量:1122846
I'm looking for a code snippet or plugin to solve one of my problems.
There are 4 different post types. Each has at least 5,000 posts. I want: if i enter a post title (plain text) in the content, it will automatically receive the post link.
So, if i have post with "CCC" title, and i will put in this one another post content, wordpress it would automatically detect this and linking automaticly the "CCC".
Is this possible? Is there such a plugin, code snippet or anything else?
Thanks
I'm looking for a code snippet or plugin to solve one of my problems.
There are 4 different post types. Each has at least 5,000 posts. I want: if i enter a post title (plain text) in the content, it will automatically receive the post link.
So, if i have post with "CCC" title, and i will put in this one another post content, wordpress it would automatically detect this and linking automaticly the "CCC".
Is this possible? Is there such a plugin, code snippet or anything else?
Thanks
Share Improve this question asked Sep 21, 2024 at 6:03 MapaMapa 194 bronze badges 2- is this possible ? yes. is there a plugin for that ? no idea, here is a place for helping about development then you will not find advices about which existing plugin using. but we can help you to write you own plugin to do that. do you know development with php ? – mmm Commented Sep 21, 2024 at 7:26
- Plugin recommendations are off topic here. While it would be possible to write one, that's way too large a scope for a Stack Exchange question. – Chris Cox Commented Sep 21, 2024 at 11:24
1 Answer
Reset to default 0You can achieve your desired result with the help of given code which needs to be added to functions.php file
of your theme.
The given code will automatically convert plain text titles in your post content into links pointing to the corresponding posts.
<?php
function auto_link_post_titles( $content ) {
// This is to get all published posts from all post types.
$args = array(
'post_type' => 'any', // this is to get posts from all post types.
'post_status' => 'publish',
'posts_per_page' => -1, // this is to retrieve all posts
);
$posts = get_posts( $args );
// Here are looping through each post and replace the title in the content with a link.
foreach ( $posts as $post ) {
$post_title = esc_html( $post->post_title ); // Here we are getting the post title.
$post_link = esc_url( get_permalink( $post->ID ) ); // Here we are getting the post link.
// Create a link element
$link = '<a href="' . $post_link . '">' . $post_title . '</a>';
// Here we are replacing the plain text title with the link.
$content = str_replace( $post_title, $link, $content );
}
return $content;
}
// We have apply the function to the content of posts.
add_filter( 'the_content', 'auto_link_post_titles' );
本文标签: linksDetect post title(s) in content and automaticly linking
版权声明:本文标题:links - Detect post title(s) in content and automaticly linking 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736290084a1928438.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论