Meilleure pratique pour implémenter des sections personnalisées dans un thème WordPress
-
-
La réponse de @toscho,bien que superbe,peutne pas répondre à ce que vous demandez.La réponse detoschoestparfaite si vos utilisateurs sont des développeurset doiventpouvoirmanipuler la section d'unepage d'accueil,parexempleen utilisant desthèmesenfants.J'ai lu votre question différemment,comme "Commentpuis-je rendre les sections d'unepage d'accueilmodifiables *par les utilisateurs * (quin'accèdentpas au code)".Pouvez-vous clarifier cela?Et si c'est comme ça queje le lis,queltype d'informations cherchez-vous à ajouter dans ces sectionsen vedette,juste des articles (ou des listes demessages)?@toscho's answer, while superb, may not actually address what you're asking. toscho's answer is perfect if your users are developers, and need to be able to manipulate the section of a homepage, for instance using child themes. I read your question differently, as "how can I make sections of a homepage editable *by users* (who aren't accessing the code)." Can you clarify this? And if it's how I read it, what type of information are you looking to add in these featured sections, just posts (or lists of posts)?
- 2
- 2013-05-24
- Matthew Boynes
-
1 réponses
- votes
-
- 2013-05-19
Leparadigme debase dans WordPressest The Callback Handler. Actions,filtres,widgets,métaboxeset ainsi de suite…tout s'exécuteen enregistrant desgestionnaires de rappel spécifiquespour certains déclencheurs. Cen’estpastoujours lamanière laplus élégante,mais celle quetout débutant doit apprendre,alorstenez-vousen à ceparadigme,chaquefois que vousne savezpas quoifaire.
Proposez donc quatre actions:
do_action( 'home_primary_custom' ); do_action( 'home_secondary_custom_1' ); do_action( 'home_secondary_custom_2' ); do_action( 'home_secondary_custom_3' );
Ensuite,vous ou un développeurtierspouvezenregistrer un rappelpour ces actions avec
add_action()
.Celapourrait être amélioré: utilisez unpréfixepour éviter les collisions avec lesplugins ou lesmodifications du cœur de WordPress,et parcourez untableaupourgarder le code compactet lisible.
$prefix = get_stylesheet(); $custom_actions = array ( 'home_primary_custom', 'home_secondary_custom_1', 'home_secondary_custom_2', 'home_secondary_custom_3' ); foreach ( $custom_actions as $custom_action ) do_action( "$prefix_$custom_action" );
Maintenant,celapeut déjà êtretrop longpour unmodèle simple,vouspouvez doncencapsuler le code dans unefonctionpersonnaliséeet l'enregistrerpour une autre actionpersonnalisée:
// front-page.php do_action( get_stylesheet() . '_custom_front_actions' ); // functions.php add_action( get_stylesheet() . '_custom_front_actions', 'custom_front_actions' ); /** * Execute custom front actions and print a container if there are any callbacks registered. * * @wp-hook get_stylesheet() . '_custom_front_actions' * @return bool */ function custom_front_actions() { $has_callbacks = FALSE; $prefix = get_stylesheet(); $custom_actions = array ( 'home_primary_custom', 'home_secondary_custom_1', 'home_secondary_custom_2', 'home_secondary_custom_3' ); // Are there any registered callbacks? foreach ( $custom_actions as $custom_action ) { if ( has_action( "$prefix_$custom_action" ) ) { $has_callbacks = TRUE; break; } } // No callbacks registered. if ( ! $has_callbacks ) return FALSE; print '<div class="' . esc_attr( "$prefix-custom-front-box" ) . '">'; foreach ( $custom_actions as $custom_action ) do_action( "$prefix_$custom_action" ); print '</div>'; return TRUE; }
Vouspouvezmaintenantimprimer un conteneurpersonnalisé uniquement s'il y a des rappelsenregistrés. Les développeurstierspeuventenregistrer leurspropres rappels ou supprimer les vôtres. Votre
front-page.php
n'abesoin que d'une ligne de code supplémentaire.The base paradigm in WordPress is The Callback Handler. Actions, filters, widgets, metaboxes and so on … everything runs by registering specific callback handlers for some triggers. This is not always the most elegant way, but the one every beginner must learn, so stick to that paradigm, whenever you’re not sure what to do.
So offer four actions:
do_action( 'home_primary_custom' ); do_action( 'home_secondary_custom_1' ); do_action( 'home_secondary_custom_2' ); do_action( 'home_secondary_custom_3' );
Then you or a third party developer can register a callback for these actions with
add_action()
.This could be improved: Use a prefix to prevent collisions with plugins or WordPress core changes, and run through an array to keep the code compact and readable.
$prefix = get_stylesheet(); $custom_actions = array ( 'home_primary_custom', 'home_secondary_custom_1', 'home_secondary_custom_2', 'home_secondary_custom_3' ); foreach ( $custom_actions as $custom_action ) do_action( "$prefix_$custom_action" );
Now, this might already be too long for a plain template, so you could encapsulate the code in a custom function and register that for another custom action:
// front-page.php do_action( get_stylesheet() . '_custom_front_actions' ); // functions.php add_action( get_stylesheet() . '_custom_front_actions', 'custom_front_actions' ); /** * Execute custom front actions and print a container if there are any callbacks registered. * * @wp-hook get_stylesheet() . '_custom_front_actions' * @return bool */ function custom_front_actions() { $has_callbacks = FALSE; $prefix = get_stylesheet(); $custom_actions = array ( 'home_primary_custom', 'home_secondary_custom_1', 'home_secondary_custom_2', 'home_secondary_custom_3' ); // Are there any registered callbacks? foreach ( $custom_actions as $custom_action ) { if ( has_action( "$prefix_$custom_action" ) ) { $has_callbacks = TRUE; break; } } // No callbacks registered. if ( ! $has_callbacks ) return FALSE; print '<div class="' . esc_attr( "$prefix-custom-front-box" ) . '">'; foreach ( $custom_actions as $custom_action ) do_action( "$prefix_$custom_action" ); print '</div>'; return TRUE; }
Now you can print a custom container only if there are any callbacks registered. Third party developers can register their own callbacks or remove yours. Your
front-page.php
needs just one additional line of code.
J'ai dumal àtrouver lameilleurefaçon d'implémenter des sectionspersonnalisées dans unthème WordPress.Parexemple,je crée unthème qui aura quatre sectionsen vedette sur lapage d'accueil.Ces sections seront dans leurpropre zone,distincte du contenu de lapage.
Je sais que celapourrait êtrefaiten ajoutant quatrepositions de widget,ou uneposition de widgetet en obligeant l'utilisateur à ajouter quatre widgets.Vouspouvez également ajouter les quatre zones dans lepanneau d'options duthème,oumêmeessayer d'utiliser unplugin de création depage.Jeme demandais si quelqu'un avait des conseils sur lameilleureméthodeet pourquoi?Ou des suggestions sur d'autresméthodes?
Merci,
David