
[英]How to get the url from <media:content> in a RSS feed using SimplePie
[英]How to get custom data from wordpress RSS with SimplePie
我已经在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;
}
}
此代码将产品价格和缩略图添加到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>';
}
在上面的代码中我应该使用什么代替MY_IMAGE_FROM_RSS和MY_PRICE_FROM_RSS
您应该使用get_item_tags()函数,并将空白用于所需的名称空间。
对于MY_IMAGE_FROM_RSS
使用$item->get_item_tags('','post-thumbnail')[0]['child']['']['url'][0]['data']
;对于MY_PRICE_FROM_RSS
使用$item->get_item_tags('','price')[0]['data']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.