Comment créer un shortcode pour afficher une publication personnalisée dans une page ou une publication standard?
2 réponses
- votes
-
- 2011-02-18
Ilexiste d'excellents didacticiels sur les codes courtspartout sur le Webet debonsexemples ici
maisjustepour commencer:
add_shortcode('film_q', 'film_shortcode_query'); function film_shortcode_query($atts, $content){ extract(shortcode_atts(array( // a few default values 'posts_per_page' => '1', 'post_type' => 'film', 'caller_get_posts' => 1) , $atts)); global $post; $posts = new WP_Query($atts); $output = ''; if ($posts->have_posts()) while ($posts->have_posts()): $posts->the_post(); $out = '<div class="film_box"> <h4>Film Name: <a href="'.get_permalink().'" title="' . get_the_title() . '">'.get_the_title() .'</a></h4> <p class="Film_desc">'.get_the_content().'</p>'; // add here more... $out .='</div>'; /* these arguments will be available from inside $content get_permalink() get_the_content() get_the_category_list(', ') get_the_title() and custom fields get_post_meta($post->ID, 'field_name', true); */ endwhile; else return; // no posts found wp_reset_query(); return html_entity_decode($out); }
et pour l'utiliser,entrez dansn'importe quel article/page:
[film_q p=FILM_POST_ID]
remplacez simplement FILM_POST_IDpar l'identifiant réel de lapublication dufilm.
J'espère que cela vous aidera
There are great tutorials about shortcodes all over the web and some good examples here
but just to get you started:
add_shortcode('film_q', 'film_shortcode_query'); function film_shortcode_query($atts, $content){ extract(shortcode_atts(array( // a few default values 'posts_per_page' => '1', 'post_type' => 'film', 'caller_get_posts' => 1) , $atts)); global $post; $posts = new WP_Query($atts); $output = ''; if ($posts->have_posts()) while ($posts->have_posts()): $posts->the_post(); $out = '<div class="film_box"> <h4>Film Name: <a href="'.get_permalink().'" title="' . get_the_title() . '">'.get_the_title() .'</a></h4> <p class="Film_desc">'.get_the_content().'</p>'; // add here more... $out .='</div>'; /* these arguments will be available from inside $content get_permalink() get_the_content() get_the_category_list(', ') get_the_title() and custom fields get_post_meta($post->ID, 'field_name', true); */ endwhile; else return; // no posts found wp_reset_query(); return html_entity_decode($out); }
and to use it enter in any post/page:
[film_q p=FILM_POST_ID]
just change FILM_POST_ID to the actual Film post ID.
Hope this helps
-
Bonjour ... Le shortcoden'extraitpas correctementpost_type=film,pour une raison quelconque?Sije fais un var_dump de `$posts`,les valeurspar défautfournies dans l'extraitne sontpas dutout utilisées?Dois-je renvoyer autre chose que `$ atts` à utiliser dans`new WP_Query () `?Hi there... The shortcode is not extracting post_type=film correctly, for some reason? If I do a var_dump of `$posts`, those default values provided in the extract are not being used at all? Should I be returning something other than `$atts` to use in the `new WP_Query()`?
- 0
- 2011-02-19
- gillespieza
-
Je suppose que le var_dump de $postn'estpas dans lafonction shortcode,maintenant sij'ai raison,vous appelez var_dump dans votreboucle dethème,c'est ainsi que vousne voyezpas les arguments que vous recherchez.I' guessing that the var_dump of $post is not in the shortcode function, now if i am right then you are calling var_dump in you theme loop the that is way you are not seeing the arguments you were looking for.
- 0
- 2011-02-19
- Bainternet
-
Ah,j'ai ajouté ceci après l'extrait: `$film_query='posts_per_page='. $ Posts_per_page. '& Post_type='. $ Post_type. '& P='. $ Id;`et utilisé `$film_query` au lieu de` $ atts`dans la requête.Merci :)Ah, I added this after the extract: `$film_query = 'posts_per_page='.$posts_per_page.'&post_type='.$post_type.'&p='.$id;` and used `$film_query` instead of `$atts` in the query. Thanks :)
- 0
- 2011-02-19
- gillespieza
-
où voyez-vous "$film_query=...."?where do you see "$film_query = ...." ?
- 0
- 2011-02-19
- Bainternet
-
Et simonfilm «$ content» contient des shortcodes?Commentpuis-je analyser cela correctement?Dois-je créer unfiltre sur le contenu renvoyé?What if my film `$content` contains shortcodes? How do I get that to parse correctly? Should I create a filter the content returned?
- 0
- 2011-02-19
- gillespieza
-
J'ai construit `$film_query` à utiliser dans` $posts=new WP_Query ($film_query); `parce que` $ atts`ne me donnait que `p=FILM_POST_ID`et pas le reste des valeurspar défaut ...I constructed `$film_query` to use in `$posts = new WP_Query($film_query);` because `$atts` was only giving me `p=FILM_POST_ID` and not the rest of the defaults...
- 0
- 2011-02-19
- gillespieza
-
Oui,get_the_content contourne lefiltre_contentYea you do, get_the_content bypass the_content filter
- 0
- 2011-02-19
- Bainternet
-
Ok,donc sij'utilise `return (apply_filters ('the_content',$ out));` au lieu de `return html_entity_decode ($ out);` cela analysebien :)Ok, so if I use `return (apply_filters ('the_content', $out) );` instead of `return html_entity_decode($out);` that parses nicely :)
- 0
- 2011-02-19
- gillespieza
-
- 2011-02-18
Essayez de commencer àpartir de cetutoriel .
Dans lafonction de rappel,effectuez une requêtepersonnalisée (ou utilisez get_post )pour l'articleet extrayez uniquement les valeurs qui vousintéressent.(C'est-à-dire:titre,extrait ...)
exemple de shorcode
[film id=10]
Utilisezid dans votrefonctionpour récupérer le contenu dufilm.
Try to start from this tutorial.
In the callback function, do a custom_query (or use get_post) for the post and extract only the values that are relevant for you. (I.e: title, excerpt...)
example shorcode
[film id=10]
Use id in your function to retrive the content of the film.
Je voudrais créer un shortcode quiextraira lesinformations d'un articlepersonnaliséet l'affichera dans unepage ou un articlenormal.
Cas d'utilisation spécifique:j'ai untype demessagepersonnalisé "Film"pour un site Web defestival defilm.Lesfilms sont affichés avec leurpropre single-film.php,maisparfois lespropriétaires du site veulent écrire un article ou unepage quimentionne unfilmparticulier,et aimeraientpouvoirextraire desextraits desinformations qui ont déjà été saisies (parexemple,Nom dufilm,informations de réservation,etc.).Celairait dans une "boîte" aubas de l'article,et j'aimerais leurfaciliter latâcheen leurfournissant une sorte de shortcode.
Commentpourrais-jeprocéder?Des ressources/tutoriels recommandéspourme mettre sur labonne voie?De quelspièges dois-je être conscient (parexemple,plusieursboucles dans unmessage)?