Permalink在WordPress页面模板的If/Else语句中发布缩略图PHP?
我正在尝试超链接WordPress博客文章的缩略图,以链接到它的个人博客文章(permalink)。下面代码中的文本链接执行此操作,但图像部分位于if/else语句中 代码:Permalink在WordPress页面模板的If/Else语句中发布缩略图PHP?,php,wordpress,custom-wordpress-pages,Php,Wordpress,Custom Wordpress Pages,我正在尝试超链接WordPress博客文章的缩略图,以链接到它的个人博客文章(permalink)。下面代码中的文本链接执行此操作,但图像部分位于if/else语句中 代码: 将值存储在变量中没有错。如果你只需要permalink一次,使用or就可以了。但是,由于您需要在多个位置使用它,因此您没有将它定义为变量,而是更频繁地调用相同/类似的函数,从而增加了开销。虽然在这个规模上,这无关紧要,但在更大的规模上,它肯定会产生影响 同样,您可以实际删除,只需检查是否返回真实值 最后一点,你确定应该注
将值存储在变量中没有错。如果你只需要permalink一次,使用or就可以了。但是,由于您需要在多个位置使用它,因此您没有将它定义为变量,而是更频繁地调用相同/类似的函数,从而增加了开销。虽然在这个规模上,这无关紧要,但在更大的规模上,它肯定会产生影响
同样,您可以实际删除,只需检查是否返回真实值
最后一点,你确定应该注释掉吗
以下是我将如何使用您提供的代码来实现这一点:
<div class="carousel-inner">
<?php while( $query->have_posts() ) { $query->the_post(); $post_count++; ?>
<div class="<?= $post_count == 1 ? 'item active' : 'item'; ?>">
<?php
$permalink = get_permalink();
if( $thumbnail = get_the_post_thumbnail( null, 'slider', array( 'class' => 'img-fluid' ) ) ){
echo "<a href='$permalink'>$thumbnail</a>";
}
?>
<div class="carousel-caption">
<h6>
<a class="headline-links" href="<?= $permalink; ?>"><?php the_title() ?></a>
</h6>
<p><?= excerpt( 15 ); ?></p>
</div>
</div>
<?php } //wp_reset_postdata(); ?>
</div>
好极了!我用了第一个,效果很好。我相信第二个也可以。我并不坚持不使用变量。这是编程。我很感激你解释了他们为什么这么做。
<div class="carousel-inner">
<?php while( $query->have_posts() ) { $query->the_post(); $post_count++; ?>
<div class="<?= $post_count == 1 ? 'item active' : 'item'; ?>">
<?php
$permalink = get_permalink();
if( $thumbnail = get_the_post_thumbnail( null, 'slider', array( 'class' => 'img-fluid' ) ) ){
echo "<a href='$permalink'>$thumbnail</a>";
}
?>
<div class="carousel-caption">
<h6>
<a class="headline-links" href="<?= $permalink; ?>"><?php the_title() ?></a>
</h6>
<p><?= excerpt( 15 ); ?></p>
</div>
</div>
<?php } //wp_reset_postdata(); ?>
</div>
<div class="carousel-inner">
<?php while( $query->have_posts() ) { $query->the_post(); $post_count++; ?>
<div class="item <?php if( $post_count == 1 ) echo 'active'; ?>">
<?php
if( has_post_thumbnail() ){
echo '<a href="'. get_permalink() .'">';
the_post_thumbnail( 'slider', array( 'class' => 'img-fluid' ) );
echo '</a>';
}
?>
<div class="carousel-caption">
<h6>
<a class="headline-links" href="<?php the_permalink(); ?>"><?php the_title() ?></a>
</h6>
<p><?php echo excerpt( 15 ); ?></p>
</div>
</div>
<?php } //wp_reset_postdata(); ?>
</div>