Web Development & WordPress

How to show posts from one wordpress website to another one without importing posts: Rest API and Shortcode

Can you Show posts from one wordpress website to another one without importing the posts?

Yes you can do it using REST API. Just you need to enable REST API of the site which posts you want to show.

I am sharing you a demo code to make you understand how you can code it ( it is a demo code, you need to modify it based on the fields which REST API url is providing) .

function latestPost() {  ob_start(); ?>
 
<div id="pencilatest_posts_46862" class="penci-latest-posts-sc penci-latest-posts-grid penci-latest-posts-el penci-el-mixed-s1 penci-latest-posts-left penci-std-continue-center penci-std-excerpt-center penci-grid-excerpt-left lp">
  <div class="penci-wrapper-posts-ajax">
    <div class="penci-wrapper-posts-content pwid-default">
      <ul class="penci-wrapper-data penci-grid penci-shortcode-render">
    
 
  

           

 
  <?php
$url = 'https://UrlFromWhereToShow.com/wp-json/wp/v2/posts';
$response = file_get_contents($url);
$data = json_decode($response, true);

if ($data) {
    $i=1;
    foreach ($data as $post) {
       $title = $post['title'] ; // Get the post title
        $date = $post['date']; // Get the post date
       $featuredImageURL = $post['featured_media'][0] ; 
       $postUrl=$post['guid'];
       $categories=$post['categories'];
        //print_r($categories);
       
 
    
       ?>
       
           <li class="grid-style">
          <article id="post-7108" class="item hentry">
            <div class="thumbnail">
              <a class="penci-image-holder penci-lazy lazyloaded pcloaded"   href="<?php echo $postUrl;?>" title="<?php echo $title;?>"  style="background-image: url('<?php echo $featuredImageURL;?>');"></a>
            </div>
            <div class="grid-header-box">
              <?php           
				if (count($categories) > 1) {?>
				
					<span class="cat">
                <a style="" class="penci-cat-name penci-cat-559" href="#" rel="category tag">
                  <span style=""><?php echo $categories[0];?></span>
                </a>
                <a style="" class="penci-cat-name penci-cat-555" href="#" rel="category tag">
                  <span style=""><?php echo $categories[1];?></span>
                </a>
              </span>
    
<?php					
} 
?>
				 <?php           
				if (count($categories)<2) {?>
				
					<span class="cat">
                <a style="" class="penci-cat-name penci-cat-559" href="#" rel="category tag">
                  <span style=""><?php echo $categories[0];?></span>
                </a>
               
              </span>
    
<?php					
} 
?>
              <h2 class="penci-entry-title entry-title grid-title">
                <a href="<?php echo $postUrl;?>"><?php echo $title;?></a>
              </h2>
               
              <div class="grid-post-box-meta">
                <span class="otherl-date">
                  <time class="entry-date published"><?php echo $date;?></time>
                </span>
              </div>
            </div>
          </article>
        </li>  
       
       <?php
     
     $i++;
	if($i==7){
		break;
	}	
       
        
    }
} else {
    echo "Error retrieving posts.";
    
}
 
?>


</ul>
    </div>
  </div>
</div>
<?php
	return ob_get_clean();				  
 }
//add_shortcode('latestpost', 'latestPost');

 

function registerLatestPostShortcode() {
    add_shortcode('latestpost', 'latestPost');
}

// Hook the shortcode registration function to the init action
add_action('template_redirect', 'registerLatestPostShortcode');

Leave a comment