永久链接 post 缩略图 PHP 在 WordPress 页面模板的 If/Else 语句中?
Permalink post thumbnail PHP inside If/Else statement for WordPress Page template?
我正在尝试将 WordPress 博客 post 缩略图 link 超级link 到其个人博客 post (permalink)。下面代码中的文本 link 执行此操作,但图像部分在 if/else 语句中。
代码:
<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() ) {
//Permalink needed below
the_post_thumbnail( 'slider', array( 'class' => 'img-fluid' ) );
}
?>
<div class="carousel-caption">
<h6><a class="headline-links" href="<?php echo get_permalink(); ?>"><?php the_title() ?></a></h6>
<p><?php echo excerpt( 15 ); ?></p>
</div>
</div>
<?php } //wp_reset_postdata(); ?>
</div>
在变量中存储值没有错。如果你只需要一次永久链接,使用 echo get_permalink();
or the_permalink();
就可以了。但是,由于您在多个地方需要它,因此您没有将它定义为变量而是比必要更频繁地调用 same/similar 函数,从而增加了开销。虽然在这个规模上没什么大不了的,但在更大的规模上它肯定会产生影响。
以同样的方式,您实际上可以删除 has_post_thumbnail()
and just check to see if get_the_post_thumbnail()
returns 真值。
最后一点,你确定 wp_reset_postdata();
应该被注释掉吗?
以下是我将如何使用您提供的代码处理此问题:
<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>
我正在尝试将 WordPress 博客 post 缩略图 link 超级link 到其个人博客 post (permalink)。下面代码中的文本 link 执行此操作,但图像部分在 if/else 语句中。
代码:
<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() ) {
//Permalink needed below
the_post_thumbnail( 'slider', array( 'class' => 'img-fluid' ) );
}
?>
<div class="carousel-caption">
<h6><a class="headline-links" href="<?php echo get_permalink(); ?>"><?php the_title() ?></a></h6>
<p><?php echo excerpt( 15 ); ?></p>
</div>
</div>
<?php } //wp_reset_postdata(); ?>
</div>
在变量中存储值没有错。如果你只需要一次永久链接,使用 echo get_permalink();
or the_permalink();
就可以了。但是,由于您在多个地方需要它,因此您没有将它定义为变量而是比必要更频繁地调用 same/similar 函数,从而增加了开销。虽然在这个规模上没什么大不了的,但在更大的规模上它肯定会产生影响。
以同样的方式,您实际上可以删除 has_post_thumbnail()
and just check to see if get_the_post_thumbnail()
returns 真值。
最后一点,你确定 wp_reset_postdata();
应该被注释掉吗?
以下是我将如何使用您提供的代码处理此问题:
<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>