Obtenir le contenu de la page en utilisant le slug
4 réponses
- votes
-
- 2012-10-06
Utilisez
get_posts()
et leparamètrename
quiest la limace:$page = get_posts( array( 'name' => 'your-slug' ) ); if ( $page ) { echo $page[0]->post_content; }
Sachez que letype depublication dans
get_posts()
estpar défaut'post'
.Si vous voulez une page ,utilisez…$page = get_posts( array( 'name' => 'your-slug', 'post_type' => 'page' ) );
Si vous voulez tous lestypes depublicationspubliques (à l'exception despiècesjointes),définissez l'argument dutype depublication sur
'any'
.Ensuite,vouspourriez obtenirplus d'un résultat car les slugsne sontpas uniques dans différentstypes depublications.Use
get_posts()
and the parametername
which is the slug:$page = get_posts( array( 'name' => 'your-slug' ) ); if ( $page ) { echo $page[0]->post_content; }
Be aware that the post type in
get_posts()
defaults to'post'
. If you want a page use …$page = get_posts( array( 'name' => 'your-slug', 'post_type' => 'page' ) );
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. -
- 2012-10-06
Si sur lapage avec le slugen question
En savoirplus sur les balises conditionnelles :
is_page()
prend également le slug comme argument.Par conséquent,
if( is_page( 'your-slug' ) ) { // fetch content }
fera ce que vous voulez.
Si sur une autrepage
Si vous souhaitez savoir comment récupérer le contenu d'un article/d'unepageen fonction d'un slug lorsque pas sur laditepage,vouspouvez alimenter
get_posts
également un slug.Cecin'estpas documenté dans le codex.Ce qui suit va récupérer l'identifiant d'un slug:
$args = array( 'name' => 'your-slug' ); $posts_from_slug = get_posts( $args ); // echo fetched content echo $posts_from_slug[0]->post_content;
If on the page with the slug in question
Read up on conditional tags:
is_page()
also takes the slug as an argument.Hence,
if( is_page( 'your-slug' ) ) { // fetch content }
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:
$args = array( 'name' => 'your-slug' ); $posts_from_slug = get_posts( $args ); // echo fetched content echo $posts_from_slug[0]->post_content;
-
- 2012-10-06
Vouspouvez obtenir unepagepar sontitreen utilisant
get_page_by_title()
fonction.Vouspouvez l'utiliser comme ceci (en supposant que vous souhaitiez afficher le contenu):
$page = get_page_by_title('Your Title'); $content = apply_filters('the_content', $page->post_content); echo $content;
BTW,pour obtenir lapageen utilisant slug:
function get_page_id_by_slug($slug){ global $wpdb; $id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$slug."'AND post_type = 'page'"); return $id; } $page = get_post(get_page_id_by_slug('my-slug'));
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):
$page = get_page_by_title('Your Title'); $content = apply_filters('the_content', $page->post_content); echo $content;
BTW, to Get page using slug:
function get_page_id_by_slug($slug){ global $wpdb; $id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$slug."'AND post_type = 'page'"); return $id; } $page = get_post(get_page_id_by_slug('my-slug'));
-
- 2015-10-21
Utilisez
get_page_by_path
.<?php get_page_by_path( $page_path, $output, $post_type ); ?>
<₹Example:
//Returns the animal with the slug 'cat' get_page_by_path('cat', OBJECT, 'animal');
pourplus d'informations,consultez la Référence desfonctions WordPress
J'utilise ce code lors du remplissage d'unmodèle dethème àpartir d'unepage,
$about = get_page_by_path('about'); $content = apply_filters( 'the_content', $about->post_content ); echo $content;
Use
get_page_by_path
.Syntax
<?php get_page_by_path( $page_path, $output, $post_type ); ?>
Example:
//Returns the animal with the slug 'cat' get_page_by_path('cat', OBJECT, 'animal');
for more refernce see WordPress Function Reference
I use this code when populating a theme template from a page,
$about = get_page_by_path('about'); $content = apply_filters( 'the_content', $about->post_content ); echo $content;
J'essaie d'obtenir le contenu de lapage quandje ne connais que la chaîne de slug.
Existe-t-il unefonctionpour cela,ou unmoyen simple de lefaire ou s'agit-il de lefaire via SQL?
Mercibeaucoup