With or without posts within. I need to display the total number of categories in my Wordpress theme.
Easy way to count category is:
1) firstly fetch all category from wordpress
2) count them using simple php funciton
complete code will be like:
<?php $args = array( 'parent' => 0, 'hide_empty' => 0 );
$categories = get_categories( $args );
echo "Total categories : ".count( $categories ); ?>
i used this code always :)
<?php
$args = array(
'get' => 'all',
'hide_empty' => 0
);
$categories = get_categories( $args );
echo count( $categories );
?>
A more straight forward way (perhaps faster?)
global $wpdb;
$categories_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->terms" );
echo "<p>Categories count is {$categories_count}</p>";
Retrieve list of category objects get_categories():
<ul class="list-group">
<?php
$categories = get_categories();
$i = 0;
foreach ($categories as $category) {
$i++;
echo '<li class="list-group-item"><span class="text-muted float-left">' . $category->category_count . '</span>' . $category->name . '</li>';
}
?>
</ul>
Related
I want to count the number of posts in each taxonomy. The below code only works if there is on 2nd level taxonomy. Based on the image. It's returning BMW Post count 2 but for Mercedez, it should return Post Count 4. Adding Model 1 & Model 2 post count.
$term = get_queried_object();
$term_id = $term->term_id;
$taxonomy_name = $term->taxonomy;
$termchildren = get_terms($taxonomy_name,array('parent' => $term_id));
foreach ( $termchildren as $child ) { ?>
<h3>
<?php echo $child->name;?>
<span class="blog_bg_blue">Post Count: <?php echo $child->count; ?></span>
</h3>
<?php} ?>
Are you looking for something like this?
Script:
$args = array(
'orderby' => 'slug',
'parent' => 0
);
$categories = get_categories( $args );
foreach( $categories as $category ){
print $category->name.' - '.$category->category_count.'</br>';
}
Post arrangement in CMS:
Output:
I would like to display a drop down menu for products in a category.
<select>
<option value="CODE HERE">Volvo</option>
</select>
So according to Wordpress coding..
<?php
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
Okay so I investigated further and I am looking to do a single page template according to https://developer.wordpress.org I am using a child theme for Storefront which is called NOVA WP.
To make this "single page template" I copied page.php and renamed it to page-buildit.php
This is Mypage in which I actually editing the code. I did copy the code but it turns out blank
found this: WooCommerce: Create a shortcode to display product categories
but my undestanding is we cant do this anymore with the new wordpress version.
<?php
$args = array(
'order' => 'ASC',
'hide_empty' => $hide_empty,
'include' => $ids,
'posts_per_page' =>'-1'
);
$product_categories = get_terms( 'product_cat', $args );
echo "<select>";
foreach( $product_categories as $category ){
echo "<option value = '" . esc_attr( $category->slug ) . "'>" . esc_html( $category->name ) . "</option>";
}
echo "</select>";
?>
Check this out. This is the way to get product categories.!
You can also use the function wp_dropdown_categories to make your code simpler.
To get a dropdown of categories of products you can write like this.
$args = array('hide_empty'=> 0,
'taxonomy'=> 'product_cat',
'hierarchical'=>1);
wp_dropdown_categories($args);
Or if you want to hold the output in a variable you can use the argument 'echo'=>0 and then echo the variable to get the same output.
$args = array('hide_empty'=> 0,
'taxonomy'=> 'product_cat',
'hierarchical'=>1,
'echo'=>0);
$cats = wp_dropdown_categories($args);
echo $cats;
Okay so here is how I solved it, with the help of Hemnath mouli, I already gave you the credit for the answer but I wanted to publish the products inside a category in a dropbox.
$args = array(
'posts_per_page' => -1,
'product_cat' => 'motherboard',
'post_type' => 'product',
'orderby' => 'title',
);
$products = new WP_Query( $args );
echo "<select>";
foreach ( $products as $product ) {
$products->the_post();
?>
<option value="<?php the_permalink(); ?>"> <?php the_title(); ?>
<?php
}
echo "</select>";
?>
Now I will need to show the image of this product after selecting it.
My code:
<?php
$category = get_the_category();
echo $category[0]->cat_name;
?>
<?php
$category = get_the_category();
echo $category[1]->cat_name;
?>
I want get two category as links but exclude "uncategorized" category.
Try below code:
<?php
$args = array(
'include' => '1,2'
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
echo '' . $category->name . '<br/>';
}
?>
In above code change 'include' => '1,2' and add the category that you want to include.
Find more here.
Updated :
If you want to get only first two category and exclude Uncategorized from that two category, then you can try below code :
<?php
$i=0;
$args = array(
'orderby' => 'id'
);
$my_category = get_categories($args);
foreach($my_category as $mcategory){
if($i==0 || $i==1){
if($mcategory->name != 'Uncategorized'):
echo '' . $mcategory->name . '<br/>';
endif;
}
$i++;
}
?>
Here I have added one dummy condition to make it work.
I have below code here. It's display that I want category names and description as well. Now I need to display post that they have inside their categories. How I do it?
<?php
$args = array(
'orderby' => 'id',
'hide_empty'=> 0,
'child_of' => 10, //Child From Boxes Category
);
$categories = get_categories($args);
foreach ($categories as $cat) {
echo '<div class="one_fourth">';
echo '<h1 class="valignmiddle uppercase title-bold">'.$cat->name.'<img src="'.$cat->term_icon.'" alt="" class="alignleft"/>'.'<br />'.'<span class="solutions">'.$cat->description.'</span>'.'</h1>';
$post = array( 'posts_per_page' => 5, 'offset'=> 1, 'category' => 10 );
$posts_array = get_posts( $post );
echo '</div>';
}
?>
If there have any other way to get child category post and display child category name and posts in loop. Please let me to know here.
After I solved this. I think this will help for you.
<?php
$args = array(
'orderby' => 'id',
'hide_empty'=> 0,
'child_of' => 5, //Child From Boxes Category
);
$categories = get_categories($args);
foreach ($categories as $cat) {
echo '<div style="width:20%;float:left;">';
echo '<h1 class="valignmiddle uppercase title-bold">'.$cat->name.'<img src="'.$cat->term_icon.'" alt="" class="alignleft"/>'.'<br />'.'<span class="solutions">'.$cat->description.'</span>'.'</h1>';
//echo '<br />';
$args2= array("orderby"=>'name', "category" => $cat->cat_ID); // Get Post from each Sub-Category
$posts_in_category = get_posts($args2);
foreach($posts_in_category as $current_post) {
echo '<span>';
?>
<li type='none' style='list-style-type: none !important;'><?='+ '.$current_post->post_title;?></li>
<?php
echo '</span>';
}
echo '</div>';
}
?>
Write for get all parent category
Now do a foreach loop for them and get their child category.
Now under above foreach write another foreach using this get post for child category
$parent_cats = get_categories($args);
foreach ( $parent_cats as $parent_cat) {
$child_cats = some wp functions to get child cats of current parent category
foreach ( $child_cats as $child_cat ) {
$child_cat_post = get the post of child category
}
}
Helpful links:
http://codex.wordpress.org/Function_Reference/get_categories
http://codex.wordpress.org/Template_Tags/get_posts
Wordpress has <?php wp_dropdown_categories( $args ); ?>
Example Usage (Dropdown without a Submit Button using JavaScript)
<li id="categories">
<h2><?php _e( 'Posts by Category' ); ?></h2>
<?php wp_dropdown_categories( 'show_option_none=Select category' ); ?>
<script type="text/javascript">
<!--
var dropdown = document.getElementById("cat");
function onCatChange() {
if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
location.href = "<?php echo esc_url( home_url( '/' ) ); ?>?cat="+dropdown.options[dropdown.selectedIndex].value;
}
}
dropdown.onchange = onCatChange;
-->
</script>
</li>
Other examples can be found on the Codex site - https://codex.wordpress.org/Function_Reference/wp_dropdown_categories
I'm not sure why this isn't working. I want to exclude categories 406, 982, and 1319 and their children from the list. I don't see any other arguments on wp codex. Does 'exclude' automatically exclude children? Is there another way to do this?
**Edit: I can't even limit the results to 10.
<?php
$args = array(
'exclude' => '406,982,1319',
'#' => '10'
);
$sidebar_artists = get_categories($args);
echo "<ul>";
foreach ($sidebar_artists as $sidebar_artist) {
echo '<li class="cat-item">' . $sidebar_artist->category_nicename . '</li>';
}
echo "</ul>";
?>
Set parent to 0:
<?php
$args = array(
'orderby' => 'name',
'parent' => 0
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
echo '' . $category->name . '<br/>';
}
?>
https://codex.wordpress.org/Function_Reference/get_categories
it looks like it should be working but did you try any alternative ? like is_category in if/else? or try exclusion by category sluginstead of IDs for sake of debugging....
WP Codex Reference for is_category