Accueil Forums Les thèmes WordPress WooCommerce Theme Storefront

2 sujets de 1 à 2 (sur un total de 2)
  • Auteur
    Messages
  • #195211
    Alexandre
    Participant
    Points: 13 pts

    Bonjour à tous !

    Dans mon apprentissage de wordpress – woocomerce je me suis lancé le défis de concevoir un site e-commerce.

    J’ai utilisé le théme Storefront, dans la page d’accueil du site je souhaite afficher toutes les catégories de mes produits.

    Je suis donc parvenu à les affichers, voici mon code :

    
    
    <span class="pun"><?</span><span class="pln">php</span> <span class="com">/**</span> <span class="com"> * Template Name: Template Home Page</span> <span class="com"> */</span> <span class="pln">get_header</span><span class="pun">();</span> <span class="pun">?></span> <span class="tag"><div</span> <span class="atn">id</span><span class="pun">=</span><span class="atv">"primary"</span> <span class="atn">class</span><span class="pun">=</span><span class="atv">"content-area"</span><span class="tag">></span> <span class="tag"><main</span> <span class="atn">id</span><span class="pun">=</span><span class="atv">"main"</span> <span class="atn">class</span><span class="pun">=</span><span class="atv">"site-main"</span> <span class="atn">role</span><span class="pun">=</span><span class="atv">"main"</span><span class="tag">></span> <span class="pun"><?</span><span class="pln">php</span> <span class="kwd">while</span> <span class="pun">(</span><span class="pln"> have_posts</span><span class="pun">()</span> <span class="pun">)</span> <span class="pun">:</span> <span class="pln"> the_post</span><span class="pun">();</span> <span class="pln"> get_template_part</span><span class="pun">(</span> <span class="str">'content'</span><span class="pun">,</span> <span class="str">'page'</span> <span class="pun">);</span> <span class="pun">?></span> <span class="tag"><section</span> <span class="atn">aria-label</span><span class="pun">=</span><span class="atv">"Catégories de produit"</span><span class="tag">></span> <span class="tag"><h2</span> <span class="atn">class</span><span class="pun">=</span><span class="atv">"storefront-product-section-custom-title"</span><span class="tag">></span><span class="pln">Nos catégories</span> <span class="tag"><ul</span> <span class="atn">class</span><span class="pun">=</span><span class="atv">"storefront-product-section-custom"</span><span class="tag">></span> <span class="pun"><?</span><span class="pln">php</span> <span class="pln"> $args </span><span class="pun">=</span> <span class="pun">[</span> <span class="com">// Déterminer des informations des catégories des produits $args = array( 'number' => $number,</span> <span class="str">'orderby'</span> <span class="pun">=></span> <span class="str">'slug'</span><span class="pun">,</span> <span class="str">'order'</span> <span class="pun">=></span> <span class="str">'ASC'</span><span class="pun">,</span> <span class="str">'hide_empty'</span> <span class="pun">=></span><span class="pln"> $hide_empty</span><span class="pun">,</span> <span class="str">'include'</span> <span class="pun">=></span><span class="pln"> $ids</span> <span class="pun">];</span> <span class="pln"> $product_categories </span><span class="pun">=</span><span class="pln"> get_terms</span><span class="pun">(</span> <span class="str">'product_cat'</span><span class="pun">,</span><span class="pln"> $args </span><span class="pun">);</span> <span class="com">// Déterminer le nombre de catégories</span> <span class="pln"> $count </span><span class="pun">=</span><span class="pln"> count</span><span class="pun">(</span><span class="pln">$product_categories</span><span class="pun">);</span> <span class="kwd">if</span> <span class="pun">(</span><span class="pln"> $count </span><span class="pun">></span> <span class="lit">0</span> <span class="pun">){</span> <span class="com">// S'il y a au moins une catégorie </span><span class="pun">?><?</span><span class="pln">php</span> <span class="kwd">foreach</span> <span class="pun">(</span><span class="pln">$product_categories </span><span class="kwd">as</span><span class="pln"> $product_category</span><span class="pun">)</span> <span class="pun">{</span> <span class="kwd">if</span><span class="pun">(</span><span class="pln">$product_category</span><span class="pun">-></span><span class="pln">slug </span><span class="pun">!=</span> <span class="str">"non-classe"</span><span class="pun">)</span> <span class="pun">{</span> <span class="com">// Déterminer l'identifiant de la vignette d'une catégorie</span> <span class="pln"> $thumbnail_id </span><span class="pun">=</span><span class="pln"> get_woocommerce_term_meta</span><span class="pun">(</span><span class="pln"> $product_category</span><span class="pun">-></span><span class="pln">term_id</span><span class="pun">,</span> <span class="str">'thumbnail_id'</span><span class="pun">,</span> <span class="kwd">true</span> <span class="pun">);</span> <span class="com">// Extraire l'URL de la vignette</span> <span class="pln"> $thumbnail_url </span><span class="pun">=</span><span class="pln"> wp_get_attachment_thumb_url</span><span class="pun">(</span><span class="pln"> $thumbnail_id </span><span class="pun">);</span> <span class="com">// Aficher la vignette et le nom de la catégorie comme un lien vers la page de la catégorie</span> <span class="pln"> echo </span><span class="str">'</span> <span class="str"> <span class="pun">.</span><span class="pln"> get_term_link</span><span class="pun">(</span><span class="pln"> $product_category </span><span class="pun">)</span> <span class="pun">.</span> <span class="str">'"> </span></span> <span class="str"> <span class="pun">.</span><span class="pln">$thumbnail_url</span><span class="pun">.</span><span class="str">'" width="200px" height="90px" /></span></span> <span class="str"> '</span> <span class="pun">.</span><span class="pln"> $product_category</span><span class="pun">-></span><span class="pln">name </span><span class="pun">.</span> <span class="str">'</span> <span class="str"> '</span><span class="pun">;</span> <span class="pun">}</span> <span class="pun">}</span> <span class="pun">}</span> <span class="pun">?></span> <span class="pun"><?</span><span class="pln">php</span> <span class="pln"> endwhile</span><span class="pun">;</span> <span class="com">// End of the loop.</span> <span class="pun">?></span> <span class="com"><!-- #main --></span>
    <span class="com"><!-- #primary --></span> <span class="pun"><?</span><span class="pln">php</span> <span class="pln">get_footer</span><span class="pun">();</span>

    J’ai aussi vu qu’il exister la fonction wp_list_categories() !!! mais je comprend pas comment la metre en pratique.

    Merci par avance de votre aide.

    Bonjour nouveau sur wordpress je me forme.

    • Ce sujet a été modifié le il y a 7 mois et 2 semaines par Alexandre.
    • Ce sujet a été modifié le il y a 7 mois et 2 semaines par Alexandre.
    • Ce sujet a été modifié le il y a 7 mois et 2 semaines par Alexandre.
    #195269
    Guy
    Participant
    Points: 4,773 pts
    ModérateurCertifiéFreelance
    Membre 2 étoiles
2 sujets de 1 à 2 (sur un total de 2)
  • Vous devez être connecté pour répondre à ce sujet.