• Resolved creativemanner

    (@creativemanner)


    Hey there, I am really happy with the plugin. I have a quick question regarding the title of the page based on results.

    I have 2 taxonomies, tax1 : tax2
    tax1 has two terms tax1tem1, tax1term2
    tax2 has 8 terms and so on.

    I would like to show specific title on result pages. this is what I ve tried but ;

    if( is_tax('tax1') && !is_tax('tax2') ) {
      $tax1term = get_term_by( 'slug', get_query_var( 'term' ), 'tax1'  ); 
    echo $tax1term->name; 
    echo $tax1term->description;
    }elseif(is_tax('tax1') && is_tax('tax2')){
     $tax1term = get_term_by( 'slug', get_query_var( 'term' ), 'tax1'  ); 
      $tax2term = get_term_by( 'slug', get_query_var( 'term' ), 'tax2'  ); 
    echo $tax2term->name; 
    echo $tax2term->description;
    }

    so basically I land on the post archive page with the filter on.
    If I select no tax1 and select tax2 = show tax2 as title
    If i select tax1 and no tax2 = show tax1 as title
    If I select tax1 and tax2 at the same time = show tax2 as title and tax1 as subtitle

    Is there a clean way of doing this?

Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author Jonathandejong

    (@jonathandejong)

    Hi,

    Thank you! If you like BTF I’d love a review ??

    That depends on the definition of “clean” ;). Here’s how I’d probably do it. This code could be simpler but it seems you want tax2 to take priority if it exists so we need a bit extra to make sure it’s always fetched first.

    
    <?php
    global $wp_query;
    $terms = array();
    if ( isset( $wp_query->query['tax2'] ) ) {
    	$terms[] = array(
    		'tax' => 'tax2',
    		'term' => $wp_query->query['tax2']
    	);
    }
    if ( isset( $wp_query->query['tax1'] ) ) {
    	$terms[] = array(
    		'tax' => 'tax1',
    		'term' => $wp_query->query['tax1']
    	);
    }
    ?>
    <?php if ( ! empty( $terms ) ) : ?>
    	<header class="archive-header">
    		<?php 
    		for ( $i = 0; $i <= count( $terms ); $i++ ) :
    			$term_object = get_term_by( 'slug', $terms[ $i ]['term'], $terms[ $i ]['tax'] );
    			if ( $i == 0 ) {
    				echo sprintf( '<h1 class="archive-header-title">%s</h1><p class="archive-header-description">%s</p>', $term_object->name, $term_object->description );
    			} else {
    				echo sprintf( '<h2 class="archive-header-subtitle">%s</h2><p class="archive-header-subdescription">%s</p>', $term_object->name, $term_object->description );
    			}
    		endfor;
    		?>
    	</header>
    <?php endif; ?>
    

    EDIT: I should note that this is all written blind. I have not tested the code at all ??

    Thread Starter creativemanner

    (@creativemanner)

    let me give it a try, thanks man.

    Thread Starter creativemanner

    (@creativemanner)

    ok,

    echo sprintf( '<h2 class="archive-header-subtitle">%s</h2><p class="archive-header-subdescription">%s</p>',$term_object->name, $term_object->description );

    giving error: NOTICE: TRYING TO GET PROPERTY OF NON-OBJECT IN …

    and

    $term_object = get_term_by( 'slug', $terms[ $i ]['term'], $terms[ $i ]['tax'] );

    giving error: NOTICE: UNDEFINED OFFSET: 2 IN …

    Plugin Author Jonathandejong

    (@jonathandejong)

    otherwise it works?
    You should remove the = in the for loop.
    So:
    for ( $i = 0; $i < count( $terms ); $i++ ) :

    Thread Starter creativemanner

    (@creativemanner)

    note that my taxs are, resource-topic and resource-type, not tax1 tax2 (was trying to give an example)

    Plugin Author Jonathandejong

    (@jonathandejong)

    Haha yeah I get that.. you’ll have to change them yourself in the snippet ??

    Thread Starter creativemanner

    (@creativemanner)

    no I did that; here is the full snippet i am using

    <?php 
    
    global $wp_query;
    $terms = array();
    if ( isset( $wp_query->query['resource-topic'] ) ) {
      $terms[] = array(
        'tax' => 'resource-topic',
        'term' => $wp_query->query['resource-topic']
      );
    }
    if (isset( $wp_query->query['resource-type'] ) ) {
      $terms[] = array(
        'tax' => 'resource-type',
        'term' => $wp_query->query['resource-type']
      );
    }else{
    
      
    }
    ?>
    <?php if ( ! empty( $terms ) ) { ?>
      <header class="archive-header">
        <?php 
        for ( $i = 0; $i <= count( $terms ); $i++ ) :
          $term_object = get_term_by( 'slug', $terms[ $i ]['term'], $terms[ $i ]['tax'] );
          if ( $i == 0 ) {
     echo sprintf( '<h1 class="page-title lined">%s</h1><div class="taxonomy-description">%s</div>', $term_object->name, $term_object->description );
          } else {
     echo sprintf( '<h4 class="archive-header-subtitle">%s</h4><p class="archive-header-subdescription">%s</p>',$term_object->name, $term_object->description );
          }
        endfor;
        ?>
      </header>
    <?php } ?>

    I am running this on archive-resource.php

    Thread Starter creativemanner

    (@creativemanner)

    here are the screens with the errors. it works but errors, funny ??

    https://take.ms/RwLDH
    https://take.ms/M7dDH
    https://take.ms/cUkLO

    Plugin Author Jonathandejong

    (@jonathandejong)

    Yeah but you have not changed <= to just < in the for loop (as I described above).
    The issue is that the loop runs 3 times with <= when there’s only 2 terms. So change it the way I described and you should be good to go!

    Thread Starter creativemanner

    (@creativemanner)

    Thanks Jonathan, that worked on the tax pages fine.

    Would it be too much to ask how can I set these header as following;

    Like I said, I am using a custom post archive file called resource-archive.php

    I would like to show;

    if it is a resource post archive page …com/resources
    Title is Resources
    if this is a tax1 (resource-topic) page …com/resources/resource-topic/termname
    Title is That Termname
    if this is a tax2 (resource-type) page …com/resources/resource-type/termname
    Title is That Termname
    if this is a tax1 (resource-topic) page and also tax2(resource-type) …com/resources/resource-topic/termname/resource-type/termname (which is what we figured)

    Is there a logic you can share ith me to resolve my week long frustration? ??

    Thanks for all your help again.

    Plugin Author Jonathandejong

    (@jonathandejong)

    Heyo,

    If I understand you correctly it should be as easy as this:

    
    <?php 
    
    global $wp_query;
    $terms = array();
    if ( isset( $wp_query->query['resource-topic'] ) ) {
      $terms[] = array(
        'tax' => 'resource-topic',
        'term' => $wp_query->query['resource-topic']
      );
    }
    if (isset( $wp_query->query['resource-type'] ) ) {
      $terms[] = array(
        'tax' => 'resource-type',
        'term' => $wp_query->query['resource-type']
      );
    }
    ?>
    <header class="archive-header">
    <?php
    if ( ! empty( $terms ) ) :
        for ( $i = 0; $i < count( $terms ); $i++ ) :
          $term_object = get_term_by( 'slug', $terms[ $i ]['term'], $terms[ $i ]['tax'] );
          if ( $i == 0 ) {
     echo sprintf( '<h1 class="page-title lined">%s</h1><div class="taxonomy-description">%s</div>', $term_object->name, $term_object->description );
          } else {
     echo sprintf( '<h4 class="archive-header-subtitle">%s</h4><p class="archive-header-subdescription">%s</p>',$term_object->name, $term_object->description );
          }
        endfor;
    else :
    echo sprintf( '<h1 class="page-title lined">%s</h1>', post_type_archive_title('', false) );
    endif;
    ?>
    </header>
    
    
Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Changing result’s title based on filter selection ?’ is closed to new replies.