2010-06-27 82 views
66

我想在啓動Wordpress循環之前獲取頁面標識。我使用Wordpress獲取循環外的頁面編號

$page = get_query_var('page_id');

顯然,它沒有返回。

我只想檢查一個頁面的ID,並根據它爲<body>標記添加一個類。

+3

http://stackoverflow.com/questions/22351038/get-the-current-page-id-inside-wordpress-plugin- I/ – 2016-12-17 13:27:50

回答

141

如果你使用漂亮的固定鏈接,get_query_var('page_id')將不起作用。

取而代之的是,從全球$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(); 
+0

完美的永久鏈接。我用全局$ post; echo $ post-> ID;但沒有成功。謝謝! – 2013-09-28 08:43:49

+4

'get_queried_object_id();'爲我返回0。我認爲問題在於我在自定義查詢後調用它。我想要實際的頁面ID。 – Victor 2014-05-23 09:26:45

+0

奇怪的是,'get_queried_object();'不適合我,但是'$ wp_query-> get_queried_object();'確實......我看了一下'get_queried_object();'這和做後者。 – SeanJA 2014-09-24 13:48:55

0

您可以在循環外使用is_page($page_id)進行檢查。

+0

I不想檢查頁面,我想獲取當前頁面的ID。 – 2010-06-27 13:31:21

+0

@atif你確定頁面ID實際上是通過了嗎?你不會在首頁上? – 2010-06-27 13:57:42

+0

是的,我沒有檢查它 – 2010-06-27 18:50:12

5

此全局$後代替:

global $post; 
echo $post->ID; 
+0

這隻會在循環之後工作,而不是之前,因爲'$ post'在啓動「循環」時被初始化。 – 2013-05-28 08:32:51

+6

@ChristianDavén--這是不正確的。此代碼適用於頁面開頭.php – CroiOS 2013-06-14 10:31:31

0

此功能得到ID關閉頁面電流。

get_the_ID(); 
+4

um ...這隻適用於[在循環中](http://codex.wordpress.org/Function_Reference/get_the_ID): _返回數字ID當前職位。 **這個標籤必須在循環內。** _ – drzaus 2013-03-01 22:18:31

+0

@drzaus其實這可以在循環之外...檢查出來。 – hitautodestruct 2015-04-19 06:34:58

+1

@hitautodestruct雖然你在技術上是正確的,它可以在循環之外工作,但這不是一個可靠的用法 - 這是從個人經驗和源代碼的角度出發的。底層方法[get_post](https://core.trac.wordpress.org/browser/tags/4.1.1/src/wp-includes/post.php#L417)恰好使用'$ GLOBALS ['post'] ',它可能在某個時候被填充,但是不能保證_unless /直到你進入循環。 – drzaus 2015-04-20 16:35:00

-4

這是正確的代碼。

echo $post->ID; 
-4

如果你不在WordPress的Loop中,你不能使用任何wordpress的方法,所以你必須使用純php。

您可以使用此代碼。並肯定會幫助你:)

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

享受!

+2

在所有方面都非常錯誤。 – JakeParis 2014-07-30 20:13:15

+0

也許..您能否介紹一下這方面的更多細節並向我展示您的解決方案? – edcv 2014-11-10 19:10:40

+1

您編寫了50行代碼來獲取'$ post-> ID'中已經存在的變量。即使你不在循環中,也可以使用許多Wordpress功能。只是不是必須在循環中使用的少數幾個。 – JakeParis 2014-11-10 21:49:19

7

您還可以創建一個通用的函數來獲取帖子的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; 
} ?> 

而簡單地做:

$page_id = get_the_post_id(); 
+0

這實際上工作嗎?它可以是一個無限遞歸函數,或者它會中斷,因爲'get_the_ID'是一個已經聲明的函數。我想你的意思是改變函數名稱,或者這是類的一部分? – drzaus 2015-04-21 18:21:34

+0

@drzaus感謝您指出了這一點。我修好了它。 – 2016-03-25 07:35:07

0

使用以下兩行代碼獲取當前頁面或帖子ID

global $post; 
echo $post->ID; 
0

如果您在一個頁面上,而這不工作:

$page_object = get_queried_object(); 
$page_id  = get_queried_object_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); 

它可能無法抓住每一個可能的永久鏈接(尤其是因爲我剝出查詢字符串),但您可以修改它以適合您的用例。

0

我用下面的方法完成了它,它對我來說非常合適。

首先宣佈在header.php中一個全局變量,分配後或在改變之前頁面的ID,因爲循環分配給它顯示的最後一個條目的ID:

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

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

echo $GLOBALS['pageid];