WordPress PHP error getting posts from another wor

2019-07-31 02:28发布

I'm using this code in the template-blog.php page of my website, to get posts from my first WP blog to the second WP blog, different websites using the same blog posts.

this code has been working great for years, but suddenly it stopped working giving me invalid argument foreach on line 133

it gives an error on this part

<?php
//$url = 'https://www.FIRSTWP.com/wp-json/wp/v2/posts/';
$url = 'https://www.FIRSTWP.com/wp-json/posts/';
$response = wp_safe_remote_get( $url );
if ( ! is_wp_error( $response ) ) {
    $remote_post = json_decode( wp_remote_retrieve_body( $response ) );
    //echo "<pre>";
    //print_r($remote_post[0]);
    //echo "</pre>";
                    //echo '<h3><a href="' . esc_url( $remote_post[0]->link ) . '">' . esc_html( $remote_post[0]->title ) . '</a></h3>';

                    //remote post's content
                    //echo '<p>' . wp_kses_post( $remote_post[0]->content ) . '</p>';

?>

<?php foreach( $remote_post as $remote_single_post){ ?>
<article id="post-1" class="post-1 post type-post status-publish format-standard hentry category-uncategorized">
                        <header class="entry-header">
                                                        <h1 class="entry-title"><a href="<?php echo esc_url( $remote_single_post->link ); ?>" title="Permalink to Hello world!" rel="bookmark" target="_blank"><?php echo esc_html( $remote_single_post->title ); ?></a></h1>

                                                    </header><!-- .entry-header -->

                                                <div class="entry-content">
                                                <div  class="api_content"><?php echo '<p>' . wp_kses_post( $remote_single_post->content ) . '</p>'; ?> </div>


                           <p class="but"><a href="<?php echo esc_url( $remote_single_post->link ); ?>" target="_blank"><img alt="" border="0" src="<?php echo get_template_directory_uri(); ?>/images/more.jpg"></a></p>
                        </div><!-- .entry-content -->


                                                                                                                <span class="cat-links">
                                <span class="entry-utility-prep entry-utility-prep-cat-links" style="color: #ff99CC;">Posted in</span>
                          <?php foreach( $remote_single_post->terms->category as $api_category){ ?>
                          <a href="<?php echo esc_html ($api_category->link)?>" rel="category tag" target="_blank"><?php echo esc_html ($api_category->name)?></a>, <?php } ?>                  
                            </span>



</article>  

I'm not sure what is causing the problem is it has been working fine for years now, so any help would be appreciated

0条回答
登录 后发表回答