WordPress REST API, Get Random Post endpoint

In the following snippet, you can register a new endpoint in the API to access random posts. This can be configuring to work with any post-type.  You’ll just updating the get_posts array accordingly.  Enjoy! You’ll want to put this inside your functions.php inside your Theme, or create a plugin.  If you’re extending the WordPress REST API … Continued

How to get the Featured Image URL in WP-API V2

Formerly as seen below you could use a function to adjust the end point so it would contain a new field for the ‘featured-image‘ url.  The Rest-API v2 has now been updated so you can add ‘?_embed‘ to the parameters in your query and you’ll get exactly what you need. or You can use the following … Continued

Add Custom Post Types to WP-API V2

You’ll notice if Version 2 of the WP-API (which is phenomenal by the way), you’ll need to add a couple things here or there to grab custom data.  Grab the function and below and throw that into your functions.php inside your theme. function hijack_posts_types() { global $wp_post_types; // Add CPT slugs here, comma separated $arr = … Continued

Add Featured Image as a Background Image

Below is how you would use the featured image as a background image (also with an if statement so you can have a fallback image). <?php if (has_post_thumbnail( $post->ID ) ): ?> <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), ‘single-post-thumbnail’ ); $image = $image[0]; ?> <?php else : $image = get_bloginfo( ‘stylesheet_directory’) . ‘/images/default_cat_img.jpg’; ?> … Continued

Add Sequential Classes to loop, WordPress

Below are to examples you will probably need the most.  Adding a sequential class to a for loop, and adding a sequential class to a while loop For Loop <?php global $post; $cntr = 1; $myposts = get_posts(‘category=1&numberposts=3&order=des’); foreach($myposts as $post) : setup_postdata($post);?> <li class=”<?php echo “post-” . $cntr; ?>”><a href=”<?php the_permalink() ?>”><?php the_title() ?></a></li> … Continued

WP REST API (WP API), Tips and Tricks

I’m starting to use the WP API more and more, and have definitely needed some help along the way.  Here are some tips and tricks I’ve been able to pick up and use. Get multiple types of posts / pages / etc /wp-json/posts?type[]=post-type&type[]=post-type Filter by specific number of posts per page /wp-json/posts?filter[posts_per_page]=4&page=2 (number of posts … Continued