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
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 = 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
You should use the get_item_tags() function and use blank for the required namespace.
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']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With