Exclude category from foreach loop

I believe you are using the following code from this pastebin link function posts_by_year() { // array to use for results $years = array(); // get posts from WP $posts = get_posts(array( ‘numberposts’ => -1, ‘orderby’ => ‘post_date’, ‘order’ => ‘ASC’, ‘post_type’ => ‘my-custom-post-type’, ‘post_status’ => ‘publish’ )); // loop through posts, populating $years arrays … Read more

How to exclude posts from category posts count

Assuming you have a list containing the post IDs you want to exclude, you could start with something like (PHP 7.3): function exclude_posts_from_category( array $cat_args ): array { $exclude = array(); // TODO Add post IDs to exclude. $cat_args[‘exclude’] = $exclude; return $cat_args; } add_filter( ‘widget_categories_args’, ‘exclude_posts_from_category’, 10, 1 ); You can read about the … Read more

WordPress showing wrong category name

As far a I can understand you need to display the category name for the current page when viewing a category page. You can make use of the query variables to retrieve the category name, for example: $category = get_queried_object(‘cat’);  echo $category->name; EDIT A global check to see what is returned by the main query … Read more

Showing HTML if Post is In Certain Taxonomy Term

Try using has_term(). Something like this: <?php if( has_term(‘term1’, ‘taxonomy_name’, $post) ) { ?> <img src=”http://www.site.com/wp-content/themes/site/img/stars-01.png” width=”100″ height=”21″ alt=”default”> <?php }elseif ( has_term( ‘term2’, ‘taxonomy_name’, $post) ) { ?> <img src=”http://www.site.com/wp-content/themes/site/img/stars-01.png” width=”100″ height=”21″ alt=”default”> <?php } else { ?> <img src=”http://www.site.com/wp-content/themes/site/img/stars-01.png” width=”100″ height=”21″ alt=”default”> <?php } ?> You may also want to try using shorthand … Read more

How to get nav to show current_page_parent class when on regular post (not blog), differentiated by category

I found the correct syntax is: if ( is_single() && in_category ( ‘5’ ) ) { However, I solved the problem using this code: //add category classes to single.php add_filter(‘body_class’,’add_category_to_single’); function add_category_to_single($classes, $class) { if (is_single() ) { global $post; foreach((get_the_category($post->ID)) as $category) { // add category slug to the $classes array $classes[] = $category->category_nicename; … Read more

A dropdown with checklist

wp_terms_checklist() is an admin side function as you suspect. I don’t recommend trying to load admin side code on the front end (especially if you are not very very familiar with its inner workings) so essentially you are left with rolling your own code to create the checklist. You should be able to use the … Read more

Add Date & Author Meta to Category Archive Page

This is how its done in Twenty Fourteen which uses a template tag: Note: The conditionals if ( is_sticky() && is_home() && ! is_paged() ) { will need to be changed or removed if ( ! function_exists( ‘twentyfourteen_posted_on’ ) ) : /** * Print HTML with meta information for the current post-date/time and author. * … Read more

Link from navbar to categories

To show all categories on a page, you will have to create a new page template. <?php /** * Template Name: Categories Page **/ get_header(); ?> <!– content –> <div id=”content”> <ul class=”category-list”> <?php wp_list_categories( ‘title_li=’ ); ?> </ul> </div> <!– / content –> <?php get_footer(); Now create a new page for categories in WordPress … Read more