WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Modify Page Title Outside of Header.php (7 posts)

  1. Casey
    Member
    Posted 3 years ago #

    Is there a way to programmatically assign the page title outside of the header.php file?

    In one of my page template files, I would like to dynamically assign the page title based on a value in a custom field. Is this possible?

  2. Casey
    Member
    Posted 3 years ago #

    Is there a WordPress hook or anything I could use to do this?

  3. Casey
    Member
    Posted 3 years ago #

    I was able to use a filter hook to assign a value to wp_title:

    function assignPageTitle(){
      return "Title goes here";
    }
    add_filter('wp_title', 'assignPageTitle');
  4. gcoghill
    Member
    Posted 3 years ago #

    Could you explain a bit more on how you accomplished this?

    Specifically, I am looking to take the wp_title and wrap it in some standard text that's used on all the pages for a custom post type, so authors don't have to enter redundant info for each post.

    Looking to do this:
    <title><?php wp_title( '|', true, 'right' ); ?>STATIC CUSTOM TEXT</title>

    When I modify header.php, I do not see any changes in the browser title. I just get the title of the post.

  5. Casey
    Member
    Posted 3 years ago #

    I created a custom page template and put the code above at the top of the page template. Then just make sure you assign the custom page template to each page that you want the title to show on.

  6. gcoghill
    Member
    Posted 3 years ago #

    Thanks! So does your code retain the original page title, or replace it with the value in the custom field?

    Sorry a beginner on WP so some of this stuff is a bit out of my grasp.

  7. stringrv
    Member
    Posted 3 years ago #

    how do you assign the template to the page programmatically? I have all the templates I want, but I want to be able to assign new pages I create programmatically with a form. I can put the code to assign the template to the page in a widget, but I don't know what code I should use for this. Any suggestions?

Topic Closed

This topic has been closed to new replies.

About this Topic