Custom Taxonomy Template (Help Me Understand the Array)
-
I have a custom post type (totd) and a custom taxonomy (totd_cat) that has a custom rewrite (small-business-tips/category) with four terms (social, marketing, technology, operations). On the CPT template, the single CPT template, and the taxonomy templates, there is a menu to choose the terms of the taxonomy in order those posts. For the taxonomy template, I have taxonomy-totd_cat.php which has in it the code below, which current produces https://m.angiemeekerdesigns.com/small-business-tips/category/marketing/ (for example, that is one of the terms in the taxonomy). It is drawing from all of the posts in the post type, rather than just the term of the taxonomy chosen. I don’t understand how to get the template to show only the posts of the taxonomy term chosen. Also, I know the array is wrong, but I don’t know how to fix it. My custom post type template works perfectly using practically the same code, but without the is_tax array.
/** Replace the standard loop with our custom loop */ remove_action( 'genesis_loop', 'genesis_do_loop' ); add_action( 'genesis_loop', 'totd_do_cat_loop'); add_action( 'genesis_loop', 'totd_teasers_do_cat_loop'); add_action('pre_get_posts', 'myprefix_query_offset', 1 ); function myprefix_query_offset(&$query) { //First, define your desired offset... $offset = $query->get('offset'); //Next, determine how many posts per page you want (we'll use WordPress's settings) $ppp = 10; //Next, detect and handle pagination... if ( $query->is_paged ) { //Manually determine page query offset (offset + current page (minus one) x posts per page) $page_offset = $offset + ( ($query->query_vars['paged']-1) * $ppp ); //Apply adjust page offset $query->set('offset', $page_offset ); } else { //This is the first page. Just use the offset... $query->set('offset',$offset); } } add_filter('found_posts', 'myprefix_adjust_offset_pagination', 1, 2 ); function myprefix_adjust_offset_pagination($found_posts, $query) { //Define our offset again... $offset = $query->get('offset'); //Reduce WordPress's found_posts count by the offset... return $found_posts - $offset; } add_action('genesis_loop', 'totd_do_cat_loop'); function totd_do_cat_loop() { if ( !is_paged() ) { global $wp_query; // Set up your query here $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; $query = array( 'post_type' => 'tipoftheday', 'orderby' => 'date', 'posts_per_page' => '1', 'tax_query' => array( array( 'taxonomy' => 'totd_cat', 'field' => 'slug', 'terms' => array ('social', 'marketing', 'operations', 'technology') )) ); $wp_query = new WP_Query( $query ); if( $wp_query->have_posts() ): while( $wp_query->have_posts() ): $wp_query->the_post(); // Get the custom fields // Store the pre tips data $tips_data_pre = array( 'totd_tags' => get_field( 'totd_tags' ), 'tip_article_headline' => get_field( 'tip_article_headline' ), 'article_author' => get_field( 'article_author' ), 'article_author_link' => get_field( 'article_author_link' ), ); // Only output if we have tips data if ($tips_data_pre['totd_tags'] != '' || $tips_data_pre['tip_article_headline'] != '' || $tips_data_pre['article_author'] != '' || $tips_data_pre['article_author_link'] != '') { echo '<span class="date time published" title="%1$s">' , do_shortcode('[post_date]'),'</span>' ; echo '<div class="tip-excerpt"><p><div class="entry-content">'; echo '<div class=h1 entry-title"><a href="'.get_permalink().'"title="'.get_the_title().'">' . get_the_title() . '</a></div>'; echo the_excerpt(); echo '<div class="entry-terms">' , do_shortcode('[post_terms taxonomy="totd_tags" before="See More Tips For: " taxonomy="totd_tags"] '),'</div>' ; echo '<div class="entry-terms"> <div class="share">Share This Tip:</div> <div class="addthis_toolbox addthis_default_style"> <a class="addthis_button_preferred_1"></a> <a class="addthis_button_preferred_2"></a> <a class="addthis_button_preferred_3"></a> <a class="addthis_button_preferred_4"></a> <a class="addthis_button_compact"></a> <a class="addthis_counter addthis_bubble_style"></a> </div> <script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=manta"></script> </div> </div></div>'; echo '</p>'; } endwhile; endif; // Reset the query wp_reset_query(); } } add_action('genesis_loop', 'totd_teasers_do_cat_loop'); function totd_teasers_do_cat_loop() { global $wp_query; // Set up your query here $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 0; $query = array( 'post_type' => 'tipoftheday', 'orderby' => 'date', 'posts_per_page' => '10', 'offset' => '1', 'paged' => $paged, ); $wp_query = new WP_Query( $query ); if( $wp_query->have_posts() ): while( $wp_query->have_posts() ): $wp_query->the_post(); // Get the custom fields // Store the pre tips data $tips_data_pre = array( 'totd_tags' => get_field( 'totd_tags' ), 'tip_article_headline' => get_field( 'tip_article_headline' ), 'article_author' => get_field( 'article_author' ), 'article_author_link' => get_field( 'article_author_link' ), ); // Only output if we have tips data if ($tips_data_pre['totd_tags'] != '' || $tips_data_pre['tip_article_headline'] != '' || $tips_data_pre['article_author'] != '' || $tips_data_pre['article_author_link'] != '') { echo '<div class="time-teaser"> <span class="day">',get_the_time( 'l' ),'</span> <span class="month">',get_the_time( 'm/d/Y' ),'</span> </div>'; echo '<div class="tip-excerpt"><p><div class="entry-content">'; echo '<div class=h2 entry-title"><a href="'.get_permalink().'"title="'.get_the_title().'">' . get_the_title() . '</a></div>'; $excerpt = get_the_excerpt(); echo string_limit_words($excerpt,25); echo '<a class="moretag" href="'. get_permalink($post->ID) . '"> Continue Reading→</a>'; echo '<div class="entry-terms">' , do_shortcode('[post_terms taxonomy="totd_tags" before="See More Tips For: " taxonomy="totd_tags"] '),'</div>' ; echo '<div class="entry-terms"></div> </div></div>'; echo '</p>'; } endwhile; // Pagination! Woo! genesis_posts_nav(); endif; // Reset the query wp_reset_query(); } genesis();
The part above that I think is most specific to what I’m looking at is
if ( !is_paged() ) { global $wp_query; // Set up your query here $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; $query = array( 'post_type' => 'tipoftheday', 'orderby' => 'date', 'posts_per_page' => '1', 'tax_query' => array( array( 'taxonomy' => 'totd_cat', 'field' => 'slug', 'terms' => array ('social', 'marketing', 'operations', 'technology') )) );
- The topic ‘Custom Taxonomy Template (Help Me Understand the Array)’ is closed to new replies.