Shortcode toujours affiché en haut de la page
-
-
Shortcodepourinclure unfichier?Jetrouve ça unpeubizarre.Shortcode for including a file? I find it a little odd.
- 0
- 2012-04-10
- Rutwick Gangurde
-
'avecmon codeje n'utilisepasecho' - lesfichiers * loop-module.php *font-ils écho ou retournent-ils leur sortie?'with my code I am not using echo' - are the *loop-module.php* files echoing or returning their output?
- 1
- 2012-04-10
- Michael
-
Paspour lemoment - lefichier ajuste unebalise deparagraphe avec "test" écrit dedans afin queje puisse voir comment celafonctionne.Not at the moment - the file just has a paragraph tag with "test" written into it so I can see how it works.
- 0
- 2012-04-10
- john joe
-
utilisezplutôt `get_template_part ('loop',$module)` à laplace,ilestplusjoli,plus rapide àtaper,prenden charge lesthèmesparents/enfants,et c'est laméthode recommandéepourinclure desmodèles dans wordpress (oui,Locate_templatepeutfaire certaines de ces choses,mais regardez combienmoins lisible!)use `get_template_part('loop',$module)` instead, it's prettier, quicker to type, supports parent/child themes, and it's the recommended method of including templates in wordpress ( yes locate_template may do some of those things but look how much less readable it is!)
- 0
- 2012-04-11
- Tom J Nowell
-
duplicationpossible de [Le résultat d'un shortcode apparaît AVANT le contenu de lapage] (http://wordpress.stackexchange.com/questions/45378/the-result-of-a-shortcode-appear-before-page-content)possible duplicate of [The result of a shortcode appear BEFORE page content](http://wordpress.stackexchange.com/questions/45378/the-result-of-a-shortcode-appear-before-page-content)
- 0
- 2012-04-11
- Chip Bennett
-
Je l'aiessayé à l'origine avecget_template_partmais,simamémoireestbonne,je n'aipaspu luipasserma variable de catégorie.I originally tried it with get_template_part but, if my memory serves me correctly, I couldn't pass my category variable to it.
- 0
- 2012-04-11
- john joe
-
2 réponses
- votes
-
- 2012-04-10
Vouspouveztamponner la sortie comme ceci:
ob_start(); include(locate_template('loop-'.$module.'.php')); return ob_get_clean();
MODIFIER.J'aiessayé ceci,j'aibien fonctionné.
function friendly_loop_shortcode( $atts, $content = null ) { extract( shortcode_atts( array( 'category' => '', 'module' => '' ), $atts ) ); ob_start(); include(locate_template('loop-'.$module.'.php')); $output = ob_get_clean(); //print $output; // debug return $output; } if (!is_admin()) { add_shortcode('test', 'friendly_loop_shortcode' ); }
You can buffer the output like this:
ob_start(); include(locate_template('loop-'.$module.'.php')); return ob_get_clean();
EDIT. I tried this, worked fine.
function friendly_loop_shortcode( $atts, $content = null ) { extract( shortcode_atts( array( 'category' => '', 'module' => '' ), $atts ) ); ob_start(); include(locate_template('loop-'.$module.'.php')); $output = ob_get_clean(); //print $output; // debug return $output; } if (!is_admin()) { add_shortcode('test', 'friendly_loop_shortcode' ); }
-
[N'utilisezpas lamiseen mémoiretampon de sortie] (http://wordpress.stackexchange.com/a/41014/73) s'ilexiste d'autresmoyens.[Do not use output buffering](http://wordpress.stackexchange.com/a/41014/73) if there are other ways.
- 4
- 2012-04-10
- fuxia
-
J'aibien peur que celan'aitpasfonctionné.I'm afraid that didn't work.
- 0
- 2012-04-11
- john joe
-
Qu'est ce quine s'estpasbien passé?J'aiessayéet mis àjour la réponsewhat went wrong? I tried and updated the answer
- 0
- 2012-04-11
- offroff
-
- 2012-04-10
Votrefichierinclusfaitessentiellement écho au HTML.Parexemple
<?php //Some PHP echo 'test'; //Some more PHP ?>
Est lemême que
<?php //Some PHP ?> test <?php //Some more PHP ?>
Les deux sontimprimésimmédiatementplutôt que renvoyés.Depuis sonimprimé,il apparaît avant le contenu de lapage.Vous devez retourner quelque chosepour qu'il soitinclus dans le contenu.
Comme l'a souligné @RutwickGangurde -ilestinhabituel d'inclure unfichiermodèle dans un shortcode.
Your included file is essentially echo-ing HTML. For instance
<?php //Some PHP echo 'test'; //Some more PHP ?>
Is the same as
<?php //Some PHP ?> test <?php //Some more PHP ?>
Both get printed immediately rather than returned. Since its printed it appears before the page content. You need to return something for it be included in the content.
As @RutwickGangurde pointed out - it's unusual to include a template file in a shortcode.
-
Merci,je ne savaispas ça.J'ai doncbesoin demettre letout dans une variableet de le renvoyer?En ce qui concerne l'inclusion d'unfichier demodèle -ehbien,je veux que les utilisateurs duthèmepuissentpersonnaliser lethème aussifacilement quepossibleet avoir un énormefichier de code courtimbriqué à l'écart du répertoire duthème racinen'estpas vraimentidéalpoureux.Thanks, I didn't know that. So I need to put the whole thing into a variable and return it? As for including a template file - well I want to make it as easy as possible for the theme users to customise the theme and having a huge shortcode file nested away from the root theme dir is not really ideal for them.
- 1
- 2012-04-10
- john joe
-
Voir la réponse de @ offroff - cela devraitfonctionnerSee @offroff's answer - that should work
- 0
- 2012-04-10
- Stephen Harris
-
"*ehbien,je veux rendre lapersonnalisation duthème aussi simple quepossiblepour les utilisateurs duthème *" - Combien de couches d'abstraction sontnécessaires?Vous avez unfichier demodèlepersonnalisé à l'intérieur d'un shortcode.Jene voispas comment celafacilite les chosespour les utilisateurs.Si vous voulez offrir lapossibilité d'ajouter du contenu statique àtous les articles,définissez simplement une `dynamic_sidebar ()` dans laboucleet laissez les utilisateurs ajouter des widgets."*well I want to make it as easy as possible for the theme users to customise the theme*" - Just how many abstraction layers are necessary? You've got a custom template file inside of a shortcode. I don't see how that makes things any easier for users. If you want to provide the ability to add static content to all posts, just define a `dynamic_sidebar()` inside the Loop, and let users add Widgets.
- 1
- 2012-04-11
- Chip Bennett
-
Laplupart des utilisateurs dethèmesn'apprécierontpas de devoirparcourir unfichier de shortcodemassifpour changer une simple classe css sur lefrontend.Sije décomposetouten utilisant le système standard loop-name.php,alorsilestbeaucoupplus rapidepoureux detrouver ce dontils ontbesoin.Pasidéalmaisje dois lesgarder heureux;)Most of the theme users will not appreciate having to trawl through a massive shortcode file to change a simple css class on the frontend. If I break up everything using the standard loop-name.php system then it's a lot faster for them to find what they need. Not ideal but I've got to keep them happy ;)
- 0
- 2012-04-11
- john joe
-
Trèsbien expliqué;Very well explained;
- 0
- 2018-01-20
- Diego Somar
-
C'est labonne réponse,car la cause duproblème était un «écho»indirect du HTML,au lieu de construire le HTML dans une variableet de le «renvoyer» à lafin de lafonction.Dans la documentation WordPress,vouspouveztrouver cet avertissement: *** N'oubliezpas d'utiliser returnet nonecho -tout ce quiest renvoyé sera affiché dans lenavigateur,maisiln'apparaîtrapas aubonendroit sur lapage. ***This is the correct answer, because the cause of the problem was an indirect "echo" of the HTML, instead of building the HTML in a variable and "return" it at the end of function. In WordPress documentation, you can find this warning: *** Remember to use return and not echo - anything that is echoed will be output to the browser, but it won't appear in the correct place on the page.***
- 0
- 2020-07-01
- aldemarcalazans
J'utilise un shortcodepourextraire différentesboucles via le loops-name.php.Pour une raison quelconque,ilesttoujoursen haut de lapage.Je l'aigoogléet utiliserecho au lieu de returnprovoque ceproblèmemais avecmon code,je n'utilisepasecho.Voici le shortcode:
Uneidée depourquoi cela seproduit?