How to block a category from one user and enable the category for the rest of the users

We will use the my_force_login() function that will force the users to login when they press on a specific category. Wp-login.php if ( ( empty( $redirect_to ) || $redirect_to == ‘wp-admin After this condition ends write this part if ($user_name == “student”) { wp_safe_redirect(“http://www.domain.co.il /?cat=4”); } else { wp_safe_redirect($redirect_to); } Inside the condition write the … Read more

WordPress Ordering Problem. How to fix ordering 1-10-100 issue?

Based on the research I’ve done on this, using term meta data is a better all around approach to ordering terms. However, I was able come up with this snippet which does sort the terms by their name, numerically: add_filter( ‘terms_clauses’, ‘wpse247680_terms_clauses’, 10, 3 ); function wpse247680_terms_clauses( $pieces, $taxonomies, $args ) { // Bail if … Read more

How do I set a specific template for sub-categories?

I would recommend using the category_template filter – just check if the current category is an ancestor of 67: function wpse_179617_category_template( $template ) { if ( cat_is_ancestor_of( 67, get_queried_object_id() /* The current category ID */ ) ) $template = locate_template( ‘category-slider.php’ ); return $template; } add_filter( ‘category_template’, ‘wpse_179617_category_template’ );

How to add style to category link?

You can use the_category filter to hook a callback function like this: add_filter(‘the_category’,’add_class_to_category’,10,3); function add_class_to_category( $thelist, $separator, $parents){ $class_to_add = ‘my-category-class’; return str_replace(‘<a href=”‘, ‘<a class=”‘ . $class_to_add . ‘” href=”‘, $thelist); }

How to display child categories of current category’s parent category?

I now realize there’s an easier way to do this: <?php if (is_category( )) { $thiscat = get_category( get_query_var( ‘cat’ ) ); $catid = $thiscat->cat_ID; $parent = $thiscat->category_parent; if (!empty ($parent) ) { //child category pages $catlist = get_categories( array( ‘child_of’ => $parent, ‘orderby’ => ‘id’, ‘order’ => ‘DESC’, ‘exclude’ => $catid, ‘hide_empty’ => ‘0’ … Read more

Name of last category level for a post

Try the following. I’ve used get_the_terms so it is readily extendible to any taxonomy. This will return an array of terms (event if it contains just one). For instance, the post may have terms a,b,c,d,e in the relationship: a > b > c (parent > child) d > e In which case the following will … Read more

Assign parent category to all posts that are already assigned to child category

be careful and only run it once $_query = new WP_Query( array( ‘post_type’ => ‘post’, ‘posts_per_page’ => -1, ) ); function complete_parent_category( $term_id, $dep=array() ) { $category = get_term( $term_id, ‘category’ ); $dep[ $category->term_id ] = $category->slug; if( $category->parent ) { $dep = complete_parent_category( $category->parent, $dep ); } return $dep; } echo ‘<pre>’; while( $_query->have_posts() … Read more