0

私はphpコードを含むhtmlのこの部分を持っています

<div id="featuredtext" class="extend">
    <?php 

        // select a category and the number of post
        $featucat = "featured";
        $featucount = "1";
    ?>

    <?php
        //declare query
        $my_query = new WP_Query('showposts='. $featucount .'&category_name='. $featucat .'');
        //if there is post then
        if ($my_query->have_posts()){

    ?>      
    <?php
        //start the looping of post
        while ($my_query->have_posts()) : $my_query->the_post();$do_not_duplicate = $post->ID; ?>
    <?php
        //get the featured image of a post
        $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>

    <?php
        //if theres featured image then..
        if (has_post_thumbnail()) {
        //put the image into a variable
        $banner = "<img class='extend' src='".$image[0]."' style='width: 100%;' />";
        //else if there is none then..
    }else{
        //since there is no featured image into the post then let set a fallback..
        $banner = "<img class='extend' src='".bloginfo('template_directory')."'/images/banner.jpg' style='width: 100%;' />";
    }?>
        <h2>
            <?php
            //display the title
            the_title();
            ?>
        </h2>
        <p>
            <?php
                //display the content
                the_content();
            ?>
        </p>        
    <?php
        //close the while loop
    endwhile; }
    //if there is no post on the specified category then display the default or the fallback
    else{ ?>    
        <h2>Yes! This is the <b style="color: #e1e315; font-weight: bold;">Featured text!</b> here you put all shout you had</h2>
        <p>a molestie <b style="color: #7e7d7d; font-weight: bold;">lacus hendrerit</b>. In arcu dolor, ulla <b style="color: #7e7d7d; font-weight: bold;" >mcorper sed</b> sollicitudin</p>

    <?php } ?>
</div>
<div class="clear extend" style="height: 10px;"></div>
<!-- display the featured image based on the $banner variable -->
<a href="<?php bloginfo('url'); ?>"><?php echo $banner; ?></a>

上記のコードからわかるように、指定されたカテゴリの投稿からコンテンツ (タイトル、コンテンツ、アイキャッチ画像) を表示します。タイトルとコンテンツは表示されますが、アイキャッチ画像 (投稿画像/サムネイル) は表示されません。たぶん、私のコードに何か問題があるので、ここでの私の主な目的である私を修正してください。上記のコードでわかるように、主な画像を取得して、主な画像がない場合はそれを表示したいだけで、デフォルトを表示するか、フォールバックの注目の画像。

4

1 に答える 1