Revision 298 is a pre-publication revision. (Viewing current revision instead.)

WordPress get_categories queries

Get only the top level categories. There is no depth parameter on the get_categories function, this query and foreach loop will ensure only toplevel categories are displayed: [php] <?php $args=array( 'taxonomy' => 'product_cat', 'hierarchical' => '1', 'hide_empty' => '0', 'pad_counts' => '1', 'orderby' => 'term_order' ); $categories=get_categories($args); foreach($categories as $category) { if($category->parent == 0 ){ echo '<p><a href="'. get_term_link($category->slug, 'product_cat') .'" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> '. $category->count . '</p> '; } } ?> [/php] This query will get just the child categories of a given parent: [php] <?php $categoryID = $category->term_id; $args=array( 'taxonomy' => 'product_cat', 'child_of' => $categoryID, 'hide_empty' => '0', 'orderby' => 'term_order' ); $categories=get_categories($args); foreach($categories as $category) { echo '<p>- <a href="'. get_term_link($category->slug, 'product_cat') .'" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> '. $category->count . '</p> '; } ?> [/php] In conjunction with the 'Custom Taxonomy Order' plugin and an orderby parameter of 'term_order', the two queries above can be combined to return a custom ordered list of categories with multiple levels. [php] <?php $args=array( 'taxonomy' => 'product_cat', 'hierarchical' => '1', 'hide_empty' => '0', 'pad_counts' => '1', 'orderby' => 'term_order' ); $categories=get_categories($args); foreach($categories as $category) { if($category->parent == 0 ){ echo '<p><a href="'. get_term_link($category->slug, 'product_cat') .'" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> '. $category->count . '</p> '; $categoryID = $category->term_id; $args=array( 'taxonomy' => 'product_cat', 'child_of' => $categoryID, 'hide_empty' => '0', 'orderby' => 'term_order' ); $categories=get_categories($args); foreach($categories as $category) { echo '<p>- <a href="'. get_term_link($category->slug, 'product_cat') .'" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> '. $category->count . '</p> '; } } } ?> [/php]

Revisions

  • November 5, 2012 @ 23:31:25 [Current Revision] by PeterLugg
  • November 5, 2012 @ 23:26:27 by PeterLugg

Revision Differences

November 5, 2012 @ 23:26:27Current Revision
Content
Unchanged: Get only the top level categories. There is no depth parameter on the get_categories function, this query and foreach loop will ensure only toplevel categories are displayed:Unchanged: Get only the top level categories. There is no depth parameter on the get_categories function, this query and foreach loop will ensure only toplevel categories are displayed:
 Added: [php]
 Added: &lt;?php
 Added: $args=array(
 Added: 'taxonomy' =&gt; 'product_cat',
 Added: 'hierarchical' =&gt; '1',
 Added: 'hide_empty' =&gt; '0',
 Added: 'pad_counts' =&gt; '1',
 Added: 'orderby' =&gt; 'term_order'
Deleted: &nbsp;Added: );
 Added: $categories=get_ categories($args);
 Added: foreach($categories as $category) {
 Added: if($category-&gt;parent == 0 ){
 Added: echo '&lt;p&gt;&lt;a href=&quot;'. get_term_link( $category-&gt;slug, 'product_cat') .'&quot; title=&quot;' . sprintf( __( &quot;View all posts in %s&quot; ), $category-&gt;name ) . '&quot; ' . '&gt;' . $category-&gt; name.'&lt;/a&gt; '. $category-&gt;count . '&lt;/p&gt; ';
 Added: }
 Added: }
 Added: ?&gt;
 Added: [/php]
Unchanged: This query will get just the child categories of a given parent:Unchanged: This query will get just the child categories of a given parent:
Deleted: &nbsp; 
 Added: [php]
 Added: &lt;?php
 Added: $categoryID = $category-&gt;term_id;
 Added: $args=array(
 Added: 'taxonomy' =&gt; 'product_cat',
 Added: 'child_of' =&gt; $categoryID,
 Added: 'hide_empty' =&gt; '0',
 Added: 'orderby' =&gt; 'term_order'
 Added: );
 Added: $categories=get_ categories($args);
Deleted: In conjunction with the 'Custom Taxonomy Order' plugin and The two queries above can be combined to return a custom ordered list of categories with multiple levels.Added: foreach($categories as $category) {
 Added: echo '&lt;p&gt;- &lt;a href=&quot;'. get_term_link( $category-&gt;slug, 'product_cat') .'&quot; title=&quot;' . sprintf( __( &quot;View all posts in %s&quot; ), $category-&gt;name ) . '&quot; ' . '&gt;' . $category-&gt; name.'&lt;/a&gt; '. $category-&gt;count . '&lt;/p&gt; ';
 Added: }
 Added: ?&gt;
 Added: [/php]
 Added: In conjunction with the '<a href="http:// wordpress.org/ extend/plugins/ order-up-custom- taxonomy-order/" target="_blank">Custom Taxonomy Order</a>' plugin and an orderby parameter of 'term_order', the two queries above can be combined to return a custom ordered list of categories with multiple levels.
 Added: [php]
 Added: &lt;?php
 Added: $args=array(
 Added: 'taxonomy' =&gt; 'product_cat',
 Added: 'hierarchical' =&gt; '1',
 Added: 'hide_empty' =&gt; '0',
 Added: 'pad_counts' =&gt; '1',
 Added: 'orderby' =&gt; 'term_order'
 Added: );
 Added: $categories=get_ categories($args);
 Added: foreach($categories as $category) {
 Added: if($category-&gt;parent == 0 ){
 Added: echo '&lt;p&gt;&lt;a href=&quot;'. get_term_link( $category-&gt;slug, 'product_cat') .'&quot; title=&quot;' . sprintf( __( &quot;View all posts in %s&quot; ), $category-&gt;name ) . '&quot; ' . '&gt;' . $category-&gt; name.'&lt;/a&gt; '. $category-&gt;count . '&lt;/p&gt; ';
 Added: $categoryID = $category-&gt;term_id;
 Added: $args=array(
 Added: 'taxonomy' =&gt; 'product_cat',
 Added: 'child_of' =&gt; $categoryID,
 Added: 'hide_empty' =&gt; '0',
 Added: 'orderby' =&gt; 'term_order'
 Added: );
 Added: $categories=get_ categories($args);
 Added: foreach($categories as $category) {
 Added: echo '&lt;p&gt;- &lt;a href=&quot;'. get_term_link( $category-&gt;slug, 'product_cat') .'&quot; title=&quot;' . sprintf( __( &quot;View all posts in %s&quot; ), $category-&gt;name ) . '&quot; ' . '&gt;' . $category-&gt; name.'&lt;/a&gt; '. $category-&gt;count . '&lt;/p&gt; ';
 Added: }
 Added: }
 Added: }
 Added: ?&gt;
 Added: [/php]

Note: Spaces may be added to comparison text to allow better line wrapping.

Tags:

No comments yet.

Leave a Reply