[英]Display posts that belong to certain category - Wordpress
I'm having trouble getting this to work. 我很难让它正常工作。 Can someone provide a quick snippet for category template that displays posts that belong to a category called 'Product A'.
有人可以提供类别模板的快速摘要,该模板显示属于“产品A”类别的帖子。 I've been using the trial and error method for the past 3 hours with no luck.
在过去的3个小时里,我一直在使用试错法,但是没有运气。
Thank you! 谢谢!
Here's what I've been playing around with - 这就是我一直在玩的-
<?php
/*
Template Name: yadayada
*/
?>
<?php get_header(); ?>
<?php get_sidebar(); ?>
<?php query_posts('cat=32&showposts=5'); ?>
<div class="post">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<div class="post-description">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
</div>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
</div>
You can used the WP_Query class. 您可以使用WP_Query类。 One way I've done it before is by first creating a category name of Product-A and making the slug 'product-a' all lower case.
我之前做过的一种方法是,首先创建Product-A的类别名称,然后将首尾名称“ product-a”全部小写。
Then instantiate a new instance of the class. 然后实例化该类的新实例。 Pass in the parameter of 'category_name=product-a' You do no pass in the category name with this parameter, but rather the slug name.
传递“ category_name = product-a”参数您不会传递带有此参数的类别名称,而会传递子弹名称。 once you do that you should be able to use the WP_Query as follows:
一旦这样做,您应该可以使用WP_Query,如下所示:
<?php $my_query = new WP_Query( 'category_name=product-a' ); ?>
<?php if ($my_query->have_posts() ) : ?>
<?php while ( $my_query->have_posts()) : $my_query->the_post() ?>
<article <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<div class="product-excerpt"><?php the_content(); ?> </div>
</article>
<?php endwhile; ?>
<?php else : ?>
<h2>Not Found</h2>
<?php endif; ?>
pretty much everything is the same as the regular loop but instead of just 几乎所有内容都与常规循环相同,而不仅仅是
<?php if(have_post()) : while(have_post()) : the_post() ?>
You would used object notation to refer to this particular query. 您将使用对象表示法来引用此特定查询。
<?php if($my_query->have_post()) : while($my_query->have_post()) : $my_query->the_post() ?>
hope it helps. 希望能帮助到你。
First get your Product A category id; 首先获取您的产品A类别ID; (if you use, your cat id in your custom query it 's gonna work perfectly instead of category name.)
(如果使用,则在自定义查询中使用的猫ID可以完美地代替类别名称。)
<?php
query_posts('cat=1');
while (have_posts()) : the_post();
the_content();
endwhile;
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.