Wordpress - Change the_title() of a page dynamically
I would use the is_page_template()
conditional:
if ( is_page_template( 'page-courses.php' ) ) {
// The current page uses your
// custom page template;
// do something
}
Edit
You would use this conditional inside your filter callback:
function wpse83525_filter_the_title( $title ) {
if ( is_page_template( 'page-courses.php' ) ) {
return 'Custom Title';
}
return $title;
}
add_filter( 'the_title', 'wpse83525_filter_the_title' );
Now, to isolate only the titles of pages that use your page template, you can take advantage of the other parameter passed to the_title
: $id
. Since you know the Id of the post for which the title is being filtered, you can query the _wp_page_template
post meta, and ensure that it equals your page template:
function wpse83525_filter_the_title( $title, $id ) {
if ( 'page-courses.php' == get_post_meta( $id, '_wp_page_template', true ) ) {
return 'Custom Title';
}
return $title;
}
add_filter( 'the_title', 'wpse83525_filter_the_title', 10, 2 );
Edit 2
If you want to target the "Courses" page specifically, use is_page()
with the page slug 'courses'
, or the page title of 'Courses'
:
function wpse83525_filter_the_title( $title ) {
if ( is_page( 'Courses' ) ) {
return 'Custom Title';
}
return $title;
}
add_filter( 'the_title', 'wpse83525_filter_the_title' );
Though, I would recommend changing page-courses.php
into a Custom Page Template, which would make this whole process much more robust.