简体   繁体   English

永久链接在WordPress页面模板的If / Else语句中发布缩略图PHP?

[英]Permalink post thumbnail PHP inside If/Else statement for WordPress Page template?

I'm trying to hyperlink the WordPress blog post thumbnail image to link to its individual blog post (permalink). 我正在尝试超链接WordPress博客文章缩略图以链接到其单个博客文章(永久链接)。 The text link in the code below does it, but the image part is inside an if/else statement. 下面代码中的文本链接可以实现,但是图像部分位于if / else语句中。

Code: 码:

<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>

There's nothing wrong with storing values in variables. 将值存储在变量中没有任何问题。 If you only need the permalink once, using echo get_permalink(); 如果只需要一次永久链接,请使用echo get_permalink(); or the_permalink(); the_permalink(); would be just fine. 很好。 However, since you need it in more than one place, you're increasing overhead by not defining it as a variable and instead calling the same/similar functions more often than necessary. 但是,由于您需要多个位置,因此您没有将其定义为变量,而是不必要地更频繁地调用相同/相似的函数,从而增加了开销。 While on this scale it won't matter much, on a larger scale it can definitely make an impact. 尽管规模如此之大无关紧要,但在更大范围内肯定会产生影响。

In that same vein, you can actually remove the has_post_thumbnail() and just check to see if get_the_post_thumbnail() returns a truthy value. 同样,您实际上可以删除has_post_thumbnail()并仅检查get_the_post_thumbnail()返回真实值。

One last note, are you sure the wp_reset_postdata(); 最后一点,您确定wp_reset_postdata(); should be commented out? 应该被注释掉吗?

Here's how I would approach this with the code you've provided: 这是我将使用您提供的代码来实现的方法:

<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>

However, if you're adamant about not using a variable (which you shouldn't be!) then you can use this: 但是,如果您坚持不使用变量(不应该这样做),则可以使用以下方法:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM