Different CSS class by category

If your template uses the function post_class() in it then your posts already have category-based classes applied to them in the HTML. In PHP, it often will look like this: <article id=”post-<?php the_ID(); ?>” <?php post_class(); ?>> For example, say you have a category of news, then posts that are in it will have a … Read more

Dropdown menu for subcategories

You don’t need to create category listing functionality, WordPress already created a helper function to handle this easily. Take a look at wp_list_categories(). And what you need can be achieved through some CSS and a little bit of JS if needed. You’ll find lots of jQuery menu plugin you can use any of those or … Read more

Display list categories from CTP

On a taxonomy archive it’s fairly straightforward, as you can get the current taxonomy with: $term_object = get_queried_object(); $taxonomy = $term_object->taxonomy; On a post type archive it’s a little bit more complicated as you first need to identify the current post type, and then retrieve the taxonomy/taxonomies attached to that post type: $post_type_object = get_queried_object(); … Read more

Listing posts under a category by date

Even though it is not the most conventional way of doing, I would proceed like this: Edit : precision I create a new file category.php which is the template for categories, and put this piece of code inside. $args = array( ‘post_type’=>’post’, ‘posts_per_page’=> -1, ‘post_status’=>’publish’, ‘orderby’=>’post_date’, ‘order’=>’DESC’ ); $query = new WP_Query($args); if($query->have_posts()) { $years … Read more

Show different title on category page

You’ll need to change this in, most likely, your header.php file in your themes root. I would suggest that you use something like YoastSEO Plugin which will give you granular manipulation of your sites <title> and <meta> tags. If this is not an option, you would have to dive into the header.php template and update … Read more

Custom template for sub-sub-categories

Instead of include(), use get_template_part() https://developer.wordpress.org/reference/functions/get_template_part/ <?php $post = $wp_query->post; if (cat_is_ancestor_of( 42, $cat ) ) { get_template_part( ‘services’, ‘template’ ) } else { get_template_part( ‘single’, ‘default’ ); } ?> If you create those files and use get_template_part(), then within those files you can set them up to display posts however you see fit. Take … Read more

Post by category+author not working!

why this code is ignoring the author id It’s not ignoring the author ID. But with that code, you need to call setup_postdata(), like this: //global $post; // Uncomment if necessary. $current_user = wp_get_current_user(); //$current_user->ID; $post_ref = get_posts( array( ‘author’ => $current_user->ID, ‘numberposts’ => 1, ‘cat’ => 31 ) ); foreach( $post_ref as $post ): … Read more