Php 如何插入此条件语句,以便在博客文章没有特征图像时显示默认图像?
当一篇博客文章没有特色图片时,我试图使用以下条件语句显示默认图片Php 如何插入此条件语句,以便在博客文章没有特征图像时显示默认图像?,php,wordpress,Php,Wordpress,当一篇博客文章没有特色图片时,我试图使用以下条件语句显示默认图片isa.jpg: <?php /* Previous Thumbnail */ if ( $prevthumbnail ) { echo $prevthumbnail; } else { echo '<img src="'.get_stylesheet_directory_uri()
isa.jpg
:
<?php
/* Previous Thumbnail */
if ( $prevthumbnail ) {
echo $prevthumbnail;
} else {
echo '<img src="'.get_stylesheet_directory_uri().'/images/isa.jpg" alt="ISA" />';
}
/* Next Thumbnail */
if ( $nextthumbnail ) {
echo $nextthumbnail;
} else {
echo '<img src="'.get_stylesheet_directory_uri().'/images/isa.jpg" alt="ISA" />';
}
?>
下面的代码显示了特色图片,但是如果没有特色图片与博客文章相关联,它只显示一个空白区域。这就是为什么我想在它的位置显示一个默认图像
<div id="next-prev-posts">
<?php $prevPost = get_previous_post(); $prevthumbnail = get_the_post_thumbnail($prevPost->ID, array(100,100) ); ?>
<?php $nextPost = get_next_post(); $nextthumbnail = get_the_post_thumbnail($nextPost->ID, array(100,100) ); ?>
<div class="inner">
<div class="nav-next">
<?php if ($nextPost) { ?>
<?php next_post_link('%link',"$nextthumbnail", false); ?>
<?php next_post_link('%link', '<span class="meta-nav">«</span> %title', false) ?>
<?php } else { echo "<p>You're at the beginning!</p>"; } ?>
</div>
<div class="nav-previous">
<?php if ($prevPost) { ?>
<?php previous_post_link('%link', '%title <span class="meta-nav">»</span>', false) ?>
<?php previous_post_link('%link',"$prevthumbnail", false); ?>
<?php } else { echo "<p>You're at the end!</p>"; } ?>
</div>
</div>
</div>
也许有一种更优雅的方式来写这篇文章,但是试试这个:
<?php
/* Check Thumbnail */
if ( !$prevthumbnail && !$nextthumbnail) {
echo '<img src="'.get_stylesheet_directory_uri().'/images/isa.jpg" alt="ISA";
echo '<img src="'.get_stylesheet_directory_uri().'/images/isa.jpg" alt="ISA";
}
elseif {
/* Previous Thumbnail */
if ( $prevthumbnail ) {
echo $prevthumbnail;
}
else {
echo '<img src="'.get_stylesheet_directory_uri().'/images/isa.jpg" alt="ISA" />';
}
/* Next Thumbnail */
if ( $nextthumbnail ) {
echo $nextthumbnail;
}
else {
echo '<img src="'.get_stylesheet_directory_uri().'/images/isa.jpg" alt="ISA" />';
}
}
?>
让我知道。
基本上我把$prevthumbnail和$nextthumbnail放在支票上。只有在检查其中至少有一个存在之后,它才会进入第二个条件级别,检查哪一个存在。我总是喜欢挑战。这就是我想到的。它可能有点长,但我必须修复代码中的bug。希望这有帮助
<div id="next-prev-posts">
<?php
$prevPost = get_previous_post();
if(isset($prevPost->ID))
$prevthumbnail = get_the_post_thumbnail($prevPost->ID, array(100,100) );
$nextPost = get_next_post();
if(isset($nextPost->ID))
$nextthumbnail = get_the_post_thumbnail($nextPost->ID, array(100,100) );
$default = '<img src="'.get_stylesheet_directory_uri().'/images/isa.jpg" alt="ISA" />';
?>
<div class="inner">
<div class="nav-next">
<?php
if ($nextPost && '' != get_the_post_thumbnail($nextPost->ID, array(100,100))) {
next_post_link('%link', $nextthumbnail, false);
next_post_link('%link', '<span class="meta-nav">«</span> %title', false);
} elseif ($nextPost) {
next_post_link('%link', $default, false);
next_post_link('%link', '<span class="meta-nav">«</span> %title', false);
} else {
print '<p>' . __( 'You are at the beginning!' ) . '</p>';
}
?>
</div>
<div class="nav-previous">
<?php
if ($prevPost && '' != get_the_post_thumbnail($prevPost->ID, array(100,100))) {
previous_post_link('%link', $prevthumbnail, false);
previous_post_link('%link', '<span class="meta-nav">«</span> %title', false);
} elseif ($prevPost) {
previous_post_link('%link', $default, false);
previous_post_link('%link', '<span class="meta-nav">«</span> %title', false);
} else {
print '<p>' . __( 'You are at the end!' ) . '</p>';
}
?>
</div>
</div>
</div>