如何使用SimplePie从wordpress RSS获取自定义数据

时间:2022-06-14 01:17:00

I have made some changes to a RSS feed in my Wordpress, and I'm using fetch_feed() to show data to another website. Imagine there are 2 websites called #Wordpress1 and #Wordpress2. This is the code i've added to #wordpress1's functions.php file

我在我的Wordpress中对RSS源进行了一些更改,我正在使用fetch_feed()向另一个网站显示数据。想象一下,有两个名为#Wordpress1和#Wordpress2的网站。这是我添加到#wordpress1的functions.php文件中的代码

add_action('rss2_item', 'dw_add_data_to_rss');
function dw_add_data_to_rss(){
    global $post;

    if( $post->post_type == 'product' ) {
        $product = new WC_Product( $post->ID );

        $output = '';
        $thumbnail_ID = get_post_thumbnail_id( $post->ID );
        $thumbnail = wp_get_attachment_image_src($thumbnail_ID, 'thumbnail');
        $output  = '<post-thumbnail>';
        $output .= '<url>'. $thumbnail[0] .'</url>';
        $output .= '<width>'. $thumbnail[1] .'</width>';
        $output .= '<height>'. $thumbnail[2] .'</height>';
        $output .= '</post-thumbnail>';

        $output .= '<price>' . number_format( $product->get_price() ) . ' ' . get_woocommerce_currency_symbol() . '</price>';

        echo $output;
    }
}

this code adds product price and thumbnail to Rss feed now we need to display these data on #Wordpress2 , but i don't know how to do it

此代码将产品价格和缩略图添加到Rss Feed现在我们需要在#Wordpress2上显示这些数据,但我不知道该怎么做

$rss = fetch_feed( 'http://localhost/wp/feed/?post_type=product' );
if ( ! is_wp_error( $rss ) ) {
    $maxitems  = $rss->get_item_quantity( 10 ); 
    $rss_items = $rss->get_items( 0, $maxitems );
}

foreach ( $rss_items as $item ) {
    echo '<a href="'. $item->get_permalink() .'"><img src="{MY_IMAGE_FROM_RSS}"> <span class="price">{MY_PRICE_FROM_RSS}</span></a>';
}

what should i use instead of MY_IMAGE_FROM_RSS and MY_PRICE_FROM_RSS in above code

在上面的代码中我应该使用什么而不是MY_IMAGE_FROM_RSS和MY_PRICE_FROM_RSS

1 个解决方案

#1


1  

You should use the get_item_tags() function and use blank for the required namespace.

您应该使用get_item_tags()函数并使用blank作为所需的命名空间。

For MY_IMAGE_FROM_RSS use $item->get_item_tags('','post-thumbnail')[0]['child']['']['url'][0]['data'] and for MY_PRICE_FROM_RSS use $item->get_item_tags('','price')[0]['data']

对于MY_IMAGE_FROM_RSS,请使用$ item-> get_item_tags('','post-thumbnail')[0] ['child'] [''] ['url'] [0] ['data']和MY_PRICE_FROM_RSS使用$ item- > get_item_tags( '', '价格')[0] [ '数据']

#1


1  

You should use the get_item_tags() function and use blank for the required namespace.

您应该使用get_item_tags()函数并使用blank作为所需的命名空间。

For MY_IMAGE_FROM_RSS use $item->get_item_tags('','post-thumbnail')[0]['child']['']['url'][0]['data'] and for MY_PRICE_FROM_RSS use $item->get_item_tags('','price')[0]['data']

对于MY_IMAGE_FROM_RSS,请使用$ item-> get_item_tags('','post-thumbnail')[0] ['child'] [''] ['url'] [0] ['data']和MY_PRICE_FROM_RSS使用$ item- > get_item_tags( '', '价格')[0] [ '数据']