【问题标题】:simple Wordpress If/Else简单的 Wordpress If/Else
【发布时间】:2012-08-05 05:39:45
【问题描述】:

我有两种不同的方法来显示 Wordpress 缩略图

我想做的是,显示第一种方法,如果它不可用,则使用第二种方法显示拇指。

以下是显示帖子缩略图的两种方法。

方法一

<!--Begin WordPress Featured post thumbnail-->
<div class="postthumbcon">
<?php
// check if the post has a featured image assigned to it.
if ( has_post_thumbnail() ) {
// get the src of the large size featured image
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large' );
$thumbnailSrc = $src[0];
// output image resized with timthumb
?>
<div class="postthumb">
">
<img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo $thumbnailSrc; ?>&h=125&w=165" alt="">

</div>
<?php } ?>
</div>
<!--end WordPress Featured post thumbnail--> 

这是第二种方法。

<!--Begin Timthumb thumbnail-->
<?php // This will show the image and link the image to the post. Alter the width and height (in both places) to your needs. ?>

<?php if ( get_post_meta($post->ID, 'thumb', true) ) { ?>
<div class="postthumb">
" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo get_post_meta($post->ID, "thumb", $single = true); ?>&h=150&w=150&zc=1" alt="<?php the_title(); ?>" width="150" height="150" />
</div>
<?php } ?>

<!--End Timthumb thumbnail-->

【问题讨论】:

    标签: wordpress thumbnails if-statement featured


    【解决方案1】:

    试试这样的:

    <div class="postthumbcon">
    <?php
    if ( has_post_thumbnail() ) {
        // method one
    } else if ( get_post_meta($post->ID, 'thumb', true) ) {
        // Method two
    } 
    ?>
    </div>
    

    这将查看帖子是否有您已经在做的缩略图,但如果它不存在,它将运行else 子句,这意味着第二个 if 语句只有在第一个失败时才会运行。

    另外,我猜你的第一部分似乎是方法二中的一个链接,请参见这一行:

    " rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo get_post_meta($post->ID, "thumb", $single = true); ?>&h=150&w=150&zc=1" alt="<?php the_title(); ?>" width="150" height="150" />
    

    以下是您的代码的完整外观:

    <div class="postthumbcon">
        <?php
        if ( has_post_thumbnail() ) {
            $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large' );
            $thumbnailSrc = $src[0];
        ?>
            <div class="postthumb">
                <img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo $thumbnailSrc; ?>&h=125&w=165" alt="" />
            </div>
        <?php } else if ( get_post_meta($post->ID, 'thumb', true) ) { ?>
            <div class="postthumb">
                <a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
                    <img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo get_post_meta($post->ID, "thumb", $single = true); ?>&h=150&w=150&zc=1" alt="<?php the_title(); ?>" width="150" height="150" />
                </a>
            </div>
        <?php } ?>
    </div>
    

    【讨论】:

    • 没问题,你应该接受答案,这样其他人才能看到它已经解决了:)
    • 谢谢,非常感谢。
    猜你喜欢
    • 2021-03-07
    • 2012-03-31
    • 1970-01-01
    • 2015-04-19
    • 2015-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多