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; ?>
Get<?php get_post_permalink(); ?>
应该显示链接,但这就是正在呈现的内容。它没有显示帖子的永久链接
解决方案
其他答案都不正确。get_the_permalink()
(您也可以使用get_permalink()
,因为它是别名)返回数据,而不是ECHO。因此,它永远不会打印到屏幕上(大多数带有get_
前缀的 WP 功能都是这样工作的。)
你有两个选择:
- 使用
get_permalink( get_the_ID() )
传递当前的帖子 ID(如果不在循环中)并回显它。 - 使用
the_permalink()
which 将回显永久链接(在循环中);
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; ?>
这将回显 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; ?>
推荐阅读
- ios - 如何向标签添加约束,使其始终与动态的 UITextView 相距 0 点?
- php - 使用文件名查找和替换多个 base64 图像
- c# - .NET Core 2.x 加载引用的程序集,在代码中没有类型引用
- c# - 无法使用 DataContract Name 属性将 JSON POST req 反序列化为 WCF
- ionic-framework - 在离子应用程序中,我想使用 InAppBrowser 从服务器打开所有链接
- json - 如何在没有密钥的情况下解析 JSON
- python - 使用 python 脚本运行程序时打印 shell 的时间命令结果
- facebook-messenger - 用户对帖子发表评论后如何向新用户发送消息
- c# - SignalR CORS 同源策略?
- visual-studio-code - 对 VSCode 的 Vim 或类似 Emacs 的模式行魔术支持