2011-01-29 40 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(其中我发现上面的代码),做同样的事情功能。

希望这会有所帮助!