Be Studios

Intercontinental Design Firm

This is our knowledge center. Enjoy!

WordPress: Get Page Title by ID

This function is used to get the title(); of a certain page using it's ID (outside the loop).

function getPageTitle($pageId){  
    if(!is_numeric($pageId)) {  
        return;  
    }  
    global $wpdb;  
    $sql_query = 'SELECT DISTINCT * FROM ' . $wpdb->posts . ' WHERE ' . $wpdb->posts . '.ID=' . $pageId;  
    $posts = $wpdb->get_results($sql_query);  
    if(!empty($posts)) {  
        foreach($posts as $post) {  
            return nl2br($post->post_title);  
        }  
    }  
}

To use it in your theme, just:

<?php getPageTitle(ID); ?>

Comments

Luca: Why not "<?php echo get_the_title($pageId) ?>" ? Cheers