Global page ID variable empty error

I am creating theme and I have a need to create global variables within theme functions.php file.

Currently I am doing it like this:

/**
* Setup globals
*/
global $nb_id, $nb_page;

$nb_id = get_the_ID();
// Prepare & set fallback
$globals_array = array(
‘nb_id’ => $nb_id,
‘nb_page’ => array(
‘id’ => $nb_id,
‘end’ => ‘front’,
‘name’ => ‘home’,
‘type’ => ‘page’
)
);

// Set each value from globals array as global $var;
foreach ($globals_array as $name => $value) {
global $$name;
$$name = $value;
$_GLOBALS[$name] = $value;

// Set user view of page
if($name == ‘nb_page’){
if(is_admin()) $$name[‘end’] = ‘back’;
$$name[‘name’] = $nb_pagenow;
// Determine if is page or single post
if(is_single()) $$name[‘type’] = ‘single’;

$_GLOBALS[$name] = $$name;
}
}

The issue is that I cannot retrieve front-end current page/post ID using get_the_ID(), $wp_query->get_queried_object_id() or even $wp->public_query_vars with page_id.

Currently all the scenarios return these values:

get_the_ID() = null
$wp_query->get_queried_object_id() = 0
$wp->public_query_vars = “page_id” [34]=> string(5) “error”

Does anyone have any idea why I am having this issue? Am I doing something wrong? Maybe there is another way to call front-end page/post data within functions.php!?

Read more here:: Global page ID variable empty error

Leave a Reply

Your email address will not be published. Required fields are marked *