在Wordpress中自动生成自定义字段值
问题描述:
我运行了一些我会聚合到我的主博客中的博文。 我使用simplepie解析来自其他博客的提要,因此帖子正在自动创建。在Wordpress中自动生成自定义字段值
我的典型职位是奠定了这样的:
- IMAGE
- 内容/ TEXT
- HYPERLINK
什么我希望做的是自动抓取超链接,将其插入到自定义字段中。自定义字段已存在于帖子中,但我需要将帖子内容中包含的超链接作为值插入。
我只需要链接,无需HTML,所以价值将只是一个直的链 - http://domain.com/fsdds
我知道有一些与图像完成这个插件,但是我还没有看到任何可以与其他任何东西一起做的事情,比如超链接。
我在Wordpress论坛发布了这个问题,被告知我必须解析整个帖子内容寻找链接,我知道,问题是我不太确定该怎么做。
感谢
答
安东尼的回答大厦,使用更新后的META一旦你有你的链接...
在functions.php文件将这个:
function catch_that_link() {
global $post, $posts;
$first_link = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/(https?://)?(www.)?([a-zA-Z0-9_%]*)\b.[a-z]{2,4}(.[a-z]{2})?((/[a-zA-Z0-9_%])+)?(.[a-z])?/', $post->post_content, $matches);
$first_link = $matches [1] [0];
if(empty($first_link)){ //Defines a default image
return 'no link found';
}
return $first_link;
}
然后在您的查询循环,类别文件或任何PHP文件,你会做以下
<?php
$post_id = 13; //replace the number with the specific post
$meta_key = 'key_example' //replace with your custom field name
$meta_value = catch_that_link();
update_post_meta($post_id, $meta_key, $meta_value);
?>
答
这是在后抓住了第一个图像的功能:你只需要更换第一preg_match_all参数
function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$first_img = "/images/default.jpg";
}
return $first_img;
}
:
“/(HTTPS://)? (www。)?([a-zA-Z0-9_%] )\ b。[az] {2,4}(。[az] {2})?((/ [a-zA-Z0-9_ %])+)?(。[az] *)?/'
将整个函数添加到您的functions.php中,并从脚本中调用该函数。 它应该返回它在帖子内容中找到的第一个链接。
这样做,感谢你和安东尼 – rocky 2010-01-29 06:12:51