JavaEar 专注于收集分享传播有价值的技术资料

Wordpress Get the Page ID outside the loop

I want to get the page ID before starting the loop in Wordpress. I am using

$page = get_query_var('page_id');

Apparently, it returns nothing.

I just want to check a page for its ID and add a class to <body> tag based on it.

10个回答

    最佳答案
  1. You can also create a generic function to get the ID of the post, whether its outside or inside the loop (handles both the cases):

    <?php
    
    /**
     * @uses WP_Query
     * @uses get_queried_object()
     * @see get_the_ID()
     * @return int
     */
    function get_the_post_id() {
      if (in_the_loop()) {
           $post_id = get_the_ID();
      } else {
           global $wp_query;
           $post_id = $wp_query->get_queried_object_id();
             }
      return $post_id;
    } ?>
    

    And simply do:

    $page_id = get_the_post_id();
    
  2. 参考答案2
  3. Use this global $post instead:

    global $post;
    echo $post->ID;
    
  4. 参考答案3
  5. If you by any means searched this topic because of the post page (index page alternative when using static front page), then the right answer is this:

    if (get_option('show_on_front') == 'page') {
        $page_id = get_option('page_for_posts');
        echo get_the_title($page_id);
    }
    

    (taken from Forrst | Echo WordPress "Posts Page" title - Some code from tammyhart)

  6. 参考答案4
  7. If you're using pretty permalinks, get_query_var('page_id') won't work.

    Instead, get the queried object ID from the global $wp_query:

    // Since 3.1 - recommended!
    $page_object = get_queried_object();
    $page_id     = get_queried_object_id();
    
    
    // "Dirty" pre 3.1
    global $wp_query;
    
    $page_object = $wp_query->get_queried_object();
    $page_id     = $wp_query->get_queried_object_id();
    
  8. 参考答案5
  9. 参考答案6
  10. This function get id off a page current.

    get_the_ID();
    
  11. 参考答案7
  12. Use below two lines of code to get current page or post ID

    global $post;
    echo $post->ID;
    
  13. 参考答案8
  14. If you're on a page and this does not work:

    $page_object = get_queried_object();
    $page_id     = get_queried_object_id();
    

    you can try to build the permalink manually with PHP so you can lookup the post ID:

    // get or make permalink
    $url = !empty(get_the_permalink()) ? get_the_permalink() : (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    $permalink = strtok($url, '?');
    
    // get post_id using url/permalink
    $post_id = url_to_postid($url);
    
    // want the post or postmeta? use get_post() or get_post_meta()
    $post = get_post($post_id);
    $postmeta = get_post_meta($post_id);
    

    It may not catch every possible permalink (especially since I'm stripping out the query string), but you can modify it to fit your use case.

  15. 参考答案9
  16. This is the correct code.

    echo $post->ID;
    
  17. 参考答案10
  18. If you are out of the Loop of WordPress you can not use any of the method of wordpress so you must use pure php.

    You can use this code. And sure will help you :)

    $page_id = @$_GET['page_id'];
    
    if (!is_numeric($page_id)) {
        // Then the uri must be in friendly format aka /my_domain/category/onepage/
        // Try this
        //$path = '/www/public_html/index.php/';
        ///$path = '/my_domain/category/onepage/';
        $path = $_SERVER['REQUEST_URI'];
        // Clean the uri
        //$path = str_replace('/', '', $page);
        $path = str_replace('.php', '', $path);
        //$path = str_replace('?s=', '', $path);
        $path = $path ? $path : 'default';
    
        $path_len = strlen($path);
        $last_char = substr($path, $path_len -1);
        //echo $last_char;
        $has_slash = strpos($last_char, "/");
        //echo $has_slash;
        if ($has_slash === 0) :
            $path = substr($path, 0, $path_len -1);
        elseif ($has_slash === null) :
            $path = substr($path, 0, $path_len);
        endif;
        //echo "path: ".$path; // '/www/public_html/index'
        $page = substr(strrchr($path, "/"), 1);
        echo "page: ".$page; // 'index'
    }
    
    $my_page_id = 31;
    $my_page = 'mypage';
    
    //echo "page: ".$page;
    //echo "page_id ".$page_id;
    if($page_id == $my_page_id || $page == $my_page) 
    {
        // your stuff....
    }
    

    Enjoy!