I'm developing a plugin that doesn't use a custom post type, but separate database tables. It's a plugin that displays a list of courses with links that lead to the different course detail pages, where the user then can subscribe for a course.
In the current state, I'm using a shortcode to get the plugins data into a page with a custom page template (page-courses.php).
I now want to change the_title()
dynamically, according to the page that the plugin shows (list of courses, course details page with forms, form submission success page). But whenever I do that with the following filter, the links to other pages in the footer also change:
<?php
add_filter('the_title', 'custom_page_title');
function custom_page_title() {
return 'Custom Title';
}
Edit
In the footer.php I have a function that includes footer links with wp_nav_menu()
so I can define them in Appearance > Menus. But with the filter above, all the links in the footer also change to 'Custom Title'. But I just want to change the title of the page, not affecting menu links in the footer.
Trying to add a conditional tag in_the_loop()
the footer links are still affected, although they are not in the loop.
<?php
add_action( 'loop_start', 'set_custom_title' );
function set_custom_title() {
if ( in_the_loop() ) {
add_filter( 'the_title', 'custom_page_title' );
}
}
function custom_page_title() {
return 'Custom Title';
}
It’s similar to this question: filter the_title problem in nav, just that the links affected are in the footer and in_the_loop()
doesn't work.
How I can I change the_title()
while only affecting the title of the current page being shown not affecting links in the footer?
Edit 2 - Solution
So I finally got it working:
<?php
add_action( 'loop_start', 'set_custom_title' );
function set_custom_title() {
add_filter( 'the_title', 'wpse83525_filter_the_title', 10, 2 );
}
function wpse83525_filter_the_title( $title, $id ) {
if ( 'page-listcourses.php' == get_post_meta( $id, '_wp_page_template', true ) ) {
return 'Custom Title';
}
return $title;
}
The file page-listcourses.php is a Custom Post Template that I assigned to the static page named 'Courses'.
I assume it didn't work before because the name of the static page and filename of the Custom Post Template were the same.
in_the_loop()
is set totrue
wheneverthe_post()
is fired. Without digging too deeply into core, and knowing that nav menus are themselves loops, I'm guessing that somewhere along the way,wp_nav_menu()
instantiates a loop, causingin_the_loop()
to returntrue
.