• Resolved akwinter84

    (@akwinter84)


    I’m using the Custom Post Type UI plugin and created a taxonomy for the Events post type already built in the Lindeza Pro theme since I want to be able to categorize each Event under the Class taxonomy. I created a taxonomy-class.php to hopefully show the posts of ONLY that category/taxonomy but it shows ALL of the Events (aka Classes) posts. I believe it’s because of the theme’s custom loop used to pull from Events:

    <?php
    $lindeza_get_list_events = lindeza_get_list_events(-1);
    while ( $lindeza_get_list_events->have_posts() ) {
    $lindeza_get_list_events->the_post();
    ?>

    Could anyone direct me on what I need to edit below to get it working? Or a better way to give Events categories other than the method I did? Below is the full code of taxonomy-class.php:

    <?php
    /**
     * The template for displaying all classes.
     *
     Template Name: Classes
     * @package Lindeza
     */
     get_header(); ?>
      	  <header>
    		<div class="page-title">
    		   <div class="wrapper">
    			    <?php if(function_exists('bcn_display')) { ?>
    				<div class="breadcrumbs">
    						<?php	bcn_display(); ?>
    				</div>
    				<?php } ?>
    			   <h2><?php printf( __( 'Classes: %s', 'lindeza' ), '<span>' . single_cat_title( '', false ) . '</span>' ); ?></h2>
    			</div>
    	    </div>
    	 </header>
        <div class="mainContainer events-page">
            <div class="content">
                <div class="wrapper">
                    <div class="content-events">
                        <div class="events-posts">
    						<?php
    							$lindeza_get_list_events = lindeza_get_list_events(-1);
    							while ( $lindeza_get_list_events->have_posts() ) {
    							$lindeza_get_list_events->the_post();
    							?>
    							<?php get_template_part( 'partials/content', 'events' ); ?>
    						<?php }  wp_reset_query(); ?>
                            <div class="pagination">
    			               <?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>
                            </div>
                        </div>
                        <?php  get_sidebar(); ?>
                    </div>
                </div>
            </div>
        </div>
    <?php get_footer(); ?>

    It can be seen in action at https://rebeccalowe.physio/rebecca-lowe/classes/ and in the right sidebar are the class categories (which changes to the default blog sidebar after click through – but I need it to stay as the custom Classes sidebar).

Viewing 9 replies - 1 through 9 (of 9 total)
  • Moderator bcworkz

    (@bcworkz)

    I don’t know what lindeza_get_list_events() does and if there’s any way to alter what it does. This is part of why we cannot be of much help with commercial themes. It appears you could replace it with new WP_Query( $args );. The crux is in defining $args – more at
    Class Reference/WP Query.

    As far as loading the right sidebar, you could modify the sidebar template to conditionally load one set of widgets or another (or output any arbitrary content), dependent on something in the main query which is contained in the global $wp_query, or something in the referrer link.

    Thread Starter akwinter84

    (@akwinter84)

    Thanks for your response! I’m investigating how new WP-Query( $args ); works right now as I am not familiar with it. Meanwhile, this was what was put in functions.php by the theme creator to create the List Events:

    // Create List Events
    
    if ( ! function_exists( 'lindeza_get_list_events' ) ) :
    	function lindeza_get_list_events($n) {
    
    		global $wp_query;
    
    		$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
    		$args = array(
    			'post_type' => 'events',
    			'orderby' => 'menu_order',
    			'order' => 'ASC',
    			'posts_per_page' => $n
    		);
    
    		$wp_query->query( $args );
    
    		return new WP_Query( $args );
    	}
    endif;

    If I use:
    <?php while (have_posts()) : the_post(); ?> in taxonomy-class.php it displays nothing. If I use

    <?php
    	$lindeza_get_list_events = lindeza_get_list_events(-1);
    	while ( $lindeza_get_list_events->have_posts() ) {
    	$lindeza_get_list_events->the_post();
    ?>

    It displays all the events… I need the happy medium of it displaying only the selected taxonomy term! ??

    Moderator bcworkz

    (@bcworkz)

    The lindeza_get_list_events() function is “pluggable”, meaning you can override it by re-declaring it after the original has been loaded. To do so, create a child theme. You will want to do this no matter what. It protects your changes when the theme is updated.

    Copy the entire function to your child theme functions.php, including the if ( ! function_exists ... endif;

    Add this to the $args array declaration:

    'tax_query' => array(
       array(
          'taxonomy' => 'class',
          'field'    => 'slug',
          'terms'    => $wp_query->get('class'),
       ),
    ),

    Be sure each array element ends with a comma:
    'posts_per_page' => $n,
    In array declarations for PHP, it’s OK to have “dangling” commas:
    $a = array( 1, 2, 3,);
    I recommend always doing so, it prevents a hard to find syntax error when the code is edited some time later.

    This code could break if the query is not a ‘class’ taxonomy query, you may need to conditionally add the tax_query to $args only if the ‘class’ query var is set. Or it may fail gracefully, I’m not sure, none of this has been tested.

    Thread Starter akwinter84

    (@akwinter84)

    Almost there!

    I gave this a shot. Classes page shows up empty; however, it does the job of showing posts only from each class category after I click on a class term in the right sidebar, but how can I have it still show ALL classes on the Classes Page?

    So appreciate the help!

    Moderator bcworkz

    (@bcworkz)

    You’ll need to identify something unique about a generic classes query that is different than a specific class term query. I’m referring to the main query contained in the global $wp_query. Use that characteristic to conditionally add a different ‘tax_query’ argument. To query for all posts with any ‘class’ term try this:

    'tax_query' => array(
       array(
          'taxonomy' => 'class',
          'operator' => 'EXISTS',
       ),
    ),

    The EXISTS operator is not very well documented, and I’ve never used it before, but I think this is how it works.

    Thread Starter akwinter84

    (@akwinter84)

    It takes us back to where we started when it displays ALL classes. :-/

    Below are the $args we tried. I just want to make sure I put them in correctly.

    // Create List Events
    
    if ( ! function_exists( 'lindeza_get_list_events' ) ) :
    	function lindeza_get_list_events($n) {
    
    		global $wp_query;
    
    		$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
    		$args = array(
    			'post_type' => 'events',
    			'orderby' => 'menu_order',
    			'order' => 'ASC',
    			'posts_per_page' => $n,
    			'tax_query' => array(
    			   array(
    				  'taxonomy' => 'class',
    				  'field'    => 'slug',
    				  'terms'    => $wp_query->get('class'),
    			   ),
    			),
    		);
    
    		$wp_query->query( $args );
    
    		return new WP_Query( $args );
    	}
    endif;

    ^ Displays the class terms on their own, but page for ALL classes disappear.

    // Create List Events
    
    if ( ! function_exists( 'lindeza_get_list_events' ) ) :
    	function lindeza_get_list_events($n) {
    
    		global $wp_query;
    
    		$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
    		$args = array(
    			'post_type' => 'events',
    			'orderby' => 'menu_order',
    			'order' => 'ASC',
    			'posts_per_page' => $n,
    			'tax_query' => array(
    			   array(
    				  'taxonomy' => 'class',
    				  'operator' => 'EXISTS',
    			   ),
    			),
    		);
    
    		$wp_query->query( $args );
    
    		return new WP_Query( $args );
    	}
    endif;

    ^ Displays ALL classes, even in the class terms pages.

    Moderator bcworkz

    (@bcworkz)

    Right. There is no one single set of tax_query args that will work for all possible ‘class’ taxonomy queries. You need one set for when a term is supplied and the other when not.

    You need some sort of conditional if/else logic to determine which set to use based on what query vars are set. var_dump the global $wp_query in each case to see what’s possible to check for.

    Thread Starter akwinter84

    (@akwinter84)

    Got it working!

    I just created another function just like List Events for the Class terms but changed the ‘$args’ array declaration.

    I don’t know if this is anything like what you suggested as I couldn’t comprehend it but it directed me to the following that worked for me:

    // Create List Events
    
    if ( ! function_exists( 'lindeza_get_list_events' ) ) :
    	function lindeza_get_list_events($n) {
    
    		global $wp_query;
    
    		$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
    		$args = array(
    			'post_type' => 'events',
    			'orderby' => 'menu_order',
    			'order' => 'ASC',
    			'posts_per_page' => $n,
    			'tax_query' => array(
    			   array(
          				'taxonomy' => 'class',
          				'operator' => 'EXISTS',
    			   ),
    			),
    		);
    
    		$wp_query->query( $args );
    
    		return new WP_Query( $args );
    	}
    endif;
    
    /**
     * Below used in taxonomy-class.php
    */
    
    // Create List Classes
    
    if ( ! function_exists( 'lindeza_get_list_class' ) ) :
    	function lindeza_get_list_class($n) {
    
    		global $wp_query;
    
    		$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
    		$args = array(
    			'post_type' => 'events',
    			'orderby' => 'menu_order',
    			'order' => 'ASC',
    			'posts_per_page' => $n,
    			'tax_query' => array(
    			   array(
    				  'taxonomy' => 'class',
    				  'field'    => 'slug',
    				  'terms'    => $wp_query->get('class'),
    			   ),
    			),
    		);
    
    		$wp_query->query( $args );
    
    		return new WP_Query( $args );
    	}
    endif;

    Thanks for your help and direction, bcworkz!

    Moderator bcworkz

    (@bcworkz)

    Your welcome!
    What you have is not exactly what I envisioned, but it uses the same logic. Either way, it works! Congratulations, well done.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Adding Categories to Custom Post Type’ is closed to new replies.