I am trying to exclude posts from my index page. I have the below code in my functions.php to accomplish that:
function my_cat_exclude($query) {
if ($query->is_home) {
//$query->set('cat','-1,-3,-4,-5,-6,-7,-8,-9,-10');
$query->set('cat','11');
}
return $query;
}
add_filter('pre_get_posts','my_cat_exclude');
I have 12 categories in my blog. I would like to show posts from only one category (category id 11). I don't want to show the rest on homepage.
If I use $query->set('cat','-1,-3,-4,-5,-6,-7,-8,-9,-10'); it doesn't show posts which shares the same category with category id 11. For example, if a post is assigned to two categories category id 5 and category 11, then, it doesn't shows that post in my home page. I would like to show all posts belong to category id 11 on my home page.
If I use $query->set('cat','11'); It shows only category with category id 11. however, it removes the other category posts, custom menus from sidebar widget. Also, removes the menu items from top menu.
Please help.
Thanks
I made some changes to your code and this works fine on my dev site.
function my_cat_exclude() {
if ( is_home() ) {
query_posts( 'cat=11' );
}
}
add_filter('pre_get_posts','my_cat_exclude');
Related
I'm creating a new post without selecting category but whenever new post is created at that time it automatically selects category. I want to disable auto select category.
Can anyone help me?
The best way of removing default category is to change a parameter is to change the parameter 'default_category' in your 'wp_options' table.
Set the parameter 'default_category' to 0. Note that it won't change the category displayed in your dashboard in the Writing settings, but it will prevent Wordpress to set a default term to your post.
Wordpress doesn't require a term or category to be set by default. It's just not true ;)
Every single post created in Wordpress will be assigned to a category by default. Even if you don't create categories, Posts will be assigned to a default category called "Uncategorized". This is how the Wordpress system works.
First of all, can you explain to us why you don't want to assign categories?
is this because you don't want the category slug added to the post URL? If this is the reason, there are so few ways to achieve this.
This plugin removes the CATEGORY slug from the post URL.
https://wordpress.org/plugins/remove-category-url/
Few other references to removing Category slug from URL without plugins:
https://jonnyjordan.com/blog/how-to-remove-category-from-your-urls-in-wordpress/
Remove category & tag base from WordPress url - without a plugin
Wordpress post will have 1 CATEGORY. This is how Wordpress works. Maybe if you want to remove the default category but if you want to select a category manually then we can achieve that with some custom coding. But you cannot have a WordPress post without assigning a category to it.
The below code will remove the default category when another category is set to a post. Make sure you have set the default category as Uncategorized.
//remove default category (uncategorized) when another category has been set
function remove_default_category($ID, $post) {
//get all categories for the post
$categories = wp_get_object_terms($ID, 'category');
//if there is more than one category set, check to see if one of them is the default
if (count($categories) > 1) {
foreach ($categories as $key => $category) {
//if category is the default, then remove it
if ($category->name == "Uncategorized") {
wp_remove_object_terms($ID, 'uncategorized', 'category');
}
}
}
}
//hook in to the publsh_post action to run when a post is published
add_action('publish_post', 'remove_default_category', 10, 2);
Do let me know if this helps.
On the woocommerce shop catalogue page is it possible to have the 'add to cart' button default to six rather than one, but only for a specific category? Selling wine and want it to add 6 bottles at a time, but also selling other items that will be sold individually.
Add this code to functions.php in your theme. Keep in mind that if you update your theme, this edit might be lost. So either save it somewhere to add it again or create a child theme and add it there.
add_filter("woocommerce_quantity_input_args", function($args, $product){
if(!is_cart() && has_term("wine-bottles", "product_cat", $product->get_id())) {
$args['input_value'] = 6;
}
return $args;
}, 10, 2);
Replace "wine-bottles" with the actual name, slug or ID of your category.
This will make the default quantity value be 6 for all products in the specified category on all pages where they're displayed.
I'm using "Recently Viewed Posts" widget for my website showing what visitor recently visited post/page, by default the widget show all category posts from the list, I need a way to hide a post category from from the widget.
Anyways here's my code.
<?php
function exclude_widget_categories($args){
$exclude = “3,6,18”; // The IDs of the excluding categories
$args[“exclude”] = $exclude;
return $args;
}
add_filter(“widget_categories_args”,”exclude_widget_categories”);
?>
I Took that from here: Wordpress - Exclude a category from appearing in menu or sidebar widgets
I'm building a shop with Woocommerce and using WooCommerce Product Category widget. I have set many product categories with subcategories. One of these categories is "Posters" and has several subcategories, like "Star signs", "Travel", "Nature"…
By default Woocommerce only shows the parent categories which is good. If I click a category "posters", I'm redirected to "posters" category archive page and the widget displays all "Posters" children subcategories and it's perfect.
Now, if I click on one of these "Posters" children subcategories, I'm redirected to the respective archive page, but my navigation doesn't show all other "Posters" children subcategories anymore.
The question:
How do I get all parent category and "sibling" subcategories while browsing a subcategory?
Random example of the standard navigation when collapsed:
Phone Cases
Mugs
Pillows
Posters
Shirts
Stickers
Example of navigation when "Posters" has been clicked:
Phone Cases
Mugs
Pillows
Posters
--Star Signs
--Travel
--Nature
--Abstract
--Typography
Shirts
Stickers
When a subcategory is clicked, e.g. "Nature", the navigation returns to look like the first example given, all collapsed. But I want it to stay expanded like in the second example.
Bellow in the screenshot are my settings for Woocommerce Product Category widget:
Any help will be appreciated.
Your settings are correct. The code below is targeting archives category pages only and it will display now all children subcategories in the Woocommerce Product Category widget, for the current subcategory:
add_filter('woocommerce_product_categories_widget_args', 'widget_product_categories_list_args', 10, 1);
function widget_product_categories_list_args( $list_args ) {
global $wp_query;
// Only for category archives pages
if ( is_tax( $list_args['taxonomy'] ) ):
// Get current category
$current_cat = $wp_query->queried_object;
// Get all Included category terms IDs in the widget
$included_ids = explode( ',', $list_args['include'] );
// Get All Childrens Ids from parent term or from current term
if($current_cat->parent != 0 )
$childrens = get_term_children( $current_cat->parent, $list_args['taxonomy'] );
else
$childrens = get_term_children( $current_cat->term_id, $list_args['taxonomy'] );
// Loop through Children term Ids and add them to existing included ones
foreach( $childrens as $child )
$included_ids[] = $child;
// Replace included product category term IDs in the $args array
$list_args['include'] = $included_ids;
endif;
return $list_args;
}
Code goes in function.php file of the active child theme (or active theme).
Tested and works.
I`m trying to create categories and sub categories for my products . So far it look like this:
-Furniture is a subcategory of Products . On furniture settings i choose Display type to show Subcategories . Like the image :
So my problem now is when i visit Furniture sub category , it shouldnt show all the subcategories of furniture in boxes ? All i get is this :
I want when i visit -Furniture to show all its categories in boxes like the image above . Its only show the Living sub category .
Thank you
You have to change the woocommerce settings . Go to
Woocommerce setting -> Products -> Display ->
Shop page display -> Select Show both
Default Category display -> Select Show both.
I hope it may help you.
Ok it seems that if you dont have any products into the subcategories it will not show them . So it seems that why i couldnt see any subcategories when i choose -Furniture and i could only see the Living subcategory .
Thank you
Probably you could try this into your function.php
add_filter( 'woocommerce_product_subcategories_hide_empty', 'show_empty_categories', 10, 1 );
function show_empty_categories ( $show_empty ) {
$show_empty = true;
// You can add other logic here too
return $show_empty;
}
also you could checkout following link for more in-depth understanding
show-subcategories-link