0

我有这部分 html 和 php 代码

<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>

从上面的代码可以看出,它显示了来自指定类别的帖子的内容(标题、内容、特色图片)。显示标题和内容,但不显示特色图片(发布图片/thumbmail)。可能是,我的代码有问题,所以请纠正我,我的主要目标,正如您在上面的代码中看到的那样,我只想获取特色图像并显示它,如果没有特色图像则显示默认值或后备特色图片。

4

1 回答 1

1

问题已解决,谢谢合作!

我的解决方案是...

如果有特色图片然后..

$banner = "<img class='extend' src='".$image[0]."' style='width: 100%;' />";

如果没有找到特色图片,则设置默认或后备

$link = get_template_directory_uri();;
$banner = "<img class='extend' src='$link/images/banner.jpg' style='width: 100%;' />";

否则,如果指定类别上没有帖子,则设置默认或后备

对于内容

<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>

对于图像

$link = get_template_directory_uri();;
    $banner = "<img class='extend' src='$link/images/banner.jpg' style='width: 100%;' />";
于 2013-07-12T19:34:55.300 回答