2013-07-12 83 views
0

我有HTML的這部分用PHP代碼從一個變量插入一個變量的變量的wordpress內

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

,你可以從上面的代碼看,它顯示內容(標題,內容,特色圖像)來自指定類別的帖子。顯示標題和內容,但不顯示特色圖像(張貼圖像/縮略圖)。可能是,我的代碼有問題,所以請糾正我,我的主要目標在這裏,正如你可以看到上面的代碼,我只想得到特色圖像,並顯示它,否則如果沒有特色的圖像,然後顯示默認或後備特色圖片。

+0

你接着說:add_theme_support( '後縮略圖');''在functions.php' W/O任何掛鉤? – SachinGutte

+0

你的'精選圖片'文件夾與這個源文件有關嗎? – SASM

+0

問題解決了!無論如何,謝謝你的合作:) –

回答

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%;' />";