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
Related
How to show category hierarchy in woocommerce
What i want is something like this
given with this slug
domain.com/category/balloons/helium-baloons
the hierarchy would be:
Baloons
Helium Baloons
Decor
Weddings
To simplified the idea it should be something like this
you can use wp_list_categories function with arguments
wp_list_categories(
array(
'taxonomy'=>product_cat'
)
);
use the below code to achive this functionality
$myterms = get_terms( 'category', array( 'parent' => 0 ) );
$current_term = get_queried_object();
echo '<ul>';
foreach($myterms as $term){
echo '<li> '.$term->name.'';
if($term->term_id == $current_term->parent){
$child_terms = get_terms( 'category', array('child_of'=>$term->term_id) );
if($child_terms){
echo '<ul>';
foreach($child_terms as $child_term){
echo '<li> '.$child_term->name.'';
}
echo '</ul>';
}
}
echo '</li>';
}
echo '</ul>';
also if you have big child heirarchi you can make this code recursive.
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>
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'm trying to get the id of the latest post in each category, and use that id to get the meta info and thumbnail and display it next to the corresponding category. I'm just not sure how to do it.
I've been trying this code, but it isn't working for me:
<?php
$args=array(
'orderby' => 'name',
'order' => 'ASC'
);
$categories=get_categories($args);
foreach($categories as $category) : ?>
<?php $randpost = get_posts(
array(
'numberposts' => 1,
'category' => array( get_query_var($category->id)),
));
$randpostid = ($randpost->ID);
?>
<?php echo '<h2 class="newsitem"><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </h2> '; ?>
<?php echo '<p>'. $category->count . ' nummer</p>'; ?>
<strong>Stad:</strong>
<?php $city = get_post_meta($randpostid, 'city', true); ?>
<?php echo $city ?>
<?php endforeach; ?>
What am I doing wrong?
Everything you have looks correct, except for one line. You need to change:
'category' => array( get_query_var($category->id)),
To:
'category' => $category->cat_ID
Category objects do not have an 'id' property, but rather a 'cat_ID' property.
ADDITIONALLY: If for whatever reason that doesn't solve your problem, the only other thing I can think of would be to change this line:
$randpostid = ($randpost->ID);
To:
$randpostid = ($randpost[0]->ID);
get_posts() returns an array, but I'm not sure if it is in array format when single posts are returned. Either way, the first code change is a must, and the second is probably needed.
If you're just after displaying information from the most recent post you could probably do this in a much more simple fashion. Something like this in your page template should work (untested):
EDIT
Answer edited in light of OP comment:
<?php
$cat_args = array('orderby' => 'name','order' => 'ASC'); //for parameters see http://codex.wordpress.org/Function_Reference/get_categories
$categories=get_categories($cat_args);
foreach($categories as $category) { // for each category we as for the most recent post
$post_args = array('numberposts' => 1, 'category' => $category, 'orderby' => 'post_date', 'order' => 'DESC', );
$posts_array = get_posts( $post_args );
foreach($lastposts as $post) : setup_postdata($post); //Use setup_postdata to access parts of the object using regular WP template tags ?>
<?php post_id = get_the_ID(); // or you could just use $post->ID ?>
<!--Do your stuff here-->
<?php endforeach; ?>
<?php } ?>