如何在循环中获取当前 WordPress 帖子的链接?

Jus*_*tin 1 php wordpress

下面是我的循环:

<?php if (have_posts()):
    // This function belowm is responsible for iterating through the posts
    while (have_posts()): the_post(); ?>
        <div class="col-md-4">
            <h3><?php the_title(); ?></h3>
            <?php the_content(); ?>
            <?php wp_link_pages(); ?>
            <?php get_post_permalink(); ?>
            <?php edit_post_link(); ?>
        </div>
    <?php
        endwhile; ?>
    <?php
endif; ?>
Run Code Online (Sandbox Code Playgroud)

Get<?php get_post_permalink(); ?>应该显示链接,但这就是正在呈现的内容。它不显示帖子的永久链接

在此输入图像描述

dis*_*for 5

其他答案都不正确。(您也get_the_permalink()可以使用,因为它是别名)返回数据,而不是ECHO。因此,它永远不会被打印到屏幕上(大多数带前缀的 WP 函数都是这样工作的。)get_permalink()get_

您有两个选择:

  1. 使用get_permalink( get_the_ID() )传递当前帖子 ID(如果不在循环中)并回显它。
  2. 使用the_permalink()它将回显永久链接(在循环中);

the_permalink():

<?php if (have_posts()):
    // This function belowm is responsible for iterating through the posts
    while (have_posts()): the_post(); ?>
        <div class="col-md-4">
            <h3><?php the_title(); ?></h3>
            <?php the_content(); ?>
            <?php wp_link_pages(); ?>
            <?php the_permalink(); ?>
            <?php edit_post_link(); ?>
        </div>
    <?php
        endwhile; ?>
    <?php
endif; ?>
Run Code Online (Sandbox Code Playgroud)

get_permalink():

<?php if (have_posts()):
    // This function belowm is responsible for iterating through the posts
    while (have_posts()): the_post(); ?>
        <div class="col-md-4">
            <h3><?php the_title(); ?></h3>
            <?php the_content(); ?>
            <?php wp_link_pages(); ?>
            <?php echo get_permalink(); ?>
            <?php edit_post_link(); ?>
        </div>
    <?php
        endwhile; ?>
    <?php
endif; ?>
Run Code Online (Sandbox Code Playgroud)

这将回显 URL,但不会使链接可点击 - 您需要将其添加到标签中<a>

<?php if (have_posts()):
    // This function belowm is responsible for iterating through the posts
    while (have_posts()): the_post(); ?>
        <div class="col-md-4">
            <h3><?php the_title(); ?></h3>
            <?php the_content(); ?>
            <?php wp_link_pages(); ?>
            <a href="<?php the_permalink(); ?>">Click Here</a>
            <?php edit_post_link(); ?>
        </div>
    <?php
        endwhile; ?>
    <?php
endif; ?>
Run Code Online (Sandbox Code Playgroud)