php - 包装WordPress最新帖子链接
问题描述
我正在尝试用指向最新帖子的链接包装一个 div。目前,代码返回特色图像的值以用作连续 div 的背景图像。我只想用最新帖子的 url 包装它。谢谢
<?php
/* Get Recent Post */
$recent_post = wp_get_recent_posts(array(
'numberposts' => 1,
'post_status' => 'publish'
));
/* If Featured Image Set */
if ( has_post_thumbnail($recent_post[0]['ID']) ){
/* Get Image */
$image = wp_get_attachment_image_src( get_post_thumbnail_id($recent_post[0]['ID']), 'full');
/* Output Div with Image Set Inline, Use padding Top for Responsive Ratio Size */
echo '
<div class="featured-image-div" style="background-image:url('.$image[0].');"></div>
';
}
解决方案
get_the_permalink
像这样使用
/* If Featured Image Set */
if ( has_post_thumbnail($recent_post[0]['ID']) ){
$image = wp_get_attachment_image_src( get_post_thumbnail_id($recent_post[0]['ID']), 'full');
echo '<a href="' . get_the_permalink($recent_post[0]['ID']) . '">
<div class="featured-image-div" style="background-image:url('.$image[0].');"></div></a>
';
}
函数文档:https ://developer.wordpress.org/reference/functions/get_the_permalink/
推荐阅读
- php - 错误“invalid_grant / 无效的 JWT 签名”。使用 Oauth 获取访问令牌
- ruby-on-rails - 如何修复错误:无法生成 PDF
- c# - 如何在 C# 应用程序中使用 Abbyy Fine Reader Engine 进行 OCR 处理
- mongodb - Pymongo 使用数组索引查找查询失败,因为键只能是字符串
- python-3.x - 寻找最接近目标的数字组合
- python - 如何使以下python代码更有效地寻找友好对
- git - Docker 构建特定的本地 git 分支
- msbuild - 命令行 MSBuild 工作 - 远程构建产生错误
- javascript - 在 onInit 中设置 ngForm 的值
- jsf - 在运行时将 ManagedBean 添加到 JSF 上下文