简体   繁体   中英

How do I get the link of a current WordPress post while it is in the Loop?

Below is my loop:

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

Get <?php get_post_permalink(); ?> <?php get_post_permalink(); ?> should display the link yet this is what is being rendered. It is not displaying the permalink of the post

在此处输入图片说明

None of the other answers are correct. get_the_permalink() (you can use get_permalink() as well, since it's an alias) RETURNS the data, not ECHO . So, it will never be printed to the screen (most WP functions with get_ prefix work this way.)

You have two options:

  1. Use get_permalink( get_the_ID() ) pass the current post id (if not in the loop) and echo it.
  2. Use the_permalink() which will echo out the permalink (in the loop);

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

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

This will echo out the URL, but will not make the link clickable - you need to add it to an <a> tag:

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

If you want to get Post Permalink you should use get_permalink($post_id)

Wordpress get_permalink function Reference

Please try this:

<?php if (have_posts()):
    // This function belowm is responsible for iterating through the posts
    while (have_posts()): the_post(); 
    $id = get_the_ID();
?>
        <div class="col-md-4">
            <h3><?php the_title(); ?></h3>
            <?php the_content(); ?>
            <?php wp_link_pages(); ?>
            <?php get_the_permalink($id); ?>
            <?php edit_post_link(); ?>
        </div>
    <?php
        endwhile; ?>
    <?php
endif; ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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