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

Ami*_*min 5 php wordpress rss simplepie

我对 Wordpress 中的 RSS 提要进行了一些更改,我正在使用fetch_feed()向另一个网站显示数据。假设有 2 个网站,分别称为 #Wordpress1 和 #Wordpress2。这是我添加到#wordpress1functions.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;
    }
}
Run Code Online (Sandbox Code Playgroud)

此代码将产品价格和缩略图添加到 Rss 提要现在我们需要在 #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>';
}
Run Code Online (Sandbox Code Playgroud)

在上面的代码中我应该使用什么来代替 MY_IMAGE_FROM_RSS 和 MY_PRICE_FROM_RSS

Bas*_*sen 2

您应该使用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']