Force category choice before creating new post

Use the ‘save_post’ hook to programmatically enteract with your new post when it is created, then use the wp_set_post_terms() function to assign your term, add_action(‘save_post’,’set_post_default_category’, 10,3); function set_post_default_category($post_id, $post, $update){ if($update) return; //only want to set if this is a new post! if(‘post’ !== $post->post_type) return; //only set for post_type = post! $term = get_term_by(‘slug’, … Read more

So my theme doesn’t have a category.php file

First, if you are going to add to the theme, learn about Child Themes https://codex.wordpress.org/Child_Themes . Then, create your own Category page – use a copy of one of the templates in your theme (placed in your Child Theme) and adjust the query to do a category-type query. By creating a Child Theme, you ensure … Read more

Filter WooCommerce archive pages by an additional category

you need to add your topic to query_vars. function so306156_add_query_vars_filter($vars) { $vars[] = “topic”; return $vars; } add_filter(‘query_vars’, ‘so306156_add_query_vars_filter’); and then, in pre_get_posts, you check for that var and act accordingly: function so306156_manipulate_main_query($wp_query) { if (function_exists(‘is_woocommerce’) && !is_admin()) : if ($wp_query->is_main_query() && is_woocommerce()) { if (isset($wp_query->query_vars[‘topic’])) { $tax_query = $query->get(‘tax_query’); $tax_query[] = [ ‘taxonomy’ => … Read more

Multiple selection for wordpress widget

Hi why don’t you used multiple check-box instead of select option field. As well as you have used deprecated functions like “create_function”. I have modified your code which replaced select option with multiple check-box selection. function myCatWidget() { register_widget( ‘my_custom_cat_widget’ ); } add_action( ‘widgets_init’, ‘myCatWidget’ ); class my_custom_cat_widget extends WP_Widget { function __construct() { parent::__construct(‘my_custom_cat_widget’, … Read more

display a widget on specific category and its sub categories

you can test category identifier and parent in a widget with that : class WidgetExample extends \WP_Widget { public const IDENTIFIANT = “example1”; public const CATEGORY_ID = 25; public function __construct() { parent::__construct( self::IDENTIFIANT , “Example” ); } public function widget($args, $instance) { if (!is_category()) { return; } $currentCategory = get_queried_object(); if ( (self::CATEGORY_ID !== … Read more

Apply WordPress function on specific woocommerce product categories only

You’re trying to use $product_cat_id even though it isn’t defined anywhere. If you want to know the current category being viewed you need to use is_product_category(6), but you need to use that inside the callback function (ChangeSelectTitle()), because WordPress hasn’t determined whether it’s a category archive yet when it runs functions.php. function ChangeSelectTitle($woocommerce_product_categories_widget_args) { if … Read more

ow to display the number of posts in a category

There is a default archive.php template in all WordPress themes which has a static layout that looks like the blog and which is used any time a category archive is directly accessed using the category link or URL, that is where you have to put your code, in the place that you want. So, basically … Read more

Set category page title in custom theme [closed]

In your functions.php of your theme file add the following code, taken from Remove “Category:”, “Tag:”, “Author:” from the_archive_title. add_filter( ‘get_the_archive_title’, function ($title) { if ( is_category() ) { //being a category your new title should go here $title = single_cat_title( ”, false ); } elseif ( is_tag() ) { $title = single_tag_title( ”, false … Read more

Show child taxonomies (that have posts) of the current parent taxonomy

Props to Sally for improving on this to show post count $term = get_queried_object(); $children = get_terms( $term->taxonomy, array( ‘parent’ => $term->term_id, ‘hide_empty’ => false ) ); if ( $children ) { foreach( $children as $subcat ) { echo ‘<li><a href=”‘ . esc_url(get_term_link($subcat, $subcat->taxonomy)) . ‘”>’ . // wrapped $subcat->name . ‘ (‘ . $subcat->count … Read more