在页面模板中显示特定分类(
问题描述:
)我有一个称为短期课程的自定义文章类型,并且我已添加一个自定义分类标准以称为课程类型,它使我可以对课程进行分类...在页面模板中显示特定分类(
我试图创建一个页面模板,将显示一个单一的分类(所有类别)的所有课程。在这种情况下,我想说明,已分配的分类(类)所有课程的Adobe ...香港专业教育学院尝试了一些不同的东西,但是我的继承人最新代码:
<?php
// get the custom post type's taxonomy terms
$custom_taxterms = wp_get_object_terms($post->ID, 'adobe', array('fields' => 'ids'));
// arguments
$args = array(
'post_type' => 'short_courses',
'post_status' => 'publish',
'posts_per_page' => 20, // you may edit this number
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => 'adobe',
'field' => 'id',
'terms' => $custom_taxterms
)
),
'post__not_in' => array ($post->ID),
);
$related_items = new WP_Query($args);
// loop over query
if ($related_items->have_posts()) :
echo '<ul>';
while ($related_items->have_posts()) : $related_items->the_post();
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php
endwhile;
echo '</ul>';
endif;
// Reset Post Data
wp_reset_postdata();
?>
它不通过任何东西拉。 ..
我曾尝试使用此代码太:
<?php
$args = array(
'post_type' => 'short_courses',
'orderby' => 'title',
'order' => 'ASC',
'product_categories' => 'adobe'
);
$the_query = new WP_Query($args);
?>
<?php if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); ?>
<?php
$thumbnail_id = get_post_thumbnail_id();
$thumbnail_url = wp_get_attachment_image_src($thumbnail_id, 'thumbnail-size', true);
?>
<p><a href="<?php the_permalink(); ?>"><img src="<?php the_field('image'); ?>" alt="<?php the_title();?> graphic"></a></p>
<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
<?php $products_count = $the_query->current_post + 1; ?>
<?php if ($products_count % 4 == 0): ?>
<?php endif; ?>
<?php endwhile; endif; ?>
但这个代码拉通过所有的短期课程,而不仅仅是那些的Adobe ...
任何人都可以电话米我要去哪里?
答
好吧,我设法解决这个问题 - 这只是一个小错误。在上面的第二批代码中,说product_categories的部分需要更改为course_type!因此,继承人的新的工作代码:
<?php
$args = array(
'post_type' => 'short_courses',
'orderby' => 'title',
'order' => 'ASC',
'course_type' => 'adobe'
);
$the_query = new WP_Query($args);
?>
<?php if (have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); ?>
<?php
$thumbnail_id = get_post_thumbnail_id();
$thumbnail_url = wp_get_attachment_image_src($thumbnail_id, 'thumbnail-size', true);
?>
<p><a href="<?php the_permalink(); ?>"><img src="<?php the_field('image'); ?>" alt="<?php the_title();?> graphic"></a></p>
<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
<?php $products_count = $the_query->current_post + 1; ?>
<?php if ($products_count % 4 == 0): ?>
<?php endif; ?>
<?php endwhile; endif; ?>
答
你注意到了这条线吗?
<?php if (have_posts()) : while
,你可以把它改成
<?php if ($the_query->have_posts()) : while
,看看是否应用了过滤器? WordPress具有全球职位功能,例如get_the_title(),这可能会干扰您的自定义查询对象,例如您正在使用的查询对象$ the_query。
我还建议您在此之前删除if声明以提高代码的可读性。 while循环不会在页面上产生任何输出,如果查询没有任何帖子,那么需要if语句。
整个代码就变成
<?php
$args = array(
'post_type' => 'short_courses',
'orderby' => 'title',
'order' => 'ASC',
'product_categories' => 'adobe'
);
$the_query = new WP_Query($args);
?>
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<?php
$thumbnail_id = get_post_thumbnail_id();
$thumbnail_url = wp_get_attachment_image_src($thumbnail_id, 'thumbnail-size', true);
?>
<p><a href="<?php the_permalink(); ?>"><img src="<?php the_field('image'); ?>" alt="<?php the_title();?> graphic"></a></p>
<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
<?php $products_count = $the_query->current_post + 1; ?>
<?php if ($products_count % 4 == 0): ?>
<?php endif; ?>
<?php endwhile; ?>
请确保该分类名称您正在使用“product_categories” =>“土坯”是你在register_taxonomy功能,即使用相同的一个确保你有register_taxonomy('product_categories',array('short_courses'),$ args);如果您还有其他产品类别,请在查询中使用它。希望这可以帮助。
似乎并不具有唉效果恐怕@Nkole - 我已经更新了我的代码,但增加了... –
高兴你得到它的工作。那些可能令人沮丧! –