0

I'm wondering if I could make a custom Page title for my Custom Post Type Archive page? Right now I'm using:

<title><?php wp_title( '|', true, 'right' ); ?></title>

For my regular pages, it display what I want, but on my custom post type archive page it displays:

Custom Post Type Name Archive

Is it possible to add a custom page title for just that page?

1

You can use is_archive conditional code in your header.php to control the title

    <?php if(is_archive()): ?>
    <title>Archive page</title>
    <?php else: ?>
    <title><?php wp_title( '|', true, 'right' ); ?></title>
    <?php endif; ?>
-1

Yes and there's several ways to do it.

Hook it in from your child themes functions file

function wpsites_add_cpt_archive_page_title() {

if ( is_post_type_archive( 'your-cpt-name' ) )

echo '<h1 class="entry-title">Your CPT Archive Title</h1>';

}

add_action('genesis_before_content', 'wpsites_add_cpt_archive_page_title');

The above code snippet uses the genesis_before_content hook which you can change to a theme specific hook or a WordPress hook like loop_start.

You can also add support for a custom post type archive settings page in the code which creates the CPT if your theme supports it.

  • OP seems to be asking for the <title> element in the HTML <head>. – leymannx May 10 at 19:07
-2

You can use this now

<?php 
if(archive()) {
  if (have_posts() ) :
    the_archive_title( '<h1 class="page-title">', '</h1>' );
    while ( have_posts() ) : 
      the_post();
    endwhile;
  endif; 
}
?>

or

<?php
$post_archive = post_type_archive_title('', false);
?>

to answer the post

<?php if ( is_post_type_archive() ) { ?>
<title><?php post_type_archive_title(); ?></title>
<?php } ?>

reference

wp docs

  • There is no check for a custom post type archive in your code. Please edit your answer, and add that. – fuxia May 10 at 18:34
  • OP seems to be asking for the <title> element in the HTML <head>. – leymannx May 10 at 19:06
  • You can use this in your title and the title of this question doesnt seem to be particulary asking for the html page title but the archive title, people wont be looking for the html page title when they find this post but the archive title in common. – DutchPrime May 11 at 7:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.