I’m trying to get page content when I only know the slug string.
Is there a function for this, or an easy way to do this or is this a case of doing it via SQL?
Thanks very much
I’m trying to get page content when I only know the slug string.
Is there a function for this, or an easy way to do this or is this a case of doing it via SQL?
Thanks very much
You must be logged in to post a comment.
Use
get_posts()
and the parametername
which is the slug:Be aware that the post type in
get_posts()
defaults to'post'
. If you want a page use â¦If you want all public post types (except attachments) set the post type argument to
'any'
. Then you could get more than one result because slugs are not unique across different post types.You can get a page by its title using
get_page_by_title()
function.You can use it like this (assuming you want to show the content):
BTW, to Get page using slug:
If on the page with the slug in question
Read up on conditional tags:
is_page()
also takes the slug as an argument.Hence,
will do what you want.
If on another page
Should you be interested on how to fetch post/page content based on a slug when not on said page, you can feed
get_posts
a slug as well. This is not documented in the codex.The following will fetch the id from a slug:
Use
get_page_by_path
to retrieve the WP_Post object, then you can extract the post content and filter for HTML output if necessary.Syntax
Example:
I use this code when populating a theme template from a page, to ensure the content is rendered as HTML:
To get a post of a different type, pass in the other two parameters:
for more reference see WordPress Function Reference