Skip to content

Instantly share code, notes, and snippets.

View yoren's full-sized avatar

Yoren Chang yoren

View GitHub Profile
@yoren
yoren / functions.php
Created July 24, 2015 15:59
Testing if a post is in a descendant category
<?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 November 16, 2016 15:06
Getting Posts By Taxonomy Or Meta Field With WP-API v2.0
<?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 December 4, 2015 10:44
Tidy Up Your AngularJS WordPress Theme With A Service
<!-- ... -->
<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 June 24, 2016 13:41
Add posts to term JSON data
<?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 September 14, 2022 21:02
Add Featured Image Thumbnail URL to wp-json/wp/v2/posts Endpoint
<?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 August 29, 2015 14:21
Creating A Service in AngularJS WordPress Theme
<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 March 12, 2016 23:54
When Page Not Found In Your AngularJS WordPress Theme
<h1>Page Not Found</h1>
<p>Sorry, but nothing can be found at this location.</p>
@yoren
yoren / main.html
Last active September 16, 2015 18:05
Previous / Next Posts Links In AngularJS WordPress Theme
<!-- ... -->
<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 August 29, 2015 14:17
Get Total Post Count With found_posts in WP_Query
<?php
$args = array(
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'foo',
'value' => 'bar'
)
),
@yoren
yoren / content.html
Last active December 4, 2015 10:44
Adding Slick Carousel To Your AngularJS WordPress Theme
<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>