所以我希望获得一篇新发布的帖子的永久链接标题。 例如“http://myblog.com/healthy-life.php”我需要健康生活.php
这是我的代码,但它让我http://myblog.com/healthy-life.php
function get_laterst_post_url() {
global $wpdb;
$query = "SELECT ID FROM {$wpdb->prefix}posts WHERE post_type='post' AND post_status='publish' ORDER BY post_date DESC LIMIT 1;";
$result = $wpdb->get_results($query);
if(is_object($result[0])) {
return get_permalink($result[0]->ID);
} else {
return '';
};
}
答案 0 :(得分:1)
您可以添加:
$url = get_bloginfo('wpurl') . "/";
$permalink_title = str_replace($url, "", get_permalink($result[0]->ID));
return $permalink_title;
但它总是最好使用wordpress函数来获取链接。说:
function get_laterst_post_url() {
$args = array(
'numberposts' => 1,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true );
$recent_posts = wp_get_recent_posts();
$url = get_bloginfo('wpurl') . "/";
$permalink_title = str_replace($url, "", get_permalink($recent_posts[0]["ID"]));
return $permalink_title;
}
答案 1 :(得分:1)
使用PHP's explode()拆分斜杠并抓住数组的最后一个块。
$segments = explode("/", get_permalink($result[0]->ID));
$permalink = array_pop($segments);
return $permalink;