Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 按字母顺序列出产品属性(Woocommerce)_Php_Wordpress_Attributes_Woocommerce_Product - Fatal编程技术网

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

Php 按字母顺序列出产品属性(Woocommerce),php,wordpress,attributes,woocommerce,product,Php,Wordpress,Attributes,Woocommerce,Product,首先,感谢您的关注/您可能提供的任何帮助 我正在尝试创建一个自定义页面模板,该模板按字母顺序创建一个列表,显示整个站点中特定(Woocommerce)产品属性的所有结果 所以在我的例子中,产品属性是“艺术家” 我发现一些代码对帖子做了类似的事情,它改变了这一点: <?php /* Template Name: Artists */ $posts_per_row = 4; $posts_per_page = 400; ?> <?php get_header(); ?>

首先,感谢您的关注/您可能提供的任何帮助

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

所以在我的例子中,产品属性是“艺术家”

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

<?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";
}

?>
但一切都不起作用

有人能解释一下我的问题吗?我可以很好地显示帖子,但我不能将其转换为正确地列出艺术家的名字,而不是产品(帖子)的名字


谢谢。

合并法典中的两个示例,并假设您的属性名为“艺术家”,则您将得到如下术语列表:

$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>';
 }
$terms=get_terms('pa_artist');
如果(!empty($terms)&&!is_wp_error($terms)){
回声“
    ”; foreach($terms作为$term){ 回音“
  • ”; } 回声“
”; }
如果您想获得术语列表,则需要使用查询术语,而不是查询帖子。顺便说一下,无论如何,请使用而不是
query\u posts()
。谢谢你的提示。请原谅我在这里缺乏知识,但是在这种情况下,我将如何使用get_terms()作为属性呢?请参阅我链接到的Codex文章。这里甚至有一个很好的例子,说明如何查询这些术语,然后循环使用它们。我会这样做的,谢谢。我想没有人会回答,所以如果你想把你的评论变成一个答案,我会接受的。谢谢你的帮助。
$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>';
 }