2012-02-16 8 views
1

投稿にサムネイルが含まれているかどうか、どうすれば確認できますか?他に何もしないなら。これは私が持っているものです:WordPressでサムネイルを確認するにはどうしたらいいですか?

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

       <?php if (has_post_thumbnail()) { ?> 
         <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> 
       <?php 
       }else{ 
       ?> 
        <?php the_post_thumbnail(); ?> 
       <?php 
       } 
       ?> 

      <?php endwhile; ?> 

     <?php endif; ?> 

助けていただければ幸いです。そのないそこにある場合

+0

どうしたのですか? –

+0

私はそれを実行するときに何も表示されません。私はthe_thumbnailのような別のものを試しましたが、まだ何も表示されていません – jorame

+0

あなたはそれがループに入っていると確信していますか? – KDM

答えて

0

まず第二に、これができますあなたのfunctions.php にこれを追加...あなたのファイルにそれをコピーして貼り付け、この

if (function_exists('add_theme_support')) { 
    add_theme_support('post-thumbnails'); 
} 

ため

をあなたのfunctions.phpファイルをチェック次に、あなたのテンプレートページ上のような何かにあなたのコードを変更する

function get_the_post_thumbnail_url($post_id = NULL) { 
    global $id; 
    $post_id = (NULL === $post_id) ? $id : $post_id; 
    $src = wp_get_attachment_image_src(get_post_thumbnail_id($post_id), 'full'); 
    $src = $src[0]; 
    return $src; 
} 

画像srcを返し、ちょうど全体のimgタグを印刷しない

これは

は、あなたが完全なimgタグのコードはいずれかを使用するだけで、印刷したい場合は、背景画像

<?php if (has_post_thumbnail()) { ?> 
    <div id="slider" style="background-image:url(<?php echo get_the_post_thumbnail_url($post->ID, 'large'); ?>); background-position: center center;"> 
    </div>     
<?php 
}else{ 
?> 
    <img src="<?php bloginfo('template_directory');?>/images/blank.jpg" alt="" /> 
<?php 
} 
?> 

これはそれに適用される背景イメージを持つdiv要素を生成しなければならないとしました次の

if (has_post_thumbnail()) { 
?> 
    <?php the_post_thumbnail();   // just the image  ?> 
    <?php the_post_thumbnail('thumbnail'); // just the thumbnail ?> 
    <?php the_post_thumbnail('medium'); // just the Medium Image ?> 
    <?php the_post_thumbnail('large');  // just the Medium Image ?> 
    <?php 
    // adding a 200x200 height and width along with a class to it. 
     the_post_thumbnail(array(200,200), array('class' => 'alignleft')); 
    ?> 
    <?php 
    // Adding a few classes to the medium image 
     the_post_thumbnail('medium', array('class' => 'alignleft another_class')); 
    ?> 

<?php 
} 

マーティ..

+0

これは正しいループ内に入りますか? – jorame

+0

はい。どこでも.. – Marty

4

は、あなたはすでに、問題は、あなたが他の文で間違ったコードを置くことで、ライン

if (has_post_thumbnail()) 

ポストサムネイルを持っている場合は、チェックしている中で、これを持っています

<?php if (has_post_thumbnail()) { ?> 
     <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> 
     <?php the_post_thumbnail(); ?> 
     HAVE THUMBNAIL DO SOMETHING 
    <?php 
     }else{ 
    ?> 
     DOESN'T HAVE THUMBNAIL : DO SOMETHING ELSE 
     <?php 
    } 
    ?> 
1

次のコード行を試してみてください:

<?php if(has_post_thumbnail()) 
     { 
     ?> 
      <img src="<?php the_post_thumbnail_url(); ?>" id="contextual" class="contextual" alt="" /> 

     <?php 
     } 
else{  
     ?> 
     <img src="<?php echo get_template_directory_uri(); ?>/design/images/i-default.jpg" id="contextual" class="contextual" alt="" /> 
<?php } ?> 
関連する問題