按字母顺序列出产品属性(Woocommerce)

时间:2022-10-25 10:15:28

For starters thank you for taking a look / any assistance you may offer.

对于初学者,感谢您查看/提供任何帮助。

I am trying to create a custom page template that creates a list in alphabetical order displaying all the results for a specific (Woocommerce) product attribute across my site.

我正在尝试创建一个自定义页面模板,按字母顺序创建一个列表,显示我网站上特定(Woocommerce)产品属性的所有结果。

So in my case the product attribute is "Artist"

所以在我的情况下,产品属性是“艺术家”

I found some code that does a similar thing for posts, which altered gives this:

我发现一些代码对帖子做了类似的事情,改变了这个:

<?php 
/*
Template Name: Artists

*/
$posts_per_row = 4;
$posts_per_page = 400;
?>

<?php get_header(); ?>
<?php get_template_part( 'headers/page', 'header' ); ?> 

<style type="text/css">
.letter-group { width: 100%; }
.letter-cell { width: 5%; height: 2em; text-align: center; padding-top: 8px; margin-bottom: 8px; float: left; }
.row-cells { width: 75%; float: right; margin-right: 180px; }
.title-cell { width: 25%;  float: left; overflow: hidden; margin-bottom: 8px; }
.clear { clear: both; }
</style>

 <div class="ps-container">
        <div class="container clearfix">
<div id="main-background">

   <div id="main-column">
      <h1><?php the_title(); ?></h1>

      <div class="margin-top"></div>  

      <div id="a-z">

         <?php
         $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
         $args = array (
            'posts_per_page' => $posts_per_page,
            'post_type' => 'product',
            'orderby' => 'title',
            'order' => 'ASC',
            'paged' => $paged
         );
         query_posts($args);
         if ( have_posts() ) {
            $in_this_row = 0;
            while ( have_posts() ) {
               the_post();
               $first_letter = strtoupper(substr(apply_filters('the_title',$post->post_title),0,1));
               if ($first_letter != $curr_letter) {
                  if (++$post_count > 1) {
                     end_prev_letter();
                  }
                  start_new_letter($first_letter);
                  $curr_letter = $first_letter;
               }
               if (++$in_this_row > $posts_per_row) {
                  end_prev_row();
                  start_new_row();
                  ++$in_this_row;  // Account for this first post
               } ?>
               <div class="title-cell"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></div>
            <?php }
            end_prev_letter();
            ?>
            <div class="navigation">
               <div class="alignright"><?php next_posts_link('Next Page &raquo;') ?></div>
               <div class="alignleft"><?php previous_posts_link('&laquo; Previous Page') ?></div>
            </div>
         <?php } else {
            echo "<h2>Sorry, no artists were found!</h2>";
         }
         ?>

      </div><!-- End id='a-z' -->

   </div><!-- End class='margin-top -->

</div><!-- End id='rightcolumn' -->
</div>
</div>

<?php get_footer(); ?>

<?php
function end_prev_letter() {
   end_prev_row();
   echo "</div><!-- End of letter-group -->\n";
   echo "<div class='clear'></div>\n";
}
function start_new_letter($letter) {
   echo "<div class='letter-group'>\n";
   echo "\t<div class='letter-cell'>$letter</div>\n";
   start_new_row($letter);
}
function end_prev_row() {
   echo "\t</div><!-- End row-cells -->\n";
}
function start_new_row() {
   global $in_this_row;
   $in_this_row = 0;
   echo "\t<div class='row-cells'>\n";
}

?>

Now to alter this, I tried using get attribute in place of the array retrieving my posts, as I normally do in other spots of my site using:

现在要改变这一点,我尝试使用get属性代替检索我的帖子的数组,就像我通常在我的网站的其他位置使用:

global $product;    
$product->get_attribute( ‘pa_attribute’ )

But nothing is working.

但没有任何工作。

Can anyone shed some light on my issue? I get it showing posts just fine, but I can't transition this to properly list the Artists names instead of product (post) names.

有谁可以解释我的问题?我觉得它显示的帖子很好,但是我无法将其转换为正确列出艺术家名称而不是产品(帖子)名称。

Thank you.

1 个解决方案

#1


Merging the two examples in the Codex, and assuming your attribute is called "artist" then you'd get a list of terms like so:

合并Codex中的两个示例,并假设您的属性被称为“artist”,那么您将得到如下所示的术语列表:

$terms = get_terms( 'pa_artist' );
 if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
     echo '<ul>';
     foreach ( $terms as $term ) {
       echo '<li><a href="' . get_term_link( $term ) . '" title="' . sprintf( __( 'View all post filed under %s', 'my_localization_domain' ), $term->name ) . '">' . $term->name . '</a></li>';

     }
     echo '</ul>';
 }

#1


Merging the two examples in the Codex, and assuming your attribute is called "artist" then you'd get a list of terms like so:

合并Codex中的两个示例,并假设您的属性被称为“artist”,那么您将得到如下所示的术语列表:

$terms = get_terms( 'pa_artist' );
 if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
     echo '<ul>';
     foreach ( $terms as $term ) {
       echo '<li><a href="' . get_term_link( $term ) . '" title="' . sprintf( __( 'View all post filed under %s', 'my_localization_domain' ), $term->name ) . '">' . $term->name . '</a></li>';

     }
     echo '</ul>';
 }