Get all subcategories and related posts

Try using these codes: if( isset( $sub_category ) ){ echo ‘<b>more items in: </b>’ . $sub_category->name; $args = array( ‘cat’ => $sub_category->term_id, ‘post__not_in’ => array( get_the_ID() ) ); $relatedpostsinsubcategory = new WP_Query( $args ); if( $relatedpostsinsubcategory->have_posts() ){ while( $relatedpostsinsubcategory->have_posts() ){ $relatedpostsinsubcategory->the_post(); ?> <a href=”https://wordpress.stackexchange.com/questions/324478/<?php the_permalink(); ?>” title=”<?php the_title_attribute(); ?>”><?php the_title(); ?></a> <?php } wp_reset_postdata(); } … Read more

How to loop only categories without posts (+ show category featured image with acf) [closed]

Well, in WP the word “loop” is mainly related to posts. It’s easier to say that you want to display categories. All you have to do is to get them using get_categories function and display them. <?php $categories = get_categories( array( ‘orderby’ => ‘name’, ‘order’ => ‘ASC’ ) ); if ( ! empty( $categories ) … Read more

.htaccess too many redirects based on category slug

It’s not clear from what you have posted why you are getting a redirect loop. We would need to see the rest of .htaccess file. However …. You may be getting a conflict with existing mod_rewrite (ie. RewriteRule) directives. Redirect is a mod_alias directive. You no doubt have existing mod_rewrite directives with the WordPress front-controller. … Read more

Warning: sizeof (): Parameter must be an array or an object that implements Countable, On products pages

Let’s take a look at get_the_terms Codex page. We can find there that this function may return: (array|false|WP_Error) Array of WP_Term objects on success, false if there are no terms or the post does not exist, WP_Error on failure. So your code will work correct only in one case – when the function returns an … Read more

List subcategories of a specific product category (adapting from posts to products taxonomy)

…to show the subcategories of a specific category (ID 89) … Try this $product_cats = wp_get_post_terms( $post->ID, ‘product_cat’ ); if ( ! empty( $product_cats ) && ! is_wp_error( $product_cats ) ) { ?> <ul> <?php foreach ($product_cats as $childcat) : ?> <?php if ($childcat->parent == 89)) { ?> <li> <a href=”https://wordpress.stackexchange.com/questions/331894/<?php echo get_term_link($childcat->term_id); ?>”><?php echo … Read more

Should the actual /category/ directory be 404? Is that normal WP behaviour

Yes, it’s normal behavior. WordPress uses a set of rewrite rules to process requests. Here are the rules that will match requests related to categories: [category/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?category_name=$matches[1]&feed=$matches[2] [category/(.+?)/(feed|rdf|rss|rss2|atom)/?$] => index.php?category_name=$matches[1]&feed=$matches[2] [category/(.+?)/page/?([0-9]{1,})/?$] => index.php?category_name=$matches[1]&paged=$matches[2] [category/(.+?)/?$] => index.php?category_name=$matches[1] As you can see, all of them require, that the request is longer than /category/. And it makes … Read more