简体   繁体   English

在现有的PHP标记内添加the_permalink

[英]Adding the_permalink inside existing PHP tags

I have this piece of code inside a Wordpress loop that checks if the field 'quote' is present. 我在Wordpress循环中有这段代码,该循环检查是否存在“ quote”字段。 If it's not here it will display the link but if it is there it wont. 如果不在这里,它将显示链接,但是如果没有,则不会显示。

<?php 
  if(get_field('quote') == ''){
  $yourTag = "<a href="the_permalink();">" ;
  } else {
  $yourTag = "";
  }
?>

and then I use this to output the link: 然后我用它来输出链接:

<div> <?php echo $yourTag; ?> </div>

The problem I'm having is with the 3rd line: 我遇到的问题是第三行:

$yourTag = "<a href="the_permalink();">" ;

All this does is add the_permalink() onto the end of the URL and doesn't output the actual link. 所有这些操作只是将the_permalink()添加到URL的末尾,并且不输出实际的链接。

Is this code is correct? 此代码正确吗?

For reference, the full loop code is here: 作为参考,完整的循环代码在这里:

<!-- Main Loop =========================================== -->


<div class="container blog-card-container">
    <div class="card-columns">


        <?php if ( have_posts() ) : ?>

            <?php /* Start the Loop */ ?>

            <?php while ( have_posts() ) : the_post(); ?>




                <!-- Conditional a  =========================================== -->

                <?php 
                if(get_field('quote') == ''){
                $yourTag = "<a href="the_permalink();">" ;
                } else {
                $yourTag = "";
                }
                ?>





                <div> <?php echo $yourTag; ?> </div>




                    <div class="card">





                        <!-- Image if loop =========================================== -->

                        <?php if ( in_category('14') ) : ?>


                            <div class="client-header-logo-card" style="background-color: <?php the_field('client_brand_colour'); ?>;">
                                <?php 

                                $image = get_field('client_logo');

                                if( !empty($image) ): ?>

                                    <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />

                                <?php endif; ?>
                            </div>              

                        <?php else: ?>

                            <div class="blog-thumb-container">
                                <?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } ?>
                            </div>                          

                        <?php endif ?>




                        <!-- Meta Data if loop =========================================== -->

                        <div class="blog-clients-card-block">

                            <?php if ( in_category('14') ) : ?>

                                <div class="client-text-block">

                                    <p class="blog-cat-label"><?php the_category(', '); ?></p>

                                    <h2><?php the_title(); ?></h2>

                                    <?php if( get_field('quote') ): ?><p class="client-quote"><span style="color:<?php the_field('client_brand_colour'); ?>; font-weight:bold;">&ldquo; </span><?php the_field('quote'); ?><span style="color:<?php the_field('client_brand_colour'); ?>;font-weight:bold;"> &rdquo;</span></p><?php endif; ?>


                                    <?php if( get_field('quote_name') ): ?><p class="client-name" style="color:<?php the_field('client_brand_colour'); ?>;"><?php the_field('quote_name'); ?></p><?php endif; ?>
                                    <?php if( get_field('quote_position') ): ?><p class="client-position" style="color:<?php the_field('client_brand_colour'); ?>;"><?php the_field('quote_position'); ?></p><?php endif; ?>


                                    <?php if( get_field('button_text') ): ?>
                                        <a class="btn btn-sm btn-client-archive" href="<?php the_permalink(); ?>" style="background-color:<?php the_field('client_brand_colour'); ?>;" role="button"><?php the_field('button_text'); ?></a>
                                    <?php endif; ?>

                                    <?php if( get_field('video_url') ): ?>
                                        <div class="embed-container">
                                            <?php the_field('video_url'); ?>
                                        </div>
                                    <?php endif; ?>       

                                </div>

                            <?php else: ?>

                                <p class="blog-cat-label"><?php the_category(', '); ?></p>
                                <h2 class="blog-card-title"><?php the_title(); ?></h2>
                                <p class="card-text"><?php the_excerpt(__('(more…)')); ?></p>
                                <p><strong><?php the_author(); ?></strong> | <?php the_date(); ?> </p>

                            <?php endif ?>


                        </div>





            </a>





        </div>



        <?php endwhile; wp_reset_postdata(); endif; ?>



    </div>

</div>

You can do it with dot (.) ie concatenation operator like below. 您可以使用点(.)连接运算符,如下所示。

 <?php 
      if(get_field('quote') == ''){
      $yourTag = "<a href='".get_the_permalink()."'>" ;
      } else {
      $yourTag = "";
      }
    ?>

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

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