Cómo obtener datos personalizados de wordpress RSS con SimplePie

2 minutos de lectura

He realizado algunos cambios en un feed RSS en mi WordPress y estoy usando fetch_feed() para mostrar datos a otro sitio web. Imagina que hay 2 sitios web llamados #Wordpress1 y #Wordpress2. Este es el código que he agregado a #wordpress1’s functions.php expediente

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

este código agrega el precio del producto y la miniatura al feed Rss ahora necesitamos mostrar estos datos en #Wordpress2, pero no sé cómo hacerlo

$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>';
}

¿Qué debo usar en lugar de MY_IMAGE_FROM_RSS y MY_PRICE_FROM_RSS en el código anterior?

Deberías usar el get_item_tags() función y use un espacio en blanco para el espacio de nombres requerido.

Para MY_IMAGE_FROM_RSS usar $item->get_item_tags('','post-thumbnail')[0]['child']['']['url'][0]['data'] y para MY_PRICE_FROM_RSS usar $item->get_item_tags('','price')[0]['data']

¿Ha sido útil esta solución?