Skip to content
Advertisement

How to get the wordpress archive title? Is their any function for checking the title of current page displayed?

I am a beginner in PHP!

I am using Free Kadance theme on WordPress.

In the Theme customization option >> Homepage Settings >> we can toggle the title ON or OFF.

If we toggle the title ON it will display the title of the website on homepage with the latest posts. I like to display the page title along with some description text on that home page. After sometime of analysis inside WordPress Theme editor.

On WordPress Theme editor >> template-parts >> archive-title >>title.php

There is a section of php code

elseif ( is_archive() || is_home() ) {
the_archive_title( '<h1 class="page-title archive-title">', '</h1>' );
}

This section of code is responsible for printing the header text that we toggle ON.

Is there some way to get the text which is printed by function: the_archive_title( )

If I can get that text to a variable, then I can check that statement to the title string and if both are equal I could execute desired php file.

Somebody please help!

I tried

      elseif ( is_archive() || is_home() ) {
      the_archive_title( '<h1 class="page-title archive-title">', '</h1>' );
      
      //custom code
         $title_var_to_check = get_the_archive_title();
         echo title_var_to_check;
         if(title_var_to_check == "string to check"){
         ?>
         <p>Checking text </p>
         <?php
         }
     //custom code
     }

Result : It worked partially! But every Archive page displayed a text “title_var_to_check” under it.

Advertisement

Answer

You need add $ to title_var_to_check

echo $title_var_to_check;

and

if($title_var_to_check == "string to check"){
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement