获取静态主页的ID,而不是发布

时间:2011-01-29 15:39:48

标签: php wordpress

我开始为Wordpress编写自定义主题。我创建了一个名为“Home”的新页面,并将首页设置为静态页面,选择“Home”。我希望此页面显示所有包含“新闻”类别和一些图片的帖子。

然后我添加了front-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(); ?>

我上传了几张图片并将其附加到“首页”页面。我现在想要获取附加图像的URL。我尝试过类似的东西:

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

但是这只会返回显示的最新帖子的ID,即使我将上面的代码放在循环外面。如果我删除静态首页并导航到主页,那么我得到了正确的结果,当我将它用作静态页面时,我如何获取主页的图像?

请原谅我,如果这很简单,首先涉足PHP和wordpress开发

1 个答案:

答案 0 :(得分: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(我找到上面的代码的地方)有一个函数可以做同样的事情。

希望这有帮助!

相关问题