Custom Post Type - Archive page title
-
18-04-2021 - |
Question
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?
La solution
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; ?>
Autres conseils
You can add a filter in the file functions.php of your template. It modify the archive page meta title of the custom post type 'myCPT'
function new_cpt_archive_title($title){ if ( is_post_type_archive('myCPT') ){ $title = 'My Custom post type archive - ' . get_bloginfo('name'); return $title; } return $title; } add_filter( 'pre_get_document_title', 'new_cpt_archive_title', 9999 );
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.
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