Skip to content

Instantly share code, notes, and snippets.

@yoren
yoren / functions.php
Created Jul 24, 2015
Testing if a post is in a descendant category
View functions.php
<?php
/**
* Tests if any of a post's assigned categories are descendants of target categories
*
* @param int|array $cats The target categories. Integer ID or array of integer IDs
* @param int|object $_post The post. Omit to test the current post in the Loop or main query
* @return bool True if at least 1 of the post's categories is a descendant of any of the target categories
* @see get_term_by() You can get a category by name or slug, then pass ID to this function
* @uses get_term_children() Passes $cats
* @uses in_category() Passes $_post (can be empty)
@yoren
yoren / class-wp-rest-posts-controller-2.php
Last active Nov 16, 2016
Getting Posts By Taxonomy Or Meta Field With WP-API v2.0
View class-wp-rest-posts-controller-2.php
<?php
/**
* This funcion comes from the source code of WP-API:
* https://github.com/WP-API/WP-API/blob/develop/lib/endpoints/class-wp-rest-posts-controller.php#L91
*
* Get a collection of posts
*
* @param WP_REST_Request $request Full details about the request
* @return WP_Error|WP_REST_Response
*/
@yoren
yoren / main.html
Last active Dec 4, 2015
Tidy Up Your AngularJS WordPress Theme With A Service
View main.html
<!-- ... -->
<p>{{data.pageTitle}}</p>
<ul>
<li ng-repeat="post in data.posts">
<a href="blog/{{post.ID}}" ng-bind-html="post.title"></a>
<a href="blog/{{post.ID}}" ng-if="post.featured_image.attachment_meta.sizes.thumbnail.url"><img ng-src="{{post.featured_image.attachment_meta.sizes.thumbnail.url}}" alt="{{post.featured_image.title}}" /></a>
<div ng-bind-html="post.excerpt"></div>
</li>
</ul>
@yoren
yoren / functions.php
Last active Jun 24, 2016
Add posts to term JSON data
View functions.php
<?php
function my_json_prepare_term( $data, $term, $context ) {
global $wp_query;
$route = $wp_query->query['json_route'];
if ( ! preg_match( '/(terms\/.+)/', $route) )
return $data;
$args = array(
@yoren
yoren / functions.php
Last active Apr 1, 2018
Add Featured Image Thumbnail URL to wp-json/wp/v2/posts Endpoint
View functions.php
<?php
function my_rest_prepare_post( $data, $post, $request ) {
$_data = $data->data;
$thumbnail_id = get_post_thumbnail_id( $post->ID );
$thumbnail = wp_get_attachment_image_src( $thumbnail_id );
$_data['featured_image_thumbnail_url'] = $thumbnail[0];
$data->data = $_data;
@yoren
yoren / main.html
Last active Aug 29, 2015
Creating A Service in AngularJS WordPress Theme
View main.html
<search-form></search-form>
<p>Categories:</p>
<ul>
<li ng-repeat="category in data.categories">
<span ng-if="current_category_id && category.ID == current_category_id" ng-bind-html="category.name"></span>
<a ng-if="!current_category_id || category.ID != current_category_id" href="category/{{category.slug}}" ng-bind-html="category.name"></a>
</li>
</ul>
<!-- ... -->
@yoren
yoren / 404.html
Last active Mar 12, 2016
When Page Not Found In Your AngularJS WordPress Theme
View 404.html
<h1>Page Not Found</h1>
<p>Sorry, but nothing can be found at this location.</p>
@yoren
yoren / main.html
Last active Sep 16, 2015
Previous / Next Posts Links In AngularJS WordPress Theme
View main.html
<!-- ... -->
<p>{{pageTitle}}</p>
<ul>
<li ng-repeat="post in posts">
<a href="blog/{{post.ID}}" ng-bind-html="post.title"></a>
<a href="blog/{{post.ID}}" ng-if="post.featured_image.attachment_meta.sizes.thumbnail.url"><img ng-src="{{post.featured_image.attachment_meta.sizes.thumbnail.url}}" alt="{{post.featured_image.title}}" /></a>
<div ng-bind-html="post.excerpt"></div>
</li>
</ul>
<posts-nav-link prev-label="&laquo; Previous Page" next-label="Next Page &raquo;"></posts-nav-link>
@yoren
yoren / functions-get_post.php
Last active Aug 29, 2015
Get Total Post Count With found_posts in WP_Query
View functions-get_post.php
<?php
$args = array(
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'foo',
'value' => 'bar'
)
),
@yoren
yoren / content.html
Last active Dec 4, 2015
Adding Slick Carousel To Your AngularJS WordPress Theme
View content.html
<h1 ng-bind-html="post.title"></h1>
<slick dots="true" autoplay="true" slides-to-show="1" slides-to-scroll="1" init-onload="true" data="media" style="width:300px">
<div ng-if="image.is_image" ng-repeat="image in media">
<img alt="{{image.title}}" ng-src="{{image.attachment_meta.sizes.medium.url}}" />
</div>
</slick>
<div ng-bind-html="post.content"></div>