i have code in category.php
<?php $getcatid = get_query_var('cat'); ?>
<?php $args = array('child_of' => ''.$getcatid.'', 'parent' => ''.$getcatid.'', 'hide_empty' => 0);?>
<?php $categories = get_categories($args); foreach ($categories as $cat) { ?>
<?php $getid = $cat->cat_ID; ?>
how to check quantity subcategory value as:
if $categories < 4 ($getcatid had smaller 4 sub) echo code loop
else (if $categories > 4 ) echo code loop
Any idea. Thanks
If you retrieve your categories with get_categories you can easily run count() on the result, to check how many entries there are. You can then use that number for your comparison.
Probably like this (untested):
<?php
// stripped your code out
$categories = get_categories($args);
if(count($categories) > 4) { }
else { }
?>
Related
I'm trying to show a menu with all subcategories of a current parent category, but not the parent category. Looks simple, but I'cant remove the parent names, or show subcategories in a single post.
This is the code that I'm currently using
<?php if (is_front_page() or is_single() ) { ?>
<?php } else { ?>
<?php
if (is_category() ) {
$this_category = get_category($cat);
}
?>
<?php
if($this_category->category_parent)
$this_category = wp_list_categories('orderby=id&title_li=&use_desc_for_title=1');
else
$this_category = wp_list_categories('orderby=id&depth=1&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID."&echo=0");
if ($this_category) { ?>
<ul>
<?php echo $this_category; ?>
</ul>
<?php } ?>
<?php } ?>
you can use below code in where you want to display child categories
$args = array('child_of' => $this_category->cat_ID);
$categories = get_categories( $args );
echo "<ul>";
foreach($categories as $category) {
echo "<li>".$category->name."</li>";
}
echo "</ul>"
Tested and it works well.
You can get the all the subcategories of parent one using get_terms($taxonomies, $args);
First of all declare your taxonomies like below:
$taxonomies = array(
'category',
);
Now you just need to pass $args parameter in get_terms function like below;
$args = array(
'parent' => $parent_term_id, // You can use parent id here
);
$terms = get_terms($taxonomies, $args);
This way you will get the all subcategories of applied $parent_term_id in $args
Hope this will help you. Let me know if you have any doubts.
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>
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'm trying to make a list of my recent posts that also show what category they are in right now I have
<?php $args = array( 'numberposts' => 30) ;
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<li>
<a href="'.get_permalink($recent["ID"]).'" title="Look'.esc_attr($recent["post_title"]).'" > '.$recent["post_title"].'</a> </li> '; } ?>
this displays the post but I would like it to also display the category name.
Any help would be great,
Thanks
$cats = get_the_category($recent["ID"]);
$cat_name = $cats[0]->name; // for the first category
You can try this inside the loop (if you have multiple categories)
$cats = get_the_category($recent["ID"]);
foreach($cats as $cat)
{
echo $cat->name." ";
}
I was able to get this to work using the following.
$cats[0]->name." "
So in the recent posts loop you can use it like this:
$args = array('numberposts' => 5, 'category' => '4,5' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
$cats = get_the_category($recent["ID"]);
echo '<li>' . $cats[0]->name." " . $recent["post_title"].' </li> ';
}
I was able to get a list that displays the category then the title by using the following.
<?php
$recentPosts = new WP_Query();
$recentPosts->query('showposts=30');?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<?php the_category(" "); ?>- <?php the_title(); ?>
<?php endwhile; ?><?php wp_reset_query()?>
I was never able to get the following code to work inside my original code It would always show up as "array" or Null (i'm guessing I just did not know the proper way to write it in) I was able to get it to show the category if I created a single post and just wanted to show the category with nothing else.
$cats = get_the_category($recent["ID"]);
foreach($cats as $cat){
echo $cat->name." "; }
I used the answer from Jaco, but
$cats[0]->name
gave me the first category from the array on each post. The adjustment I made was to use an incremental operator in my code and all is well.
A much simplified example:
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
$i = 0;
$cats = get_the_category($recent["ID"]);
echo $cats[$i]->name;
$i++;
}
wp_reset_query();
I'm trying to target the last item in a foreach loop but it's acting a little strange.
I've got two items in the loop, I've tried the following:
Not set a +/- for the count, this targets the first item.
Using -1 as the count also targets the first item.
Using +1 as the count doesn't target any items until you add a 3rd
item, then it works as intended.
Here's the code, can anyone help?
<?php $pages = get_pages(array('child_of' => $post->ID, 'sort_column' => 'menu_order'));
foreach($pages as $key => $post)
{
setup_postdata($post);
$fields = get_fields();
?>
<div class="event<?php if( $key == (count( $pages ) +1) ) echo 'last'; ?>">
</div>
<?php } wp_reset_query(); ?>
This is the way to do it:
<?php
$pages = get_pages(array('child_of' => $post->ID, 'sort_column' => 'menu_order'));
// keep a record of the number of pages -1
// in order to compare against 0 indexed array key
$pagesNo = count($pages)-1;
foreach($pages as $key => $post)
{
setup_postdata($post);
$fields = get_fields();
?>
<div class="event<?php if( $key == $pagesNo ) echo 'last'; ?>"></div>
<?php } wp_reset_query(); ?>
When you need to count, a for or while loop might be a better than foreach.