How do I show child categories if in a parent category, and posts if in a child?

Solved, thanks to nofearinc on the wp.org irc chat! 🙂 https://gist.github.com/mpeshev/cb1cd3c56d06017b3d87 <?php $cat_id = get_query_var(‘cat’); if( ! empty( $cat_id ) ) { $category = get_category( $cat_id, ARRAY_A ); if( ! empty( $category ) ) { // parent category if( $category[‘parent’] === ‘0’ ) { // get child IDs $terms = get_term_children( $cat_id, ‘category’ ); foreach( … Read more

Show posts of one category only with Custom Taxonomy on single.php

Since I’m not sure if you are using single.php for anything else, I am going to suggest that you copy single.php to single-14kgs.php in the theme directory. Once you have done that, modify the following: <?php next_posts_link(); ?> becomes… <?php next_post_link(‘%link’,’%title’,TRUE) ?> and <?php previous_posts_link(); ?> becomes… <?php previous_post_link(‘%link’, ‘%title’, TRUE); ?> The third argument … Read more

How to Override default update_count_callback for category

In your update_count_callback function do a check for $post->post_status and don’t increment your count if post_status is not private. See this excellent answer on writing a custom update_count_callback callback function. Edit: Misread the question. To override the existing default for the category taxonomy you can create a function that overrides the global $wp_taxonomies variable function … Read more

Related content based on category name

You have a couple of flaws here and also a few places where you can optimize the code Instead of using get_the_category(), use wp_get_post_terms(). It is a bit faster, and you have the option to just get the term ID’s from the post categories. This is one place where you can optimize your code ID … Read more

So how to redirect category to a page of the same name if it exists?

get_page_by_path does something similar: function pagefromcat_template_redirect() { if ( ! is_category() ) { return; } $category = get_queried_object(); $page = get_page_by_path( $category->slug ); if ( $page instanceof WP_Post ) { $url = get_permalink( $page ); wp_safe_redirect( $url, 301 ); } } add_action( ‘template_redirect’, ‘pagefromcat_template_redirect’ );

is_category in pre_get_posts results in php notices

is_category(‘category-name’) won’t work until after the query is run. If we check source code where those notices are generated, we can see that it’s using get_queried_object, which gets populated with the results of the main query, and at the pre_get_posts stage will still be empty. As an alternate, check the contents of $query->query_vars, which will … Read more