• ondraa

    (@ondraa)


    hello,

    is there any way how to get page/post ID in functions.php file?

    this code

    global $wp_query;
    $postid = $wp_query->post->ID;

    doesn’t work for me, i dumped $wp_query variable after “globalizing” and this variable looks like empty

    [query] =>
        [query_vars] => Array
            (
            )
    
        [queried_object] =>
        [queried_object_id] =>
        [request] =>
        [posts] =>
        [post_count] => 0
        [current_post] => -1
    .
    .
    .
    .
    .
    .
    .
    .
    .

    thanks for help

Viewing 10 replies - 1 through 10 (of 10 total)
  • Michael

    (@alchymyth)

    this often works:

    global $post; $postid = $post->ID;

    Rahul Sonar

    (@rahulsonar)

    you need to create a loop using $wp_query:

    https://codex.www.ads-software.com/The_Loop

    Thread Starter ondraa

    (@ondraa)

    global $post; $postid = $post->ID;

    doesn’t work, $post is empty

    you need to create a loop using $wp_query

    i dont understand, i want get ID of current post/page (in functions.php file), so i dont know what query i need create…

    Michael

    (@alchymyth)

    you might need to give more information:

    where do you call the function?
    are there custom queries in your template before you call the function?

    Thread Starter ondraa

    (@ondraa)

    i am not calling function, on start of functions.php file i want to get ID of post/page and with this ID works inside functions.php file

    Michael

    (@alchymyth)

    on start of functions.php file i want to get ID of post/page

    there is no post/page information available at this point.

    Thread Starter ondraa

    (@ondraa)

    there is no post/page information available at this point.

    well, good to know:)

    i found solution based on function url_to_postid()

    $url = explode('?', 'https://'.$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
    $ID = url_to_postid($url[0]);

    with this i can get post/page ID on the start of functions.php file

    omg ondraa thank you so much, i’ve wasted 6 hours on that problem too !!!!

    i would hug you right now if i could !!!!

    i’m trying to modify the template of only one page of my blog, but since it uses various css files, i needed to load a different css file according to the page… Without the id it was impossible !!!

    THANK YOU !!!

    Thanks for this!
    Does this also work when you have pretty permalinks or custom post types?
    $url = explode('?', 'https://'.$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
    Cheers!

    Endolil

    (@zabdesign)

    I am trying enque js files in my functions.php in dependence of custom field values.

    $url = explode('?', 'https://'.$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
    $ID = url_to_postid($url[0]);

    I was able to output the id of the current page/post through my functions.php with this but I could not get this to work:

    $form = get_post_meta($ID,'form');

    I tried to write a function that could use the $postID to access to a custom field value but that doesn’t work either.

    function custom_field_v($key, $echo = false) {
    	$url = explode('?', 'https://'.$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
    	$postID = url_to_postid($url[0]);
    	$value = get_post_meta($postID, $key, $echo);
    	if($echo == false) {
    		return $value;
    	} else {
    		echo $value;
    	}
    }
    
    // and than get a value
    $form = custom_field_v('form');

    Any idea how to get this to work in the functions.php?

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘get page/post ID in functions.php file’ is closed to new replies.