[英]Display posts that belong to certain category - Wordpress
我很难让它正常工作。 有人可以提供类别模板的快速摘要,该模板显示属于“产品A”类别的帖子。 在过去的3个小时里,我一直在使用试错法,但是没有运气。
谢谢!
这就是我一直在玩的-
<?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>
您可以使用WP_Query类。 我之前做过的一种方法是,首先创建Product-A的类别名称,然后将首尾名称“ product-a”全部小写。
然后实例化该类的新实例。 传递“ category_name = product-a”参数您不会传递带有此参数的类别名称,而会传递子弹名称。 一旦这样做,您应该可以使用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; ?>
几乎所有内容都与常规循环相同,而不仅仅是
<?php if(have_post()) : while(have_post()) : the_post() ?>
您将使用对象表示法来引用此特定查询。
<?php if($my_query->have_post()) : while($my_query->have_post()) : $my_query->the_post() ?>
希望能帮助到你。
首先获取您的产品A类别ID; (如果使用,则在自定义查询中使用的猫ID可以完美地代替类别名称。)
<?php
query_posts('cat=1');
while (have_posts()) : the_post();
the_content();
endwhile;
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.