0

I have this code in my custom template file:

<?php
/*
Template Name: custom-template-view
*/
ob_start();
get_header();


function assignPageTitle(){
  return "my page title";
}
add_filter('wp_title', 'assignPageTitle');// not working
add_filter('the_title', 'assignPageTitle'); // not working


?>

<div>MY CONTENT</div>

<?php get_footer(); ?>

I want to set custom title tag but it is not working. In this template I am showing records from custom table.

  • Did you try <?php the_title();?>? – Gregory Schultz Jul 27 '16 at 8:06
2

Template files are not the right place to add in functions like that. The template file isnt loaded until late in the hook/filter process. Your function should be in functions.php. This way it can be added in before your tempalte files load, and functions that are in use in them can be altered.

Often times wp_title() is used in the header.php so it possible you are using that, but form the code you show you are filtering functions you are not using. Except maybe in header.php, but its loaded and executed before this custom template file. You also do not show the use of the_title() so I can not tell how/when/where this would actually filter anything if the function was placed in the right location.

0

Why don't you just echo you custom post title? For example:

if (some_conditional) {
    echo 'My custom title';
} else {
    the_title();
}

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.