WordPress自定义帖子类型特色图像

时间:2014-04-20 21:29:47

标签: php wordpress foreach

以下是我正在处理的脚本。我正在使用自定义post_types。在帖子上我附上了一张精选图片,但在下面的剧本中,我似乎无法将图像放到我的页面上。

<?php
        $myrows = $wpdb->get_results( "SELECT * FROM nvi_posts
        WHERE post_type = 'custom' AND post_status='publish'" );
        foreach ($myrows as $row) {?>
        <section class="custom <?php echo $row->post_name;?>">
            <div class="container">
                <div class="custom-item">
                    <?php if (has_post_thumbnail( $row->ID )) { ?>
                    <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' ); ?>
                    <img src="<?php echo $image[0]; ?>"/>
                    <?php } ?>
                    <h3><?php echo $row->post_title;?></h3>
                    <a href="<?php //the_permalink(); ?>" class="btn btn--hell">View</a>
                </div>
            </div>
        </section>
    <?php }?>

1 个答案:

答案 0 :(得分:1)

你不应该自己写查询,你应该做的是使用WP_Query。这就是你如何做到的。

<?php
$wpbp = new WP_Query( array( 'post_type' => 'YOUR_CUSTOM_POST_TYPE_NAME' ) );
if ( $wpbp->have_posts() ) :  while ( $wpbp->have_posts() ) : $wpbp->the_post(); ?>
    <section class="custom">
            <div class="container">
                <div class="custom-item">
                <?php if ( ( function_exists('has_post_thumbnail') ) && ( has_post_thumbnail() ) ) :  ?>
                    <?php the_post_thumbnail( 'full' ); ?>
                <?php endif; ?>
                <h3><?php the_title(); ?></h3>
                <a href="<?php //the_permalink(); ?>" class="btn btn--hell">View</a>
            </div>
        </div>
    </section>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
相关问题