开发者

Get ID for static homepage, not post

I am starting to write a custom theme for Wordpress. I have created a new page called 'Home' and set the front page to be a static page, selecting 'Home'. I want this page to show all posts with a category of 'news' plus a couple of images.

I then added front-page.php with the following:

<?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(); ?>

I've uploaded a couple of images and attached them to th开发者_开发技巧e 'Home' page. I now want to get the URL for the attached images. I've tried something like:

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

but this only returns the ID of the most recent post being shown, even if I put the above code outside the loop. If I remove the static front page and navigate to the Home page then I get the correct results, any ideas how I can get the images for the Home page when I use it as a static page?

Forgive me if this is simple, first foray into PHP and wordpress development


I think your issue is that you're using get_the_ID(); outside of The Loop. the_ID(); and get_the_ID(); grab the current post ID from the loop; if used outside of The Loop, all you'll get is the last one. See: http://codex.wordpress.org/Function_Reference/get_the_ID

To get the ID of the current page, try:

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

If that doesn't work, there's a function at http://wordpress.org/support/topic/how-to-get-page-id-using-php?replies=5 (Where I found the above code) that does the same thing.

Hope this helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜