将帖子永久链接转换为真实的帖子id,用于现场和舞台网站



我在一个网格中,需要一些帮助。

有三个演员进化了。

  1. 现场/制作网站(wordpress)
  2. 本地网站/舞台/测试网站(wordpress)
  3. 第三方分析

背景:我知道这很糟糕,但事实是制作网站和本地网站的内容没有同步。即生产中的post-id 1可以作为本地中的post-id 24。

问题定义:我被指派使用第三方的API来获取点击量最大的热门文章列表,并将其显示在我们的网站上。

$result = file_get_contents($url);
$result_json = json_decode($result, true);

以上几句话对我来说很容易。

错误:由于第三方没有帖子摘录,他们将URL、image_link、hit_count、帖子标题和作者信息作为JSON发送。因此,在我的网站上,我能够很好地展示所有这些领域。但问题开始于我的经理将文章摘录添加到列表中。我尝试将URL转换为postid,并使用获得帖子摘录

get_the_excerpt(url_to_postid($top_posts['link']))

它适用于现场直播。但在舞台上和地方上都不起作用。WellpostURL有它的域名。即使我用我的本地域或后台域替换了域名。它没有显示任何东西。对于一些帖子,当它显示摘录时,它不是来自同一篇文章。伙计们需要一些想法。

有没有什么方法可以让我从URL获取鼻涕虫?我尝试使用explode()函数。但有时子弹并不是数组中的最后一个项目。

谢谢你通读。我很感激你的帮助。

现在我提出了一个解决方案。主要使用URL获取帖子ID,如果没有找到,则使用帖子标题获取帖子ID。

public static function get_excerpt ( $id = null ) {
$post = get_post($id);
if ( empty($post) ) {
return '';
}
if ( strlen($post->post_excerpt) ) {
// Use the excerpt
$excerpt = $post->post_excerpt;
$excerpt = apply_filters('the_excerpt', $excerpt);
} else {
// Make excerpt
$content = $post->post_content;
$content = strip_shortcodes($content);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
$excerpt_length = apply_filters('excerpt_length', 55);
$excerpt_more = apply_filters('excerpt_more', ' ' . '[…]');
$excerpt = wp_trim_words($content, $excerpt_length, $excerpt_more);
$excerpt = wpautop($excerpt);
}
return apply_filters('wp_trim_excerpt', $excerpt);
}

调用此函数以获取摘录。

$postid     = url_to_postid($top_posts['link']); 
$postid  = $postid!=0 ? $postid :  get_page_by_title($post_title, OBJECT, 'post')->ID; 
$excerpt    = self::get_excerpt($postid);

这对我来说是有效的,因为我并没有相同标题的帖子。

我仍在等待我的问题得到更好的解决。

最新更新