通过自定义字段(wordpress)获取图像的 url

发布于 2024-11-03 15:03:00 字数 781 浏览 1 评论 0原文

我什至不确定是否可以做到这一点,但是...

我已经从我的论坛添加了一个提要到 WordPress,它效果很好,但我需要它从帖子中的图像(提要)中的自定义字段中自动添加图像的 url第一张图片就很好了,因为它只有一个滑块 有

什么办法可以做到这一点吗?

详细信息

好吧,我想我没有很好地解释这一点,所以做了一些屏幕截图

在此处输入图像描述

这是我当前的滑块 在此处输入图像描述

这是我使用的另一个提要的导入帖子

在此处输入图像描述

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

在此处输入图像描述

将图像 url 添加到自定义字段

在此处输入图像描述

最后查看滑块正在工作

这就是我想要做的(自动),所以我的来自我的 booru / 论坛 / 2 个其他我的网站和(2 个其他人)网站的提要使我的主页位于一个新网站上

希望这能解释更多

Am not even sure if this can be done but...

Ive added a feed from my forums to wordpress it works great but I need it to auto add the url of the image in a custom field from the images in the post (feed) first image would be fine as its only ahve a slider

Is there any way to do this?

Details

Ok I think I did not explain this very well so made a few screen shots

enter image description here

This is my slider at the minute with my
enter image description here

This is an imported post one other feed I was using

enter image description here

On this image you can see the custom field (which I have to fill in after every import)

enter image description here

Adding the image url into the custom field

enter image description here

and finaly a view of the slider working

This is what am trying to do (auto) so my feed from my booru / forums / 2 other of my sites and (2 other peoples) sites make my home page on a new site

Hope this explain it alot more

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑凤梨 2024-11-10 15:03:00

这使用外部 WordPress 内置的 Simple Pie 库 来获取 feed,获取图像url 并为每个项目创建一个新帖子,并将图像 url 保存为自定义字段。

要激活该进程,我们必须挂钩 wp_cron。下面的代码每天执行一次,但最好每周执行一次以防止重叠。可能会发生一些重叠,因此仍然需要一种方法来检查我们是否已经导入图像。

首先,我们需要一个函数来在创建帖子后保存自定义字段。本节来自我在 WordPress Answers 上找到的另一个答案

编辑:

这需要封装在一个插件中来安排 cron 事件,并且 cron 事件缺少触发它的操作。

编辑:

下面的最终版本经过测试并且可以工作,但是 OP 获取的提要正在使用相对 url,因此需要在输出代码中的某个位置添加域名。

<?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;
    }

        

This uses the external Simple Pie library built into WordPress to fetch the feed, get the image url and create a new post for each item and save the image url as a custom field.

To activate the process we have to hook into wp_cron. The code below does it daily but it would probably be better to do it weekly to prevent overlap. Some overlap will probably occur so this still needs a way to check if we have already imported the image

First we need a function to save the custom field after the post has been created. This section comes from another answer I found on WordPress Answers.

Edit:

This needs to be wrapped in a plugin to schedule the cron event and the cron event was missing the action to make it fire.

Edit:

Final version below tested and it works but the feed the OP is getting is using relative url's so the domain name needs to be added somewhere in the output code.

<?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;
    }

        
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文