On archive.php show loop for child categories if they exist or posts if they don’t, on custom post type

This works, anything better would be appreciated $term = get_queried_object(); $term_id = $term->term_id; $taxonomy_name = $term->taxonomy; $termchildren = get_term_children( $term_id, $taxonomy_name ); if (!empty($termchildren)) { echo ‘<div class=”subcatwrapper”>’; echo ‘<ul class=”workshopgrid centerWidth”>’; foreach ( $termchildren as $child ) { $term = get_term_by( ‘id’, $child, $taxonomy_name ); echo ‘<li class=”workshopbox”>’; echo ‘<div class=”workshopimage”><a href=”‘. get_term_link( $term->slug, … Read more

Show custom post type on post category page doesn’t work / breaks navigation

The reason the menu is disappearing is because pre_get_posts applies to all queries on the page, including menus, your code is interferring with the menu queries. The is_main_query method should make sure it only affect the query in the loop, which in most cases will be the query displaying the posts. function test_add_cpt_to_archive_page( $query ) … Read more

Custom Post Types using wrong template (index) instead of archive-{type}.php – previously worked as expected

The issue was, in fact, the new taxonomy using “year” clashes with the query. WordPress was accepting year=about-us in the query chain for instance, as year was a taxonomy, before the expected page=about-us – preventing it from appearing as a page and using the index.php template as there is no “year” template. I changed year … Read more

Add a class to a menu item depending on a body class

Use the nav_menu_css_class() filter. The following function will add the current-menu-item class to a page identified by it’s slug. You can Update the $cpt_name and $menu_item_id variables to reflect your setup. add_filter( ‘nav_menu_css_class’, ‘nav_parent_class’, 10, 2 ); function nav_parent_class( $classes, $item ) { $cpt_name=”team”; $menu_item_id = 127; // id of menu item to add current-menu-item … Read more

WordPress Rewrite Url with arugments

Instead of using $_SESSION variables we’ll register a query variable. function wpa_91718_query_var( $vars ) { $vars[] = ‘event_page’; return $vars; } add_filter( ‘query_vars’, ‘wpa_91718_query_var’ ); Then we can modify the query before it is run using pre_get_posts. If I understood you and your code correctly, then if the query var is present in the URL, … Read more