Récupérer le contenu du message en dehors de la boucle
-
-
Vousne connaissezpas l'ID?Utilisez `get_queried_object_id ()`!https://developer.wordpress.org/reference/classes/wp_query/get_queried_object_id/Don't know the ID? Make use of `get_queried_object_id()` ! https://developer.wordpress.org/reference/classes/wp_query/get_queried_object_id/
- 0
- 2016-04-12
- jave.web
-
10 réponses
- votes
-
- 2012-05-10
Vouspouvez utiliser
get_page()
pour renvoyer le$post
objet d'unepage statique:$page_id = 302; $page_object = get_page( $page_id ); echo $page_object->post_content;
Modifier
Demême,vouspouvez utiliser
get_post()
pour renvoyer le$post
objet d'unmessage:$post_id = 302; $post_object = get_post( $post_id ); echo $post_object->post_content;
You can use
get_page()
to return the$post
object of a static page:$page_id = 302; $page_object = get_page( $page_id ); echo $page_object->post_content;
Edit
Similarly, you can use
get_post()
to return the$post
object of a post:$post_id = 302; $post_object = get_post( $post_id ); echo $post_object->post_content;
-
Les codes courtsfonctionnent-ilstoujours lorsque vous l'utilisez?Do shortcodes still work when using this?
- 0
- 2014-02-20
- Tim Baas
-
Je viens de découvrir quenon.Utilisez `setup_postdata ($post);` d'abord,après cela,vouspouvez utiliser `the_content ();`Just found out it doesn't. Use `setup_postdata( $post );` first, after that, you can use `the_content();`
- 0
- 2014-02-20
- Tim Baas
-
Vouspouvez simplement lancer `apply_filters ('the_content',$post_object->post_content);`You could just run `apply_filters( 'the_content', $post_object->post_content );`
- 5
- 2017-03-01
- Nathan Powell
-
- 2014-09-14
pour obtenir le contenu dumessageen dehors de laboucle,vouspouvez écrire quelque chose comme ça
global $post; $content = $post->post_content; if ( !empty( $content ) ) : echo $content; endif;
to get the content of the post outside the loop you can write something like this
global $post; $content = $post->post_content; if ( !empty( $content ) ) : echo $content; endif;
-
- 2014-03-23
Si votre contenu contient des codes courts,vous devez utiliser:
$post_id = 22; $post_object = get_post( $post_id ); echo do_shortcode( $post_object->post_content );
If your content include shortcodes, you should use:
$post_id = 22; $post_object = get_post( $post_id ); echo do_shortcode( $post_object->post_content );
-
Nefaitespas de shortcode https://kovshenin.com/2013/dont-do_shortcode/Don't do shortcode https://kovshenin.com/2013/dont-do_shortcode/
- 0
- 2015-06-24
- Brad Dalton
-
Cettepagene fournitpas une raisonimpérieuse d'éviter `do_shortcode` autre que de dire:" Celapourrait être lent "et" Vouspouvez simplement utiliser lafonction que le code court appelle. "Ces raisonsne sont valables quepour lesplustrivialespour les scénarios lesplus simples.Il échoue lorsque le shortcodeestgénéré ailleurs,ou s'il y aplusieurs shortcodes,ou shortcodesmélangés avec d'autres contenuset balisages.Sans raisonimpérieuse,je ne seraispas d'accord avec la déclaration visant à éviter `do_shortcode`.Cela sent [l'optimisationprématurée] (http://ubiquity.acm.org/article.cfm?id=1513451).That page doesn't provide a compelling reason to avoid `do_shortcode` other than saying, "It might be slow" and "You can just use the function that the short code calls." Those reasons are only valid for the most trivial for the most simple scenarios. It fails when the shortcode is generated somewhere else, or if there are multiple shortcodes, or shortcodes mixed in with other content and markup. Without a compelling reason, I would disagree with the statement to avoid `do_shortcode`. This smells like [premature optimization](http://ubiquity.acm.org/article.cfm?id=1513451).
- 1
- 2017-04-17
- Jeff
-
Et cela augmente également le couplageentre votre codeet le code du shortcode d'origine.Si laméthode de sauvegarde du shortcodeest renommée ou si la signature change,votre code se cassera.And it also increases the coupling between your code and the original shortcode's code. If the shortcode's backing method is ever renamed or the signature changes, your code will break.
- 0
- 2017-04-17
- Jeff
-
- 2014-05-23
Par souci d'exhaustivité,en s'appuyant sur le commentaire de Tim ci-dessuset inspiré de l'article de Stephen Harris ,la solution quipermet d'utiliser
the_content()
est:$post_id = 302; global $post; $post = get_post($post_id); setup_postdata( $post ); the_content(); wp_reset_postdata( $post );
Etpar conséquent,lesfiltres sont appliqués (desparagraphes serontinsérés,etc.)et les codes courtsfonctionnent.
For completeness, building on Tim's comment above and inspired by Stephen Harris's article, the solution that enables use of
the_content()
is:$post_id = 302; global $post; $post = get_post($post_id); setup_postdata( $post ); the_content(); wp_reset_postdata( $post );
And hence filters get applied (paragraphs will be inserted etc.) and shortcodes work.
-
Cela afailliplantermonnavigateur:/retirer l'esperluette aide,mais cen'esttoujourspas l'analyse des codes courts.This almost crashed my browser :/ taking out the ampersand helps, but it's still not parsing shortcodes.
- 0
- 2015-10-05
- Zade
-
- 2015-06-24
Puisque vous connaissez votre ID depublication cible (302),vouspouveztrouver utile cette syntaxe abrégée que vouspouvez utiliser hors de laboucle (bien que sesperformances soient àpeuprès lesmêmes que danstoute autreméthode alternative :)
echo(get_post_field('post_content',302));
Since you know your target post ID (302), you may find useful this shorthand syntax that you can use out of the loop (though its performance is pretty much the same as in any other alternative method:)
echo(get_post_field('post_content',302));
-
- 2016-02-08
Vouspouvez utiliser lafonction
get_post_data()
pour obtenir unmessage hors de laboucle.Placez ce code dansfunctions.phpfunction get_post_data($postId) { global $wpdb; return $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID=$postId"); }
puis ajoutez cetextraitpourplus de contrôle sur leprocessus
<?php $data = get_post_data(302); echo $data->post_date; // post date echo $data->post_title; // post title echo $data->post_content; // post content echo $data->comment_count; // comments number ?>
You can use the
get_post_data()
function to get post outside the loop. Place this code in functions.phpfunction get_post_data($postId) { global $wpdb; return $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID=$postId"); }
and then add this snippet for more control on the process
<?php $data = get_post_data(302); echo $data->post_date; // post date echo $data->post_title; // post title echo $data->post_content; // post content echo $data->comment_count; // comments number ?>
-
- 2017-03-01
Vouspouvez utiliser,comme dit,la solution avec
get_post
et$post_object->post_content
,maisn'oubliezpas d'ajouter une vérification avant d'utiliser cemessageobjet:function get_post_content( $post_id = null ) { $post_object = get_post( $post_id ); if ( ! $post_object ) { return ''; } //else return apply_filters('the_content', $post_object->post_content); } echo get_post_content( $other_post_id );
You can use, as said, the solution with
get_post
and$post_object->post_content
, but don't forget to add a check before you use that post object:function get_post_content( $post_id = null ) { $post_object = get_post( $post_id ); if ( ! $post_object ) { return ''; } //else return apply_filters('the_content', $post_object->post_content); } echo get_post_content( $other_post_id );
-
- 2016-04-20
Vouspouvez simplement appelerget_the_content (postId)
<?php echo get_the_content($postId); ?>
You can simply call get_the_content(postId)
<?php echo get_the_content($postId); ?>
-
Enfait,cen'estpaspossible.Premier argument,il y apour le contenu quandil y a un lienplus: https://codex.wordpress.org/Function_Reference/get_the_contentActually, that's not possible. First arg there is for the content when there is a more link: https://codex.wordpress.org/Function_Reference/get_the_content
- 2
- 2017-08-24
- joshcanhelp
-
- 2015-04-04
utilisez
wp_reset_postdata();
celafonctionnera .. (modifié)<?php $args = array( 'post_type' => 'posttype', 'p' => 'post_id' ); $the_query = new WP_Query( $args ); if( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <?php the_content(); ?> <?php endwhile; endif; wp_reset_postdata(); ?>
posttypepeut être "post","page" ou votretype depostpersonnalisé.Ici,p=302est votreidentifiant demessage. J'espère que celafonctionnera.
use
wp_reset_postdata();
it will work.. (edited)<?php $args = array( 'post_type' => 'posttype', 'p' => 'post_id' ); $the_query = new WP_Query( $args ); if( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <?php the_content(); ?> <?php endwhile; endif; wp_reset_postdata(); ?>
posttype can be "post" , "page" or your custom post type. Here p=302 is your post id.. Hope it will work.
-
N'utilisezjamais `query_posts` sauf si vous devezinterrompre lesfonctionnalités de lapage.Utiliseztoujours `WP_Query` ou`get_posts`pour les requêtespersonnalisées :-)Never use `query_posts` unless you need to break page functionalities. Always use `WP_Query` or `get_posts` for custom queries :-)
- 2
- 2015-04-04
- Pieter Goosen
-
oui .. vous avez raison .. Ilpeut également Wp_Query ..même résultattrouvé ..yes.. you are right.. It can Wp_Query as well.. same result found..
- 0
- 2015-04-05
- Jahirul Islam Mamun
-
Il y a aussi lefiltre `pre_get_posts`et`the_post`.Tellement de détails.There is also the `pre_get_posts` filter, and `the_post`. So much detail.
- 0
- 2017-03-01
- Nathan Powell
-
- 2012-05-10
vouspouvezmettre du contenu dans une catégorie Xet utiliser query_post avanttout comme ceci:
<?php query_posts('cat=X&showposts=1'); ?> <?php while (have_posts()) : the_post(); ?> <?= get_the_content(); ?> <?php endwhile; ?>
you can put content in a category X and use query_post before while like this :
<?php query_posts('cat=X&showposts=1'); ?> <?php while (have_posts()) : the_post(); ?> <?= get_the_content(); ?> <?php endwhile; ?>
Existe-t-il unmoyen d'obtenir le contenu d'un autreen dehors de laboucle?L'IDest 302et je doisen afficher le contenu sur une autrepage.