WordPress查询slug的单个帖子 [英] WordPress query single post by slug

查看:59
本文介绍了WordPress查询slug的单个帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我想不使用循环显示单个帖子的那一刻,我使用以下方法:

For the moment when I want to show a single post without using a loop I use this:

<?php
$post_id = 54;
$queried_post = get_post($post_id);
echo $queried_post->post_title; ?>

问题是,当我移动网站时,id通常会更改. 有没有办法通过slug查询此帖子?

The problem is that when I move the site, the id's usually change. Is there a way to query this post by slug?

推荐答案

来自WordPress Codex:

From the WordPress Codex:

<?php
$the_slug = 'my_slug';
$args = array(
  'name'        => $the_slug,
  'post_type'   => 'post',
  'post_status' => 'publish',
  'numberposts' => 1
);
$my_posts = get_posts($args);
if( $my_posts ) :
  echo 'ID on the first post found ' . $my_posts[0]->ID;
endif;
?>

WordPress Codex获取帖子

这篇关于WordPress查询slug的单个帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆