2011-01-29 22 views
2

我開始爲Wordpress編寫自定義主題。我創建了一個名爲'Home'的新頁面,並將首頁設置爲靜態頁面,選擇'Home'。我希望此頁面顯示所有包含「新聞」類別和一些圖片的帖子。獲取靜態主頁的ID,而不是發佈

我加入前page.php文件有以下幾點:

<?php get_header(); ?> 

<div class='detail'> 
    <?php if (have_posts()) { 
    query_posts('category_name=news'); 
    while (have_posts()) : the_post(); ?> 
     <h4><?php the_date('d/m/Y'); ?> - <?php the_title(); ?></h4> 
     <div class='post'><?php the_content(); ?></div> 
    <?php endwhile; }?> 
</div> 

<?php get_footer(); ?> 

我上傳了一些圖片,並將它們連接到「主頁」。我現在想要獲取附加圖片的網址。我已經試過類似:

$images =& get_children('post_type=attachment&post_mime_type=image&post_parent=' . get_the_ID()); 

但這只是返回最近的帖子的ID被顯示,即使我把上面的代碼外循環。如果我刪除靜態首頁並導航到主頁,那麼我會得到正確的結果,當我將其用作靜態頁面時,如何獲取主頁圖像?

原諒我,如果這是簡單的,首次進軍PHP和WordPress開發

回答

1

我覺得你的問題是,你正在使用get_the_ID();在循環之外。 the_ID();和get_the_ID();從循環中獲取當前的帖子ID;如果在The Loop之外使用,你會得到的是最後一個。請參閱:http://codex.wordpress.org/Function_Reference/get_the_ID

爲了得到當前頁面的ID,請嘗試:

$page=get_page_by_title($page_name); 
$images =& get_children('post_type=attachment&post_mime_type=image&post_parent='.$page->ID); 

如果不工作,有一個在http://wordpress.org/support/topic/how-to-get-page-id-using-php?replies=5(其中我發現上面的代碼),做同樣的事情功能。

希望這會有所幫助!