Wordpress后备图像

时间:2015-06-09 10:16:24

标签: wordpress wordpress-theming thumbnails image-generation

我从之前的开发人员中选择了一个项目,他正在使用此代码生成缩略图。我需要添加一个默认的后备图片,而且我不确定最佳做法。

<?php get_header(); ?>
<?php get_sidebar(); ?>
        <div id="container">
        <?php if(have_posts()) : ?>
        <?php while(have_posts()) : the_post(); ?>
        <div class="post" id="post-<?php the_ID(); ?>">
            <div class="entry">
                <?php the_content(); ?>
            </div>
            <?php $categories = get_categories(array('orderby'=>'order','number'=>'13'));
            $wud = wp_upload_dir();
            $width = get_option('thumbnail_size_w');
            $height = get_option('thumbnail_size_h');
            $exclude_cats = array(1, 27); // Array of category ID's to exlude from this page
            ?>
            <div id="gallery">
                <ul class="cat-list">
                <?php foreach ($categories as $cat) {
                    if (!in_array($cat->cat_ID, $exclude_cats)) : ?>
                    <li class="cat-item cat-item-<?php echo $cat->cat_ID; ?>">
                        <a href="<?php echo get_category_link( $cat->term_id ); ?>" title="View all artists under the <?php echo $cat->cat_name; ?> category">
                            <div class="cat-ss" style="position: relative; width: <?php echo $width; ?>px; height: <?php echo $height; ?>px; overflow: hidden;">
                            <?php $artists = get_posts('category=' .  $cat->cat_ID . '&orderby=rand');
                            foreach ($artists as $artist) {
                            $name = get_the_title( $artist->ID );
                            $thumb = $wud['baseurl'] . '/thumb-' . sanitize_title( $name ) . '-' . $width . 'x' . $height . '.jpg'; ?>

                                <img src="<?php echo $thumb; ?>" alt="<?php echo $name; ?>" title="<?php echo $name; ?>" width="<?php echo $width; ?>" class="cat-ss-image" height="<?php echo $height; ?>" style="position: absolute; top: 0; left: 0;" />
                            <?php } ?>
                                <div class="cat-ss-caption png" style="position: absolute; bottom: 0;"><h3><?php echo $cat->cat_name; ?></h3></div>

                            </div>
                        </a>
                    </li>
                <?php endif;
                } ?>
                <ul>
            </div>
            <div style="clear: both"></div>
        </div>
        <?php endwhile; ?>
        <?php else : ?>
        <div class="post">
            <h2><?php _e('Not Found'); ?></h2>
        </div>
        <?php endif; ?>
        </div>
        </div>
<?php get_footer(); ?>

2 个答案:

答案 0 :(得分:0)

无论您的代码是什么,都应该查看has_post_thumbnail,然后使用get_the_post_thumbnail输出缩略图或后备图片。

在我的许多网站上都像魅力一样。您可以在我链接到的法典页面中找到示例。

答案 1 :(得分:0)

看起来像拉他的代码是

<?php $thumb = $wud['baseurl'] . '/thumb-' . sanitize_title( $name ) . '-' . $width . 'x' . $height . '.jpg'; ?>

所以要使它成为条件if / else语句,如果它不为空呈现图像缩略图,并使用后备如果它是空的,执行此操作(添加您需要的任何其他代码以呈现图像):

<?php if(!empty $thumb) { ?>
<img src="<?php echo $thumb; ?>" alt="<?php echo $name; ?>" title="<?php echo $name; ?>" width="<?php echo $width; ?>" class="cat-ss-image" height="<?php echo $height; ?>" style="position: absolute; top: 0; left: 0;" />
<?php } else { ?>
<img src="yourfallback.jpg" style="position: absolute; top: 0; left: 0;">
<?php } ?>