我有WordPress网站与100个广告帖子,我需要删除所有外部链接从这篇文章。但是新的职位也应该有外部的联系。因此,这就是为什么我必须从数据库中删除外部链接,而不是使用脚本。
有什么办法吗?
编辑,我使用这个脚本删除ID为1- 3568的帖子中的链接。
add_filter( 'the_content', 'filter_the_content_in_the_main_loop' );
function filter_the_content_in_the_main_loop( $content ) {
if ( is_single(range(1, 3568)) ) {
$content = preg_replace(array('"<a href(.*?)>"', '"</a>"'), array('',''), $content);
}
return $content;
}有可能添加一些东西,什么会将这些数据发送到数据库?
发布于 2022-03-03 00:11:21
首先需要标识包含链接的表和列。
有两种类型的链接:site links和external links。
站点链接应该类似于http:\\example.com\anything_else
post的WP标准表是wp_posts,但对于hiperlink则取决于定制(可以是wp_post中的一列,也可以是其他地方的列)。这是你自己找的)
假设表是wp_posts,列是links,那么您需要按模式查找,并不使用这些列进行更新。
主要模式应该是start with : http:\\example.com (意思是将作为站点链接而不是外部链接的链接)。
翻译成sql
update wp_posts
set links = ''
where links not like 'http:\\example.com%'注: WP标准DB为MySql
更新:
function filter_the_content_in_the_main_loop( $content ) {
global $post;
if ( is_single(range(1, 3568)) ) {
$content = preg_replace(array('"<a href(.*?)>"', '"</a>"'), array('',''), $content);
//find the post_id and set new content
//wp standard way of working with post instead of direct sql
//this will update each time each post, even the new one after display
//(if is in range, so for new one to remain unmodified just keep a lower range,
//up to last 100 posts)
$my_post = array();
$my_post['ID'] = $post->ID;
$my_post['post_content'] = $content;
wp_update_post( $my_post );
}
return $content;
}https://stackoverflow.com/questions/71330466
复制相似问题