Sorties de shortcode en haut du_contenu
-
-
duplicationpossible de [Shortcodetoujours affichéen haut de lapage] (http://wordpress.stackexchange.com/questions/48491/shortcode-always-displaying-at-the-top-of-the-page)possible duplicate of [Shortcode always displaying at the top of the page](http://wordpress.stackexchange.com/questions/48491/shortcode-always-displaying-at-the-top-of-the-page)
- 0
- 2012-11-18
- Michael
-
4 réponses
- votes
-
- 2012-11-18
Toutes lesfonctions doivent renvoyer une chaîne,vousne devez utiliser
echo
nullepart. Réécrivez lesfonctions,utilisez une variableinternepourgérer les chaîneset renvoyez cela:// Output a single menu item function projects_menu_entry($id, $title, $link_self) { global $blog_id; $out = ''; if ($link_self || $id != $blog_id) { $out .= '<li>'; if ($id == $blog_id) { $out .= '<strong>'; } $url = get_home_url( $id, '/' ); $out .= '<a href="' . $url . '">' . $title . '</a>'; if ($id == $blog_id) { $out .= '</strong>'; } $out .= '</li>'; } return $out; } // Output the whole menu // If $link_self is false, skip the current site - used to display the menu on the homepage function projects_menu($link_self = true) { global $wpdb; $out = '<ul>'; $out .= projects_menu_entry(1, 'Home', $link_self); $blogs = $wpdb->get_results(" SELECT blog_id FROM {$wpdb->blogs} WHERE site_id = '{$wpdb->siteid}' AND spam = '0' AND deleted = '0' AND archived = '0' AND blog_id != 1 "); $sites = array(); foreach ($blogs as $blog) { $sites[$blog->blog_id] = get_blog_option($blog->blog_id, 'blogname'); } natsort($sites); foreach ($sites as $blog_id => $blog_title) { $out .= projects_menu_entry($blog_id, $blog_title, $link_self); } $out .= '</ul>'; return $out; } // Adds a [bloglist] shortcode function bloglist_shortcode($atts) { return projects_menu(false); } add_shortcode('bloglist', 'bloglist_shortcode');
Pour unexemple similaireet étendu,voir: Comment renvoyer le contenu de laboucle .
All functions have to return a string, you should not use
echo
anywhere. Rewrite the functions, use an internal variable to handle the strings and return that:// Output a single menu item function projects_menu_entry($id, $title, $link_self) { global $blog_id; $out = ''; if ($link_self || $id != $blog_id) { $out .= '<li>'; if ($id == $blog_id) { $out .= '<strong>'; } $url = get_home_url( $id, '/' ); $out .= '<a href="' . $url . '">' . $title . '</a>'; if ($id == $blog_id) { $out .= '</strong>'; } $out .= '</li>'; } return $out; } // Output the whole menu // If $link_self is false, skip the current site - used to display the menu on the homepage function projects_menu($link_self = true) { global $wpdb; $out = '<ul>'; $out .= projects_menu_entry(1, 'Home', $link_self); $blogs = $wpdb->get_results(" SELECT blog_id FROM {$wpdb->blogs} WHERE site_id = '{$wpdb->siteid}' AND spam = '0' AND deleted = '0' AND archived = '0' AND blog_id != 1 "); $sites = array(); foreach ($blogs as $blog) { $sites[$blog->blog_id] = get_blog_option($blog->blog_id, 'blogname'); } natsort($sites); foreach ($sites as $blog_id => $blog_title) { $out .= projects_menu_entry($blog_id, $blog_title, $link_self); } $out .= '</ul>'; return $out; } // Adds a [bloglist] shortcode function bloglist_shortcode($atts) { return projects_menu(false); } add_shortcode('bloglist', 'bloglist_shortcode');
For a similar, extended example see: How to return loop contents.
-
Ah,tu as raison;ça a du sens.Bontravail autour.Merci.Ah, you're right; that makes sense. Good work around. Thanks.
- 0
- 2012-11-18
- markratledge
-
- 2012-11-18
Vousne pouvezpas remplacer lesinstances de
echo
car vousne pouvez revenir qu'une seulefois.Vous devez créer une chaîneet la renvoyer.function projects_menu_entry($id, $title, $link_self) { global $blog_id; $ret = ''; if ($link_self || $id != $blog_id) { $ret .= '<li>'; if ($id == $blog_id) { $ret .= '<strong>'; } // and so on $ret .= '</ul>'; return $ret; }
Faites celapour les deuxfonctionset jem'attendrais à ce que celafonctionne.Toutesmesexcuses sij'aimal lu quelque chose.
You can't replace the instances of
echo
because you can only return once. You need to build a string and return that.function projects_menu_entry($id, $title, $link_self) { global $blog_id; $ret = ''; if ($link_self || $id != $blog_id) { $ret .= '<li>'; if ($id == $blog_id) { $ret .= '<strong>'; } // and so on $ret .= '</ul>'; return $ret; }
Do that for both functions and I would expect this to work. Apologies if I have grossly misread something.
-
Merciet vous avez raison.toscho vous abattu de deuxminutes.Thanks, and you're right. toscho beat you by two minutes.
- 0
- 2012-11-18
- markratledge
-
- 2012-11-18
Desidées?Oùest lafonction qui doit être retournée au lieu de écho?
Je vais vousproposer une solution alternative quine vous obligepas à remplacer lesinstances d'écho ou à créer une chaîne de retour.
Vouspouvez activer lamiseen mémoiretampon de sortieet renvoyer letampon sousforme de chaîne.
Ajoutez
ob_start()
au début de lafonction avanttout appel d'écho.à lafin de lafonction ajouter:
$response = ob_get_contents(); ob_end_clean(); return $response;
Any ideas? Where is the function that needs to be returned instead of echoed?
I'm going to offer an alternative solution that does not require you to replace instances of echo or build a return string.
You can turn on output buffering and return the buffer as a string.
Add
ob_start()
to the beginning of the function before any echo call.at the end of the function add:
$response = ob_get_contents(); ob_end_clean(); return $response;
-
Cetteméthodefonctionnetrèsbien;J'aifinalementessayé.Merci.This method works great; I finally got around to trying it. Thanks.
- 0
- 2013-08-29
- markratledge
-
- 2019-03-10
Réponse courteet rapide:
Vous devez
return
votre sortieet non laecho
.$output = 'first'; $output .= 'second'; //notice the dot (.=) after the first variable $output .= 'third'; return $output;
Short and quick answer:
You should
return
your output and notecho
it.$output = 'first'; $output .= 'second'; //notice the dot (.=) after the first variable $output .= 'third'; return $output;
Le shortcodeproduitpar cettefonction - une liste detous les sites d'unmultisite - sort au-dessus du contenu de laboucle,peuimporte oùilestplacé dans l'éditeur.
J'ai regardé les autres questionset réponses associées sur WPSEet jeme suis rendu compte que cela a à voir avec lafonction utilisant
echo
au lieu dereturn
,mais cen'estpas comme aussi simple que de remplacer lesinstances deecho
parreturn
dans lafonction ci-dessous. Ouen ajoutantecho=0
avec unefonction WP commewp_list_pages()
Desidées? Oùest lafonction qui doit être renvoyée au lieu d'être renvoyée?