2016-05-08 13 views
1

Ich habe ein PHP/Wp-Skript geschrieben, die in der Theorie ein div mit einer Kategorie Name, Beschreibung und alle seine Produkte innerhalb drucken sollte. Der Code sieht wie folgt aus:WooCommerce: Produkte-of-Kategorie Schleife bizarre Probleme

<?php 

$args2 = array('taxonomy' => 'product_cat', 'parent' => 9); 
$sub_cats = get_categories($args2); 

foreach($sub_cats as $sub_category) { ?> 

    <div class="treatments-description col-md-9" id="<?php echo $sub_category->term_id;?>"> 

     <h2 class="section-heading"> 
      <span class="line-behind-text"><?php echo $sub_category->name;?></span> 
     </h2> 

     <p class="section-text"> 
      OPIS: <?php echo category_description(); ?> //this part does not work too, not sure why 
     </p> 

     <h3 class="section-heading"><p class="line-behind-text">Dostępne zabiegi</p></h3> 

     <table class="treatments-table table products"> 

      <tr class="table-heading"> 
       <th class="name" id="<?php echo $sub_category->term_id;?>">Usługa</th> 
       <th>Czas trwania</th> 
       <th>Cena</th> 
       <th></th> 
      </tr> <?php 

      $name = $sub_category->name; 
      $args = array('post_type' => 'product', 
          "product_cat" => $sub_category->term_id //PROBLEM HERE 
        ); 

      $loop = new WP_Query($args); 

      if ($loop->have_posts()) { 

       while ($loop->have_posts()) : $loop->the_post(); 

        $product = new WC_Product(get_the_ID()); ?> 

        <tr> 
         <td class="name"><?php the_title(); ?><p class="small"><?php the_content(); ?></p></td> 
         <td><?php the_excerpt(); ?></td> 
         <td><?php echo $product->price; ?>zł</td> 
         <td><button class="button-product materialbutton">Rezerwuj</button> </td> 
        </tr> <?php 

       endwhile; 
      } 
      else { 

       echo __('No products found'); 
      } ?> 

      <h1>THE END</h1> <?php 
} //ALL UNCLOSED TAGS ARE GETTING CLOSED AFTERWARDS 

in Theorie Nun sollte es ein, die divs so anzuzeigen:

  1. Kategorie a: 1a. Kategoriebeschreibung 1b. Kategorietabelle
  2. Kategorie b: 2a. Kategoriebeschreibung 2b. Kategorie Tabelle

Aber stattdessen das Ergebnis sieht wie folgt aus: Outcome

So wie Sie nicht nur sehen, ist es nicht die Seite korrekt auslegen (die Reihenfolge ist descrption1, description2, Tabelle 1, Tabelle 2, Geist der Standort von <h1>THE END</h1>), scheint es auch nicht korrekt mit den Produkten der Kategorie übereinstimmen. Das gleiche Ergebnis tritt auf, wenn Id im Array setzen

"product_cat" => 14 // bewährte Kategorie-ID, enthält Beiträge

ich in wp erlebt, aber ziemlich neu in WooCommerce. Wenn mir jemand mit diesen Problemen helfen könnte, würde es gerne geschätzt werden.

+0

eigentlich nur ein Punkt, den Sie haben eine Menge offener Fragen mit keine Antworten/angenommene Antworten? – David

Antwort

1

Schwer von Ihren Bildern zu erzählen, aber ich bin fast 100% Sie schließen die Tabelle Tags nicht korrekt und was Sie beschreiben, ist genau das, was in diesem Fall passieren würde.

Problem ist, öffnen Sie eine Tabelle Tag an einem Iterationspunkt (foreach) und fügen Sie dann 2 Zeilen, ok bis jetzt, am nächsten Iterationspunkt, fügen Sie ein h2 Tag innerhalb der offenen Tabelle Tag und so weiter.

Der Browser wird versuchen, diese für Sie zu beheben, indem er die Tags schließt, aber er wird Ihren HTML-Code nicht für Sie neu anordnen, so dass die h Tags oberhalb der Tabelle in der gerenderten Ansicht sichtbar sind.

Siehe unten: Die Tabelle wird nur geöffnet, wenn es Posts gibt.Wenn Sie möchten, öffnen ein, wenn noch keine Kommentare, müssen Sie die Tabelle schließen, bevor erklärt etc

<?php 

$args2 = array('taxonomy' => 'product_cat', 'parent' => 9); 
$sub_cats = get_categories($args2); 

foreach($sub_cats as $sub_category) { ?> 

    <div class="treatments-description col-md-9" id="<?php echo $sub_category->term_id;?>"> 

     <h2 class="section-heading"> 
      <span class="line-behind-text"><?php echo $sub_category->name;?></span> 
     </h2> 

     <p class="section-text"> 
      OPIS: <?php echo category_description(); ?> //this part does not work too, not sure why 
     </p> 

     <h3 class="section-heading"><p class="line-behind-text">Dostepne zabiegi</p></h3> 
     <?php 

      $name = $sub_category->name; 
      $args = array('post_type' => 'product', 
          "product_cat" => $sub_category->term_id //PROBLEM HERE 
        ); 

      $loop = new WP_Query($args); 

      if ($loop->have_posts()) { 
       //only include the table if we have posts??? 

        echo '<table class="treatments-table table products">'; 

        echo '<tr class="table-heading"> 
         <th class="name" id="<?php echo $sub_category->term_id;?>">Usluga</th> 
         <th>Czas trwania</th> 
         <th>Cena</th> 
         <th></th> 
         </tr>'; 



       while ($loop->have_posts()) : $loop->the_post(); 

        $product = new WC_Product(get_the_ID()); ?> 

        <tr> 
         <td class="name"><?php the_title(); ?><p class="small"><?php the_content(); ?></p></td> 
         <td><?php the_excerpt(); ?></td> 
         <td><?php echo $product->price; ?>zl</td> 
         <td><button class="button-product materialbutton">Rezerwuj</button> </td> 
        </tr> <?php 

       endwhile; 

       echo '</table>'; 


      } 
      else { 

       echo __('No products found'); 
      } ?> 

      <h1>THE END</h1> <?php 
} //ALL UNCLOSED TAGS ARE GETTING CLOSED AFTERWARDS -- must close tags in the loop, otherwise multiple open tags!! 
+0

Ihr Code funktioniert, für alles außer der Anzeige der Produkte. Es gibt mir "keine Produkte gefunden", auch wenn es die Kategorie Info korrekt anzeigt – aln447

+0

Das Problem erscheint das selbe, auch wenn ich den Argumenten eine statische "product_cat" => 13 gebe, wobei 13 eine Katze mit Produkten laut wp-admin ist mindestens – aln447

+0

BEHOBEN! Das Problem war, dass product_cat nimmt den Namen nicht die ID, so mit '$ args = array ('post_type' => 'Produkt', " product_cat "=> $ sub_category-> name // PROBLEM HIER );' Hat gut funktioniert – aln447

1
  1. Für das erste Problem „keine Einträge gefunden“, ich bin ziemlich sicher, dass Sie category_description(); ersetzen müssen durch $sub_category->description; (in dem Code unten ist $sub_cat->description;)

  2. Für Sie zweites Problem, ich kann es nicht testen kann, und ich bin nicht ganz sicher, aber Sie brauchen etwas mehr (see in here) ...

  3. Sie müssen auch (am Ende) mit </table> Tisch zu schließen und auch </div> (am Ende)


<?php 

    $args2 = array('taxonomy' => 'product_cat', 'parent' => 9); 
    $sub_cats = get_categories($args2); 

    foreach($sub_cats as $sub_cat) { ?> 
     $sub_cat_name = $sub_cat->name; 
     $sub_cat_description = $sub_cat->description; // <= Here (1) 
     $sub_cat_id = $sub_cat->term_id; 

    <div class="treatments-description col-md-9" id="<?php echo $sub_category->term_id;?>"> 

     <h2 class="section-heading"> 
      <span class="line-behind-text"><?php echo $sub_cat_name; ?></span> 
     </h2> 

     <p class="section-text"> 
      OPIS: <?php echo $sub_cat_description; ?> 
     </p> 

     <h3 class="section-heading"><p class="line-behind-text">Dostępne zabiegi</p></h3> 

     <table class="treatments-table table products"> 

      <tr class="table-heading"> 
       <th class="name" id="<?php echo $sub_cat_id; ?>">Usługa</th> 
       <th>Czas trwania</th> 
       <th>Cena</th> 
       <th></th> 
      </tr> 
     <?php 

      global $post; // Here (2) 
      $terms = get_the_terms($post->ID, 'product_cat'); 
      foreach ($terms as $term) { 
       $product_cat_id = $term->term_id; 
       // $product_cat_name = $term->name; 
       break; 
      } 
      $args = array( 
       'post_type' => 'product', 
       'product_cat' => $product_cat_id' 
      ); 

      $loop = new WP_Query($args); 

      if ($loop->have_posts()) { 

       while ($loop->have_posts()) : $loop->the_post(); 

        $product = new WC_Product(get_the_ID()); ?> 

      <tr> 
       <td class="name"><?php the_title(); ?><p class="small"><?php the_content(); ?></p></td> 
       <td><?php the_excerpt(); ?></td> 
       <td><?php echo $product->price; ?>zł</td> 
       <td><button class="button-product materialbutton">Rezerwuj</button> </td> 
      </tr> 
      <?php 
       endwhile; ?> 
     </table> 
     <?php 
      } 
      else { 
       echo __('No products found'); 
      } ?> 

     <h1>THE END</h1> 
     <?php 
     } 
     ?> 
    </div>