Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

            
              <div id="app">

  <div class="container">
    
    <div class="row mb-5">
    
      <div class="col-sm-12">
          
          <h1>WP REST API Search Example (Final)</h1>
        
          <p>Search through six of the most popular WordPress blogs at once, powered by the <a href="https://developer.wordpress.org/rest-api/">WP REST API</a> and <a href="https://vuejs.org/">Vue.JS</a>. This is a companion pen to a tutorial on the <a href="https://www.toptal.com/developers/blog">Toptal Engineering Blog</a>.</p>
        
        <p><a href="https://codepen.io/bacoords/pen/EZvPMN">Return to Phase Two</a></p>
      
      </div>
      
    </div>
  
    <div class="row">
    
      <div class="col-md-4 mb-5 push-md-8">
      
        <div class="sticky-top card">
        
          <div class="card-block input-group mb-3">

            <input type="text" class="form-control" v-model.lazy="searchQuery" v-on:change="loadPosts" />
            
            <span class="input-group-btn">
            
              <button class="btn" type="button" v-on:click="loadPosts()">Search</button>
              
            </span>
            
          </div>
          
          <div class="card-block">
            
            <h6>Websites:</h6>

            <div class="form-check" v-for="source in sources">

              <label>

                <input type="checkbox" name="{{source.name}}" v-model="source.on" v-on:change="loadPosts"/>

                {{source.name}}

              </label>  

            </div>

          </div>
            
        </div>
      
      </div>
      
      <div class="col-md-8 p-0 pull-md-4">
  
        <div class="container mb-5" v-show="searchQuery">
          
          <div class="row">
            
            <div class="col-md-12">
               
              <div class="card card-inverse card-info">
              
                <div class="card-block">
                
                  Search Results for: {{searchQuery}}
                
                </div>
              
              </div>
              
            </div>
            
          </div>
          
        </div>
        
        <div class="container" v-for="source in sources">

          <div class="row">

            <div class="container" v-show="source.on" >

              <div class="row mb-5" v-for="post in source.posts">

                <div class="col-md-12">

                  <h4 v-html='post.title.rendered'></h4>

                  <h5 class="text-muted">Published {{post.date | prettyDate}} on {{source.name}}</h5>
                  
                </div>

                <div class="col-md-12">

                  <div v-html='post.excerpt.rendered'></div>

                  <div class="text-right">

                    <a v-bind:href="post.link" target="_blank">Keep Reading</a>

                  </div>

                </div>

              </div>

            </div>

          </div>

        </div>

      </div>
    
    </div>
    
  </div>

</div>
            
          
!

CSS

            
              // Basic body styles
body{
  color: #494949;
  font-family: -apple-system, BlinkMacSystemFont, 
    "Segoe UI", "Roboto", "Oxygen", 
    "Ubuntu", "Cantarell", "Fira Sans", 
    "Droid Sans", "Helvetica Neue", sans-serif !important;
  line-height: 1.4;
  margin-top: 1rem;
}


// Overrides for WordPress.org
.sharedaddy{
  display: none;
}
            
          
!

JS

            
              
var app = new Vue({
  
  el : '#app',
  
  data : function(){
    return {
      searchQuery : '',
      sources : [
        {
          name: 'css-tricks.com',
          on: true,
          url : 'https://css-tricks.com/wp-json/wp/v2/posts?per_page=3',
          posts : []
        },
        {
          name: 'poststatus.com',
          on: true,
          url : 'https://poststatus.com/wp-json/wp/v2/posts?per_page=3',
          posts : []
        },
        {
          name: 'torquemag.io',
          on: true,
          url : 'https://torquemag.io/wp-api/wp/v2/posts?per_page=3',
          posts : []
        },
        {
          name: 'wordpress.org',
          on: true,
          url : 'https://wordpress.org/news/wp-json/wp/v2/posts?per_page=3',
          posts : []
        },
        {
          name: 'wpmudev.org',
          on: true,
          url : 'https://premium.wpmudev.org/blog/wp-json/wp/v2/posts?per_page=3',
          posts : []
        },
        {
          name: 'wptavern.com',
          on: true,
          url : 'https://wptavern.com/wp-json/wp/v2/posts?per_page=3',
          posts : []
        }
      ]
    }
  },
  
  filters: {
    
    // Using Moment.js to convert post date to a readable format
    prettyDate: function(value){
      
      // Return if date is empty
      if(!value) return '';
      
      // Convert date to Moment.js
      var date = moment.utc(value);
      
      // Return formatted date
      return date.format("MMM DD, YYYY,");
      
    }
    
  },
  
  methods: {
    
    // Load all posts from separate APIs
    loadPosts : function(){
      
      var self = this;
      
      self.sources.forEach(function(source, index){
        
        self.$delete(source, 'posts');
        
        //Verify that source should be displayed
        if(source.on){

          var searchUrl = self.generateUrl(source);
          
          // Get API with vue-resource     
          self.$http.get(searchUrl).then(function(response)  {

            self.$set(source, 'posts', response.data);

          }, function(response) {

            console.log('Error');

          });
        
        }
        
      });
      
    },
    
    // Generate the search URL
    generateUrl : function(source){
      
      var self = this;
          
      // Add search parameters.
      if(self.searchQuery){

        return source.url + '&search=' + encodeURI(self.searchQuery);

      }else{

        return source.url;

      }      
      
    }
    
  },
  
  mounted : function(){
    
    this.$nextTick(function(){

      // Load posts on initial page load
      this.loadPosts();
      
    });
    
  }
  
});
            
          
!
999px

Console