发布未显示为附件的图像

时间:2013-09-06 作者:mantis

我正在尝试在中显示附加到帖子的图像single.php 我使用过:

<?php if (have_posts()) : while(have_posts()) : the_post(); ?>

$args = array(
\'post_type\' => \'attachment\',
\'numberposts\' => -1,
\'post_status\' => \'any\',
\'post_parent\' => $post->ID
);


$attachments = get_posts($args);

if ($attachments) : ?>
    <ul class="portfolio-image-list">

    <?php foreach($attachments as $attachment): ?>
        <li class="box">
        <figure>
        <?php the_attachment_link($attachment->ID, true); ?>
            <figcaption><?php echo $attachment->post_title; ?></figcaption>
        </figure>
    </li>
    <?php endforeach; ?>
    </ul>
    <?php else : ?>

            <p>No images found for this post.</p>

<?php endif; ?>

 <?php endwhile; ?>
 <?php endif; ?>
然而,该图像似乎在帖子内容中(经确认var_dump($post) 并且没有附件var_dump($attachments) 给我一个空数组。

我好像误解了什么。。。

var_dump($post); 给我:

 object(WP_Post)#136 (25) { 
 ["ID"]=> int(91) 
 ["post_author"]=> string(1) "1" 
 ["post_date"]=> string(19) "2013-09-04 13:14:17" 
 ["post_date_gmt"]=> string(19) "2013-09-04 13:14:17" 
 ["post_content"]=> string(285) "Snowy Egret"
 ["post_title"]=> string(11) "Snowy Egret"
 ["post_excerpt"]=> string(0) ""
 ["post_status"]=> string(7) "publish"
 ["comment_status"]=> string(4) "open"
 ["ping_status"]=> string(4) "open"
 ["post_password"]=> string(0) ""
 ["post_name"]=> string(11) "snowy-egret"
 ["to_ping"]=> string(0) ""
 ["pinged"]=> string(0) ""
 ["post_modified"]=> string(19) "2013-09-06 08:36:01"
 ["post_modified_gmt"]=> string(19) "2013-09-06 08:36:01      
 ["post_content_filtered"]=> string(0) ""
 ["post_parent"]=> int(0)
 ["guid"]=> string(44) "/trouble_focusing/?p=91" 
 ["menu_order"]=> int(0)
 ["post_type"]=> string(4) "post"
 ["post_mime_type"]=> string(0) ""
 ["comment_count"]=> string(1) "0"
 ["filter"]=> string(3) "raw"
 ["format_content"]=> NULL
}

1 个回复
SO网友:hariprasad

尝试查询帖子而不是$attachments=get\\u posts($args);或者像这样做

<?php
$args = array( \'posts_per_page\' => 5, \'offset\'=> 1, \'category\' => 1 );

$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endforeach; 
wp_reset_postdata();?>
同时检查此urlhttp://codex.wordpress.org/Template_Tags/get_posts

结束

相关推荐

how to edit attachments?

在将例如文件附加到帖子时,如何在事后编辑/删除它们?在帖子编辑器中找不到任何内容。谢谢