Wordpress列出当前父级的子页面内容

时间:2014-02-26 11:47:15

标签: php wordpress if-statement

使用以下代码我已成功列出WordPress页面标题。这些页面有特色图片。当我运行以下代码时,图像显示为Image Unavailable而不是实际正确的图像。

我错过了什么吗?标题显示正确。

PHP

<?php query_posts(array('showposts' => 30, 'post_parent' => $post->ID, 'post_type' => 'page'));

    while (have_posts()) { the_post();

        if(has_post_thumbnail()) { ?>
            <div class="entry-thumbnail">
                <?php the_post_thumbnail('medium');?>
            </div>
        <?php } else { ?>
            <div class="entry-thumbnail">
                <img src="/assets/dummy-image.jpg" alt="Image Unavailable" />
            </div>
        <?php } ?>

        <?php the_title();
    }
    wp_reset_query();  // Restore global post data
}?>

2 个答案:

答案 0 :(得分:0)

has_post_thumbnail()有时失败,因为codex在此处说http://codex.wordpress.org/Function_Reference/has_post_thumbnail

你可以尝试以下

if ( '' != get_the_post_thumbnail() ) { ?>
            <div class="entry-thumbnail">
                <?php the_post_thumbnail('medium');?>
            </div>
        <?php } else { ?>
            <div class="entry-thumbnail">
                <img src="/assets/dummy-image.jpg" alt="Image Unavailable" />
            </div>
        <?php } ?>

答案 1 :(得分:0)

<?php $this_page_id=$wp_query->post->ID; ?>

        <?php query_posts(array('showposts' => 20, 'post_parent' => $this_page_id, 'post_type' => 'page')); while (have_posts()) { the_post(); ?>


                        <?php $url = wp_get_attachment_url( get_post_thumbnail_id($this_page_id, 'thumbnail') ); ?>
                        <img src="<?php echo $url ?>" />

                        <h2><?php the_title(); ?></h2>


        <?php } ?>