0

I've been recently spending some time building a blog to use as my personal portfolio. So far all of my problems have been answered by videos or articles since I'm still a beginner. But, I think I've found an issue that hasn't been easy to research on my own.

For starters, I'm using OceanWPv1.5.16 as my theme. My Wordpress is version 4.9.6 and I have my home page set to display recent posts. By default, the page title is my site's tagline. However I would like to have a custom title. I haven't found a way to change this, so I think I might have to edit the theme directly. This would be my first experience doing that, so I want to be extra clear on my approach.

Here's the page currently: sflsurge.com

Here's the title that I would like on the home page: sflsurge.com/articles

Any guidance (even if it's recommending me a different forum because this is off-topic here) would be greatly appreciated! I think this is the last step before I'm ready to start showing friends!

EDIT: I am trying to edit the page title. When you go to my home page, the title is 'eSports Analysis and Management'. However, I would like to change it to the /articles title 'SFL Surge eSports \n An eSports Analytics blog & podcast network'. My question does not have to do with custom post types.

  • Possible duplicate of Is there a way to override the <title> tag specified in header.php? – Casper Jun 11 '18 at 23:15
  • I'm not working with my own custom theme though. It also seems like header.php doesn't have any <title> tags. – B. Patali Jun 11 '18 at 23:43
  • 2
    I'm not sure there's any way we can tell from looking at the frontend, it's the PHP that generates the title we'd need to see it – Tom J Nowell Jun 12 '18 at 0:19
  • Yoast plugin and All in One SEO let you customise page titles; have you tried one of these? – Nathaniel Flick Jun 12 '18 at 4:13
1

First of all: don't edit third party theme directly!

Use what is called a child theme so the next time you update the main theme, your changes won't be overwritten by the new files.

In your child theme, you will have a functions.php file, where you override the main functionalities from WordPress by hooking actions and filters.

In order to customise the title of your blog, you can hook the filter "wp_title" like so:

add_filter('wp_title', 'my_custom_title');
function my_custom_title( $title )
{
    // Return my custom title
    return sprintf("%s %s", $title, get_bloginfo('name'));
}

To add a custom title to each page, you'd better off using a meta box with a custom field. There's no simple way of doing this but here's a concise snippet for adding the metabox with poor validation (use carefully):

<?php

add_action( 'add_meta_boxes', 'so_add_post_title_metabox' );
add_action( 'save_post_page', 'so_save_post_title_metabox_data' ); // in this case, saving post of post_type "page"

function so_add_post_title_metabox() {
    add_meta_box( 'post_title_meta', __( 'Title', 'domain-name' ), 'so_post_title_metabox_callback', 'page' ); // same as above
}

function so_post_title_metabox_callback($post) {

    $post_id = $post->ID;

    $post_title_meta = get_post_meta( $post_id, 'post_title_meta', true);

    ?>
    <label><?php _e( 'Custom post title', 'domain-name' )?></label>
    <input type="text" name="post_title_meta" value="<?php echo $post_title_meta ?>">
    <?php
}

function so_save_post_title_metabox_data($post_id) {

    if ( !isset($_POST['post_title_meta'] ) ) {
        return $post_id;
    }

    $post_title_meta = sanitize_text_field( $_POST['post_title_meta'] );
    update_post_meta( $post_id, 'post_title_meta', $post_title_meta );

}

To retrieve your custom field and render the custom title:

<?php

add_filter( 'wp_title', 'so_custom_title' );
function so_custom_title( $title ) {

    global $post;

    $post_id = $post->ID;

    if( $custom_title = get_post_meta( $post_id, 'post_title_meta', true ) ) {
        return esc_html( $custom_title );
    }

    return $title;
}

Another way (not recommended in this case) to do that programmatically is by using template parts in your child theme.

There's also plenty of third party plugins to ease the job like WP SEO from Yoast (full featured SEO plugin) and others that will provide just that functionality (can't mention as I don't use any).

| improve this answer | |

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.