1

私は現在 <?php the_post_thumbnail('post-thumbnail', array( 'class' => "search-thumb attachment-post-thumbnail")); ?>、検索結果のタイトルと抜粋を保持する div の内部を持っています。私が達成しようとしているONLYのは、投稿にサムネイルがある場合はサムネイルを追加することです。

これは、サムネイルを追加すると div の高さが大きくなるためです。サムネイルのある div の高さが 150px だとしましょう。検索結果にサムネイルがない場合は、高さをなくして div をテキストのみに合わせたいと思います。

search.php 内の HTML //

<?php get_header(); ?>

<div id="container" class="clearfix">
<div id="content-wrap">

<h1 class="heading1">Search Results</h1>
<div class="br"></div>

<div class="clear"></div>
<h2><?php echo 'Items Found ' . $wp_query->found_posts; ?></h2>
<div class="clear"></div>

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


    <div class="s-res">
    <h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
    <div class="search-thumb">
            <?php the_post_thumbnail('post-thumbnail', array( 'class'   => "search-thumb attachment-post-thumbnail")); ?>
        </div>
    <?php echo str_replace('[...]', '', get_the_excerpt()); ?>
    </div>

    <?php endwhile; ?>
    <?php else : ?>
    <i><?php _e('Sorry, but nothing matched your search criteria.<br> Please try again with different keywords.'); ?></i>
    <?php endif; ?>

</div>
</div>





<?php get_footer(); ?>

高さを表示するCSS //

.s-res {
    height: 150px;
    margin-bottom: 10px;
}

img.search-thumb {
    float: left;
    background: url(images/diag_lines.png) repeat;
    height: 100px;
    padding: 10px;
}

スクリーンショット//

スクリーンショット

4

1 に答える 1

3

if ( has_post_thumbnail() ) {...条件付きブロック内でサムネイル div をラップするだけです。関数を使用するときのコードは次のようになります。

<?php get_header(); ?>

<div id="container" class="clearfix">
<div id="content-wrap">

<h1 class="heading1">Search Results</h1>
<div class="br"></div>

<div class="clear"></div>
<h2><?php echo 'Items Found ' . $wp_query->found_posts; ?></h2>
<div class="clear"></div>

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

    <div class="s-res">
    <h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
    <?php if ( has_post_thumbnail() ): ?>
        <div class="search-thumb">
            <?php the_post_thumbnail('post-thumbnail', array( 'class'   => "search-thumb attachment-post-thumbnail")); ?>
        </div>
    <?php endif ?>
    <?php echo str_replace('[...]', '', get_the_excerpt()); ?>
    </div>

    <?php endwhile; ?>
    <?php else : ?>
    <i><?php _e('Sorry, but nothing matched your search criteria.<br> Please try again with different keywords.'); ?></i>
    <?php endif; ?>

</div>
</div>

<?php get_footer(); ?>

質問する前に、まずGoogleで簡単に検索することをお勧めします;) この関数は簡単に見つけることができます。WordPress にはかなり優れたコード ドキュメントがあるため、答えが見つかる可能性は十分にあります。あなたの質問のほとんどはそこにあります。

于 2012-11-15T12:48:27.160 に答える