来自图像的网址通过自定义字段(wordpress)

时间:2011-04-26 20:19:26

标签: image wordpress field

我甚至不确定是否可以这样做但是......

我添加了一个从我的论坛到wordpress的提要它很好但我需要它自动添加图像的网址在自定义字段中的图像在帖子(饲料)第一张图片将是好的,因为它唯一的啊滑块

有没有办法做到这一点?

详细

好吧我觉得我没有很好地解释这个,所以做了几个屏幕截图

enter image description here

这是我的滑块 enter image description here

这是我正在使用的另一个Feed

的导入帖子

enter image description here

在此图片上,您可以看到自定义字段(我必须在每次导入后填写)

enter image description here

将图片网址添加到自定义字段

enter image description here

最后一个滑块工作视图

这就是我想要做的事情(自动)所以我的booru / forums / 2我的其他网站和(2个其他人)网站上的Feed会在新网站上显示我的主页

希望这可以解释更多

1 个答案:

答案 0 :(得分:2)

这使用外部Simple Pie library built into WordPress获取Feed,获取图片网址并为每个项目创建新帖子,并将图片网址另存为自定义字段。

要激活这个过程,我们必须挂钩到wp_cron。下面的代码每天都会这样做,但每周做一次以防止重叠可能会更好。可能会发生一些重叠,因此仍需要检查我们是否已导入图像

首先,我们需要一个函数来在创建帖子后保存自定义字段。此部分来自another answer I found on WordPress Answers

编辑:

这需要包装在一个插件中以安排cron事件,而cron事件缺少使其触发的操作。

编辑:

下面的最终版本已经过测试,它可以运行,但是OP正在使用相关网址,因此需要在输出代码中的某处添加域名。

<?php
/*
Plugin Name: Fetch The Feed Image
Version: 0.1
Plugin URI: http://c3mdigital.com       
Description: Sample plugin code to fetch feed image from rss and save it in a post
Author: Chris Olbekson
Author URI: http://c3mdigital.com
License: Unlicense For more information, please refer to <http://unlicense.org/>
*/

//Register the cron event on plugin activation and remove it on deactivation

register_activation_hook(__FILE__, 'c3m_activation_hook');
register_deactivation_hook(__FILE__, 'c3m_deactivation_hook');

add_action( 'c3m_scheduled_event', 'create_rss_feed_image_post');
function c3m_activation_hook() {
    wp_schedule_event(time(), 'weekly', 'c3m_scheduled_event');
}

function c3m_deactivation_hook() {
 wp_clear_scheduled_hook('c3m_scheduled_event');    
}


function create_rss_feed_image_post() {
     if(function_exists('fetch_feed')) {
            include_once(ABSPATH . WPINC . '/feed.php');               // include the required file
            $feed = fetch_feed('http://animelon.com/booru/rss/images'); // specify the source feed

    }       

        foreach ($feed->get_items() as $item) :

        //  global $user_ID;
            $new_post = array(
            'post_title' =>  $item->get_title(),
            'post_status' => 'published',
            'post_date' => date('Y-m-d H:i:s'),
            //'post_author' => $user_ID,
            'post_type' => 'post',
            'post_category' => array(0)
            );
            $post_id = wp_insert_post($new_post);

        if ($enclosure = $item->get_enclosure() )

            update_post_meta( $post_id, 'feed_image_url', $enclosure->get_link() );
        endforeach;
    }
相关问题