Change Title Based on Taxonomy Filter in archive-{cpt}.php

admin2025-06-06  7

So I have page in Custom Post Type RENTAL with following custom taxonomies:

  • Merk -- with terms: Honda, Daihatsu, Toyota, etc
  • Kendaraan -- with parent terms: Mobil, Bus, Motor; & child terms: MPV, hatchback, sedan, etc.
  • Seat -- with terms: 2 seat, 4 seat, 8 seat, etc

What I Wanted to Achieve

I want my title H1 in my archive page with taxonomy filter to change based on taxonomy used to filter, example:

/?kendaraan=mobil&merk=honda&seat=2seat

TITLE: Rental Mobil Honda (2 Seat)

/?kendaraan=sedan&merk=honda&seat=4seat

TITLE: Rental Mobil Sedan Honda (4 Seat) (NOTE: "Sedan" being child of "Mobil")

/?kendaraan=sedan&merk=honda

TITLE: Rental Mobil Sedan Honda (NOTE: "Sedan" being child of "Mobil")

And I also want it to have different title when it has no taxonomy filtered.

/

TITLE: Rental Mobil, Bus, Motor

Unfortunately my code can achieve 'some' of what I wanted. But the title always pick taxonomy even if it isn't filtered.

$merk = wp_strip_all_tags( get_the_term_list( get_the_ID(), 'merk', 'Merk ', '', ' ') );
$kendaraan = wp_strip_all_tags( get_the_term_list( get_the_ID(), 'kendaraan', '', '', ' ') );
$kendaraanParent = get_term_parents_list( get_the_ID(), 'kendaraan' );
$seat = wp_strip_all_tags( get_the_term_list( get_the_ID(), 'seat_mobil', '(', '', ')' ) );

    <header class="archive-header">
<h1 class="archive-title">
Rental <?php echo $kendaraan ; ?><?php echo $merk ; ?><?php echo $kendaraanParent->name ; ?><?php echo $seat ; ?> - TravelBos.id
</h1>
</header><!-- .archive-header -->

Code above works great for when I filter all three taxonomies.

/?kendaraan=sedan&merk=honda&seat=4seat

But when filtering two, one, or none taxonomy(ies) it turned stupid.

Any ideas? Thank you!

So I have page in Custom Post Type RENTAL with following custom taxonomies:

  • Merk -- with terms: Honda, Daihatsu, Toyota, etc
  • Kendaraan -- with parent terms: Mobil, Bus, Motor; & child terms: MPV, hatchback, sedan, etc.
  • Seat -- with terms: 2 seat, 4 seat, 8 seat, etc

What I Wanted to Achieve

I want my title H1 in my archive page with taxonomy filter to change based on taxonomy used to filter, example:

https://example/rental/?kendaraan=mobil&merk=honda&seat=2seat

TITLE: Rental Mobil Honda (2 Seat)

https://example/rental/?kendaraan=sedan&merk=honda&seat=4seat

TITLE: Rental Mobil Sedan Honda (4 Seat) (NOTE: "Sedan" being child of "Mobil")

https://example/rental/?kendaraan=sedan&merk=honda

TITLE: Rental Mobil Sedan Honda (NOTE: "Sedan" being child of "Mobil")

And I also want it to have different title when it has no taxonomy filtered.

https://example/rental/

TITLE: Rental Mobil, Bus, Motor

Unfortunately my code can achieve 'some' of what I wanted. But the title always pick taxonomy even if it isn't filtered.

$merk = wp_strip_all_tags( get_the_term_list( get_the_ID(), 'merk', 'Merk&nbsp;', '', '&nbsp;') );
$kendaraan = wp_strip_all_tags( get_the_term_list( get_the_ID(), 'kendaraan', '', '', '&nbsp;') );
$kendaraanParent = get_term_parents_list( get_the_ID(), 'kendaraan' );
$seat = wp_strip_all_tags( get_the_term_list( get_the_ID(), 'seat_mobil', '(', '', ')' ) );

    <header class="archive-header">
<h1 class="archive-title">
Rental <?php echo $kendaraan ; ?><?php echo $merk ; ?><?php echo $kendaraanParent->name ; ?><?php echo $seat ; ?> - TravelBos.id
</h1>
</header><!-- .archive-header -->

Code above works great for when I filter all three taxonomies.

https://example/rental/?kendaraan=sedan&merk=honda&seat=4seat

But when filtering two, one, or none taxonomy(ies) it turned stupid.

Any ideas? Thank you!

Share Improve this question edited Nov 11, 2018 at 8:14 Syamsul Alam asked Nov 11, 2018 at 5:36 Syamsul AlamSyamsul Alam 11 silver badge6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I'll answer this question myself since I found the solution after consulting with my programmer friend.

Basically the code above that I posted is wrong, turns out I shouldn't be using get_the_term_list but to use get_query_var instead.

The final code is to replace the above code I posted with these code:

EDIT: Change few things to call proper "taxonomy name" as taxonomy slug can be different with taxonomy name. Credit @Milo.

<?php
// get the currently queried taxonomy term, for use later in the template file
$kendaraan = get_query_var('kendaraan');
$merk = get_query_var('merk');
$seat = get_query_var('seat');
$kendaraan = get_term_by( 'slug', $kendaraan, 'kendaraan' );
$merk = get_term_by( 'slug', $merk, 'merk' );
$seat = get_term_by( 'slug', $seat, 'seat' );
?>

<h1 class="archive-title">Rental 
<?php if ( empty( $kendaraan ) && empty( $merk ) && empty( $seat ) ): ?>
Mobil, Bus, & Motor - TravelBos.id
<?php else: ?>
<?php echo $kendaraan->name; ?> <?php echo $merk->name; ?> <?php if (!empty($seat)) {echo "(". $seat->name .")";} ?> - TravelBos.id
<?php endif; ?>
</h1>

Now this code will make it possible for the H1 archive heading to change based on the taxonomy being filtered when viewing that particular archive page.

Hope it helps anyone who has the same problem as me.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749175606a316995.html

最新回复(0)