WordPress.org

Forums

get page/post ID in functions.php file (11 posts)

  1. ondraa
    Member
    Posted 4 years ago #

    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

  2. alchymyth
    Forum Moderator
    Posted 4 years ago #

    this often works:

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

  3. Rahul Sonar
    Member
    Posted 4 years ago #

    you need to create a loop using $wp_query:

    http://codex.wordpress.org/The_Loop

  4. ondraa
    Member
    Posted 4 years ago #

    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...

  5. alchymyth
    Forum Moderator
    Posted 4 years ago #

    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?

  6. ondraa
    Member
    Posted 4 years ago #

    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

  7. alchymyth
    Forum Moderator
    Posted 4 years ago #

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

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

  8. ondraa
    Member
    Posted 4 years ago #

    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('?', 'http://'.$_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

  9. patrox
    Member
    Posted 4 years ago #

    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 !!!

  10. Twansparant
    Member
    Posted 3 years ago #

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

  11. endolil
    Member
    Posted 3 years ago #

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

    $url = explode('?', 'http://'.$_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('?', 'http://'.$_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?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.