WordPress.org

Ready to get started?Download WordPress

Forums

Post title is being displayed instead of Page title (2 posts)

  1. lspoor
    Member
    Posted 1 year ago #

    I've created a simple function to display the page title. I've had to use some conditionals to try and change the title on the blog to use wp_title(); because get_the_title ends up displaying a post title instead of the page title. I've used this code but it doesn't seem to work, I'm still getting a post title.

    function ls_page_title() {
    
    		$checkbox = get_post_meta( get_the_id(), 'ls_disable_page_title', true);
    		// $pageTitle = get_post_meta( get_the_id(), 'ls_page_title', true) ? get_post_meta( get_the_id(), 'ls_page_title', true) : get_the_title();
    
    		if ( is_page( get_option('page_for_posts') ) ) {
    			$pageTitle = wp_title('', false);
    		}
    		else {
    			$pageTitle = get_post_meta( get_the_id(), 'ls_page_title', true) ? get_post_meta( get_the_id(), 'ls_page_title', true) : get_the_title();
    		}
    
    		if ( $checkbox !== '1' ) {
    			echo '<h1 class="page-title">' . $pageTitle . '</h1>';
    		}
    
    	}
  2. lspoor
    Member
    Posted 1 year ago #

    Better yet, as you'll notice I've added a part to let the user specify their own title should they not actually want the one used for the page. As I set my Blog page to 'Blog' I'd like the title to be Blog then but still allow the user to add a custom title should they want to.

Topic Closed

This topic has been closed to new replies.

About this Topic