我有一个页面,将所有产品都放在一个页面上Link 前往试验现场。
我想做的是使用其类别订购产品<果汁、花蜜、浓缩液等
和往常一样
$orderby =
id
name
slug
count
term_group
没有按需要的方式订购。是否有可能?如果有,我将如何实现这一点?
我的产品。php:
<?php
/*
Template Name: Products
*/
get_header();
?>
</div>
<?php
$orderby = \'name\';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$taxonomy = \'mahlakategooriad\';
$title = \'\';
$args = array(
\'orderby\' => $orderby,
\'show_count\' => $show_count,
\'pad_counts\' => $pad_counts,
\'hierarchical\' => $hierarchical,
\'taxonomy\' => $taxonomy,
\'title_li\' => $title
);
$term_obj = get_terms($taxonomy,$args);
$type = \'produktid\';
$args = array( \'post_type\' => \'produktid\', \'posts_per_page\' => 1);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
echo \'<div class="main small">\';
echo \'<h1><a href="\'.get_permalink().\'">\'.get_the_title().\'</a></h1>\';
the_content();
echo \'</div>\';
?>
<div class="big-pic"><?php echo get_the_post_thumbnail( $loop->post->ID, \'tooted-big\' ); ?></div>
<?php
endwhile;
$args=array(
\'post_type\' => $type,
\'post_status\' => \'publish\',
\'posts_per_page\' => -1,
\'caller_get_posts\'=> 1,
\'orderby\' => \'date\',
\'order\' => \'ASC\'
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
?>
<div class="tooted">
<?php
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php
$toote_thumb = get_the_post_thumbnail( $page->ID, \'tooted-thumb\' );
$toote_big = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), \'tooted-big\' );
if ($toote_thumb) {
echo \'<div class="toode">\';
echo \'<a href="\'.get_permalink().\'" class="toote-thumb" data-link="\'.$toote_big[0].\'">\'.$toote_thumb.\'</a>\';
}
?>
<a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>" class="toote-title"><?php the_title(); ?></a></div>
<?php
endwhile;
}
wp_reset_query();
?>
<div class="clear"></div>
</div>
<div class="clear"></div>
最合适的回答,由SO网友:gmazzap 整理而成
在回答之前,我想说,您的代码并不是真的错了,但可以改进:
当您只能运行一个查询时,为什么要运行两个查询
caller_get_posts
已弃用,请使用ignore_sticky_posts
, 取而代之的是get_terms
没有title_li
也没有show_count
论点此外$taxonomy
是第一个参数,在$args
数组(第二个参数)。看来你的论点是有意的wp_list_categories
功能您可以从mahlakategooriad
分类法,但在模板中不使用它们。也许你不把所有的代码都贴出来您设置
$wp_query
到
null
在有效填充之前?为什么?此外,您使用
wp_reset_query
这在自定义查询中不需要。使用
wp_reset_postdata
, 取而代之的是我们来回答吧。
我将遵循的工作流是运行一个posts循环,将posts放在一个助手数组中,并使用其类别slug作为键。在循环过程中,我还加入了另一个变量第一个粘性帖子。
当数组被填充时,我开始循环遍历它并输出标记。
// get posts
$args = array(
\'post_type\' => \'produktid\',
\'posts_per_page\' => -1,
\'orderby\' => \'date\',
\'order\' => \'ASC\' ,
\'ignore_sticky_posts\' => true
);
$my_query = new WP_Query( $args );
// init helper vars
$first = null;
$sticky = null;
$ordered = array();
// loop
while ( $my_query->have_posts() ) : $my_query->the_post();
$terms = get_the_terms( get_the_ID(), \'mahlakategooriad\' );
$term = array_shift($terms); // get first term
global $post;
if ( ! isset($ordered[$term->slug]) ) $ordered[$term->slug] = array();
$ordered[$term->slug][] = $post;
if ( $my_query->current_post == 0 ) $first = $post;
if ( is_null($sticky) && is_sticky() ) $sticky = $post;
endwhile;
wp_reset_postdata();
// get the array of term slugs and order it by name
$terms = array_keys( $ordered );
sort( $terms );
// get the big thumbnail and print it
$featured = $sticky ? : $first;
if ( ! empty($featured) ) {
global $post;
setup_postdata($post);
echo \'<div class="main small">\';
echo \'<h1><a href="\' . get_permalink() . \'">\' . get_the_title() . \'</a></h1>\';
the_content();
echo \'</div>\';
$big_img = get_the_post_thumbnail( $featured->ID, \'tooted-big\' );
if ( ! empty( $big_img ) ) printf(\'<div class="big-pic">%s</div>\', $big_img);
}
wp_reset_postdata();
// the loop
echo \'<div class="tooted">\';
if ( ! empty($terms) ) { foreach ( $terms as $term ) {
$ordered_posts = $ordered[$term];
$term_obj = get_term_by(\'slug\', $term, \'mahlakategooriad\');
printf(\'<h3>\' . __(\'Products in %s\') . \'</h3>\', $term_obj->name );
global $post;
foreach ( $ordered_posts as $post) {
setup_postdata($post);
$thumb_id = get_post_thumbnail_id( get_the_ID() );
$plink = get_permalink();
$title = get_the_title();
if ($thumb_id) {
$thumb = wp_get_attachment_image_src( $thumb_id, \'tooted-thumb\' );
$big = wp_get_attachment_image_src( $thumb_id, \'tooted-big\' );
echo \'<div class="toode">\';
printf(
\'<a href="%s" class="toote-thumb" data-link="%s"><img src="%s" alt="%s" /></a>\',
$plink, $big[0], $thumb[0], esc_attr($title)
);
}
printf(
\'<a href="%s" title="%s" class="toote-title">%s</a></div>\',
$plink, esc_attr($title), $title
);
}
echo \'<div class="clear"></div>\';
} }
wp_reset_postdata();
echo \'<div class="clear"></div></div><div class="clear"></div>\';