php WordPress 在循环外获取页面 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3127385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:45:56  来源:igfitidea点击:

WordPress Get the Page ID outside the loop

phpwordpress

提问by Atif Mohammed Ameenuddin

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

我想在 WordPress 中开始循环之前获取页面 ID。我在用

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

我只想检查一个页面的 ID 并<body>根据它添加一个类来标记。

回答by TheDeadMedic

If you're using pretty permalinks, get_query_var('page_id')won't work.

如果您使用的是漂亮的永久链接,get_query_var('page_id')则行不通。

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

相反,从 global$wp_query获取查询的对象 ID :

// 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();

回答by Nadeem Khan

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):

您还可以创建一个通用函数来获取帖子的 ID,无论是在循环外还是在循环内(处理这两种情况):

<?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();

回答by ryscript

Use this global $post instead:

改用这个全局 $post:

global $post;
echo $post->ID;

回答by banesto

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)

(取自Forrst | Echo WordPress“帖子页面”标题 - 来自 tammyhart 的一些代码

回答by Jarrett Barnett

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:

您可以尝试使用 PHP 手动构建永久链接,以便您可以查找帖子 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.

它可能无法捕获所有可能的永久链接(特别是因为我正在剥离查询字符串),但您可以修改它以适合您的用例。

回答by Edd

I have done it in the following way and it has worked perfectly for me.

我以以下方式完成了它,它对我来说非常有效。

First declared a global variable in the header.php, assigning the ID of the post or page before it changes, since the LOOP assigns it the ID of the last entry shown:

首先在 header.php 中声明一个全局变量,在更改之前分配帖子或页面的 ID,因为 LOOP 为其分配了显示的最后一个条目的 ID:

$GLOBALS['pageid] = $wp_query->get_queried_object_id();

$GLOBALS['pageid] = $wp_query->get_queried_object_id();

And to use anywhere in the template, example in the footer.php:

并在模板中的任何地方使用,例如footer.php:

echo $GLOBALS['pageid];

echo $GLOBALS['pageid];

回答by Braj Kishor Sah

Use below two lines of code to get current page or post ID

使用以下两行代码获取当前页面或帖子 ID

global $post;
echo $post->ID;

回答by nikc.org

You can use is_page($page_id)outside the loop to check.

您可以使用is_page($page_id)循环外来检查。

回答by jruzafa

This function get id off a page current.

此函数从当前页面获取 id。

get_the_ID();

回答by Banna

This is the correct code.

这是正确的代码。

echo $post->ID;