WordPress.org

Ready to get started?Download WordPress

Forums

Modify html title element to include site 'section' name. (5 posts)

  1. gulliver
    Member
    Posted 1 year ago #

    I have a site with a static front page and a posts page of 'notes'.

    How do I modify the html title element to include 'notes' rather than the default of just site plus post title?

    Also, with subpages, how do I include the name of the parent in the title? Example: 'fyi' is the parent of 'contact', and the browser title should be sitename > fyi > contact - rather than sitename > contact.

  2. I suggest using a plug-in like All in One SEO Pack or WordPress SEO by Yoast in order to more easily set custom titles.

  3. gulliver
    Member
    Posted 1 year ago #

    Thanks. I'll look at that option - and still prefer to hardcode them with functions and conditionals though.

  4. Fire Truck
    Member
    Posted 1 year ago #

    How do I modify the html title element to include 'notes' rather than the default of just site plus post title?

    if(is_home()){
            single_post_title();
        }

    with subpages, how do I include the name of the parent in the title?

    if($post->post_parent){
            echo get_post($post->post_parent)->post_title;
        }
  5. gulliver
    Member
    Posted 1 year ago #

    Thanks.

    Since originally posting I went back to it and after a bit of searching and experimenting came up with something which seems to work:

    <title><?php
    
    /* Print the <title> tag based on what is being viewed. */
    
    // Add the site name and separator.
    bloginfo('name');  echo ' | ';
    
    // Add the site description for the front page.
    $site_description = get_bloginfo('description', 'display');
    if ( $site_description && ( is_front_page() ) ) echo "$site_description";
    
    // Custom title for 404.
    if (is_404()) echo 'file not available';
    
    // Custom title for search results.
    if (is_search()) {echo 'search result for: '; the_search_query('');}
    
    // Show parent page title.
    if($post->post_parent)  {$parent_title = get_the_title($post->post_parent); echo $parent_title. ' : ';}
    
    if (is_page()) wp_title('');
    
    // Show page title on posts page.
    if (is_home()) wp_title('');
    
    // Show section title on posts.
    if (is_single()) wp_title('Clips: ');
    
    // Add a page number if necessary:
    if ($paged >= 2 || $page >= 2 ) echo ' - ' . sprintf( __('page %s'), max($paged, $page) );
    
     ?>
    </title>

    (In this example, the site has a static front page and the posts page is 'clips'.)

    I don't pretend to understand much of this stuff, and there's a few things which seem odd but without which I couldn't get things to dispay as required.

    If there's anything that's a problem, please tell me.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.