2011-02-15 42 views
0

嘿傢伙, 也許你們有一些編程WordPress插件的經驗。我有一個可能相當明顯的問題,但我在網上找不到任何東西。wordpress插件:在插件中查詢post-ID?

<?php 
/* 
Plugin Name: test 
*/ 

function test($content) { 

    echo $post_id; 
    return $content; 
} 

add_filter('the_content', 'test'); 
?> 

我有一個simpel插件,它應該回顯其內容中每個帖子的唯一ID。 因此,在我的每個帖子有10個職位的首頁應該有它的ID呼應。

任何想法如何實現?謝謝!

+0

解決方案:http://stackoverflow.com/questions/10978524/get-the-post-id-in-wordpress-plugin/18947526#18947526 – ekussberg 2013-09-22 18:49:20

回答

2

我的猜測是 使用global關鍵字來訪問帖子的ID在功能

,也是我的猜測是回報和回聲都不會在功能一起工作

function test($content) { 
     global $post; 
     return $post->ID.'<br>'.$content; 
    } 
+0

謝謝你,但不工作! – matt 2011-02-15 16:41:20

+0

@mathiregister檢查更新了一個 – 2011-02-15 16:45:03

1

你混合回聲和回報 - 那不起作用。然而,嘗試:

function test($content) 
{ 
    return "id: ".$post_id."<br/>".$content; 
} 

也,請務必使用小寫標識,因爲它是區分大小寫的

http://codex.wordpress.org/Function_Reference/get_the_ID可能是有用的藏漢

0

過濾器應該返回,沒有迴音。

function test($content) { 
    global $post; 
    return 'id: ' . $post->ID . '<br />' . $content; 
} 

爲了看後對象的屬性,你必須把$post到函數的範圍,這是該行所做的..

global $post; 

這然後允許參照對象的ID ,例如。

$post->ID; 

請參閱此處瞭解幫助瞭解操作和過濾器。
http://codex.wordpress.org/Plugin_API

實施例的過濾器。
http://codex.wordpress.org/Plugin_API#Example