Binders: category

Getting the list of categories available in a blog is a simple as adding one line:

{{category show="list" assign_to="categories"}}

Although the PHP code that allows accessing the categories is just a WordPress function, the {{category}} binder allow binding the data to a page and access the information directly from the HTML code.

 

Parameters

assign_to

(string) (required*) The name of the object to which you wish to bind the data output.
Default: none

show

(string) (required*) Defined what would you like to display. Valid value for “show” is list which instruct the binder to fetch a list of all categories that are not empty.
Default: none

slug

(string) (required*) The category slug for which you wish to retrieve blog posts. When using the slug parameter the binder will retrieve a list of post instead of a list of categories.
Default: none

 

Return

This binder returns an object with a data property containing the requested information.

 show=”list”

array (size=4)
  'success' => boolean true
  'message' => string 'success' (length=7)
  'action' => string 'appc_post_categories' (length=20)
  'data' => 
    array (size=8)
      0 => 
        array (size=16)
          'term_id' => string '7' (length=1)
          'name' => string 'API' (length=3)
          'slug' => string 'api' (length=3)
          'term_group' => string '0' (length=1)
          'term_taxonomy_id' => string '7' (length=1)
          'taxonomy' => string 'category' (length=8)
          'description' => string '' (length=0)
          'parent' => string '0' (length=1)
          'count' => string '1' (length=1)
          'cat_ID' => string '7' (length=1)
          'category_count' => string '1' (length=1)
          'category_description' => string '' (length=0)
          'cat_name' => string 'API' (length=3)
          'category_nicename' => string 'api' (length=3)
          'category_parent' => string '0' (length=1)
          'permalink' => string 'http://learn.simplifysites.com/category/api/' (length=44)
      1 => ...)

 

slug=”binder”

array (size=15)
  'success' => boolean true
  'action' => string 'appc_post_category_content' (length=26)
  'offset' => int 0
  'post_count' => int 1
  'found_posts' => int 1
  'total_pages' => int 1
  'size' => string '6' (length=1)
  'paged' => int 1
  'has_next' => boolean false
  'next' => null
  'has_prev' => boolean false
  'prev' => null
  'is_first' => boolean true
  'is_last' => boolean true
  'data' => 
    array (size=1)
      0 => 
        array (size=27)
          'ID' => int 27
          'post_author' => string '1' (length=1)
          'post_date' => string '2016-02-11 03:53:55' (length=19)
          'post_date_gmt' => string '2016-02-11 03:53:55' (length=19)
          'post_content' => string '<p>Understanding API</p>
' (length=25)
          'post_title' => string 'API's Overview' (length=14)
          'post_excerpt' => string 'Understanding API' (length=17)
          'post_status' => string 'publish' (length=7)
          'comment_status' => string 'open' (length=4)
          'ping_status' => string 'open' (length=4)
          'post_password' => string '' (length=0)
          'post_name' => string 'api' (length=3)
          'to_ping' => string '' (length=0)
          'pinged' => string '' (length=0)
          'post_modified' => string '2016-02-11 03:53:55' (length=19)
          'post_modified_gmt' => string '2016-02-11 03:53:55' (length=19)
          'post_content_filtered' => string '<p>Understanding API</p>
' (length=25)
          'post_parent' => int 0
          'guid' => string 'http://blog-seed-appcropolis.simplifysites.com/?p=27' (length=52)
          'menu_order' => int 0
          'post_type' => string 'post' (length=4)
          'post_mime_type' => string '' (length=0)
          'comment_count' => string '0' (length=1)
          'filter' => string 'raw' (length=3)
          'post_thumbnail' => string 'http://placehold.it/320x210' (length=27)
          'post_permalink' => string 'http://learn.simplifysites.com/api/' (length=35)
          'post_content_raw' => string 'Understanding API' (length=17)

 

Examples

 

 # EXAMPLE #1: Get post in a given category
 
 {{category slug="news" assign_to="news_posts"}}

 

# EXAMPLE #2: Get posts from a category dynamically
 
{{category slug="{$query.category_name}" assign_to="posts"}}

 

# EXAMPLE #3: get a list of categories

{{category show="list" assign_to="categories"}}
<ul>
{{foreach $categories.data as $cat}}
         <li class="cat-item cat-item-{{$cat.cat_ID}} {{if $cat.slug == $query.category_name}}current-cat{{/if}}">
            <a href="{{$cat.permalink}}" title="{{$cat.description}}">{{$cat.name}}</a> ({{$cat.count}})
        </li>
{{/foreach}}
</ul>

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *