Requête de modèle de page avec WP_Query
5 réponses
- votes
-
- 2011-10-01
Essayez ceci ... En supposant que lenom dumodèleest "my_template.php",
$query = new WP_Query( array( 'post_type' => 'page', 'meta_key' => '_wp_page_template', 'meta_value' => 'my_template.php' ) ); //Down goes the loop...
Vouspouvez également utiliser get_posts ,oumodifier interrogez lesmessages pourfaire letravail.Ces deuxfonctions utilisent lesmêmesparamètres que WP_Query .
Try this... Assuming the template name is 'my_template.php',
$query = new WP_Query( array( 'post_type' => 'page', 'meta_key' => '_wp_page_template', 'meta_value' => 'my_template.php' ) ); //Down goes the loop...
You can also use get_posts, or modify query posts to get the job done. Both these functions use the same parameters as WP_Query.
-
- 2012-05-18
Incorrect: àpartir de wordpress 3,vous avezbesoin de quelque chose qui ressemble à:
$args = array( 'post_type' => 'page', 'meta_query' => array( array( 'key' => '_wp_page_template', 'value' => 'my_template.php' ) ) );
Incorrect: as of wordpress 3 you need something akin to:
$args = array( 'post_type' => 'page', 'meta_query' => array( array( 'key' => '_wp_page_template', 'value' => 'my_template.php' ) ) );
-
JE VOUS REMERCIE!!J'aiparcouru cettepagetrop rapidement,utilisé la réponse acceptée.Cela lefait.Pourn'importe qui d'autre,notez letableau à l'intérieur dutableau à l'intérieur dutableau ...THANK YOU!! Skimmed this page way too quickly, used the accepted answer. This does it. For anyone else, note the array inside the array inside the array...
- 0
- 2016-04-26
- Jeremy Carlson
-
La seule différenceiciest le `post_type`.Sinon,vousn'avezpasbesoin dutableau `meta_query`pour une seulepaire clé/valeurpersonnalisée.The only difference here is the `post_type`. Otherwise you don't need the `meta_query` array for a single custom key/value pair.
- 3
- 2016-11-16
- Rutwick Gangurde
-
Bien sûr,laméta-requêteestnécessaire.Sauf que celapourrait être "en ligne" avec `meta_key`et`meta_value` ou avec untableau simple,quipourraitinclureplusieurs conditions.Of course the meta query is needed. Except it could be "inline" with `meta_key` and `meta_value` or with a plain array, which could include multiple conditions.
- 0
- 2020-02-14
- Maxime Culea
-
- 2011-10-01
Lemodèle depageest stockéen tant que valeurméta avec la clé "_wp_page_template".
Il vous suffit donc d'utiliser cette clé dans unparamètre deméta-requête.Pour desexemples
Voir
http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query#Query_based_on_Custom_Field_and_Sorted_by_Value et http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
The page template is stored as a meta value with key "_wp_page_template".
So all you need is to use that key in a meta query parameter. For examples
and http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
-
- 2019-09-09
Si vous avez lemodèle dans un autre dossier:
$args = array( 'post_type' => 'page', //it is a Page right? 'post_status' => 'publish', 'meta_query' => array( array( 'key' => '_wp_page_template', 'value' => 'page-templates/template-name.php', // folder + template name as stored in the dB ) ) );
If you have the template inside another folder:
$args = array( 'post_type' => 'page', //it is a Page right? 'post_status' => 'publish', 'meta_query' => array( array( 'key' => '_wp_page_template', 'value' => 'page-templates/template-name.php', // folder + template name as stored in the dB ) ) );
-
- 2018-09-04
Si latentative de quelqu'un aboutit à zéropublication,lenom dumodèleestprobablementerroné.J'aiessayé lenom defichierphpet lenom demonmodèleet ilsn'ontpasfonctionné.Ensuite,j'ai décidé d'inspecter laboîte de sélection desmodèles dans laquellenous sélectionnons lemodèle dans l'éditeur depage.J'aitrouvé ceci:
<option value="templates-map/component-tutorial-1.php" selected="selected">Tutorial -1</option>
J'ai utilisé
templates-map/component-tutorial-1.php
et cela afonctionné.If anyone's attempt incorrectly results in zero posts, probably the template name is wrong. I tried the php file name and my template name and they didn't work. Then I decided to inspect the templates select box where we select the template on the page editor. I found this:
<option value="templates-map/component-tutorial-1.php" selected="selected">Tutorial -1</option>
I used
templates-map/component-tutorial-1.php
and it worked.
Je voudraisinterroger uniquement lespages avec un certainmodèle depage avec
WP_Query
ou unefonction qui renverrait l'objet depublication,maisje netrouve aucuneinformation à ce sujet sur le codex officiel./p>