小冬SEO

wordpress调用指定分类列表的循环标签3种

2019-06-11 7:48:44 2439 WordPress笔记

直接上调用代码吧:

1.调用指定分类的列表循环调用标签:

<?php query_posts('cat=3&showposts=10');?>
<?php while (have_posts()) : the_post(); ?>
<li><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" rel="nofollow"><?php the_title(); ?></a></li>
<?php endwhile; wp_reset_query(); ?>

2.调用指定分类的的列表循环调用标签,可加限制参数

<?php if (have_posts()) : ?>
<?php query_posts('cat=2' . $mcatID. '&caller_get_posts=4&showposts=4&offset=1'); ?>
<?php while (have_posts()) : the_post(); ?>
<li>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" rel="nofollow"><img src="<?php echo catch_first_image() ?>" alt="<?php the_title(); ?>"><p><?php the_title(); ?></p></a>
</li>
<?php endwhile; ?>
<?php endif; wp_reset_query(); ?>
<!--其中cat为栏目id,offset为从第几条开始调用,showposts为先是几条。-->

3.调用指定分类下的列表

<?php query_posts('showposts=6&cat=1'); 
//cat=1为调用ID为1的分类下6篇文章
while(have_posts()) : the_post(); ?>
<li><a href="<?php echo the_permalink() ?>"  title="<?php the_title(); ?>"><?php the_title(); ?></a><?php the_time('Y-m-d'); ?></li>
<?php endwhile; ?>

4.调用多个指定分类下的文章列表

<?php
            $args=array(
                'cat' => array(16,17,18,19,20),   // 分类ID
                'posts_per_page' => 32, // 显示篇数
            );
            query_posts($args);
            if(have_posts()) : while (have_posts()) : the_post();
            ?>
<a href="<?php the_permalink() ?>" target="_blank"><?php the_title(); ?></a>
            </li>
           
        <?php  endwhile; endif; wp_reset_query(); ?>

5.调用指定多个列表下的分类和列表循环

<?php
query_posts('posts_per_page=4&caller_get_posts=1&orderby=modified&cat='.$cid);
// print_r(wp_list_categories('depth=1&title_li=&show_count=0&hide_empty=0&child_of='.$cid));
while (have_posts()) : the_post();
echo '<article class="storedesign">';
echo '<div class="storedesign_img">';
echo '<a target="_blank" href="'.get_permalink().'" rel="nofollow">';
  if (has_post_thumbnail()) { 
the_post_thumbnail( 'thumbnail',array( 'alt' => trim(strip_tags( $post->post_title )), 'class' => 'home-thumb'));
}
else { //如果缩略图不存在,就调用catch_first_image()方法获取第一张图或默认图
echo '<img src="'.catch_first_image().'" width="270px" height="166px" alt="'.$post->post_title.'" title="'.$post->post_title.'">';
}
echo '</a></div>';
echo '<div class="storedesign_h">';
echo '<header>';
echo '<a target="_blank" href="'.get_permalink().'" title="'.$post->post_title.'">';
echo '<h3>'.$post->post_title.'</h3>';
echo '</a>';
echo '</header>';
echo '<div class="information">';
echo '<div class="source">';
echo '<a target="_blank" href="/'.get_the_category()[0]->slug.'/" rel="nofollow">';
// $category = the_ID();
// $category = get_the_category();
// $parent = get_cat_name($category->category_parent);
// print_r($category);
// if (!empty($parent)) {
// echo $parent;
// } else {
// echo $category[0]->cat_name;
// }
echo ''.get_the_category()[0]->name.'';
//当前分类名称
echo '</a>';
echo '</div>';
echo '<time title="发布时间">';
echo ''.the_time("Y-m-d").'';
echo '</time>';
echo '<div class="pageviews" title="浏览量">';
echo ''.getPostViews(get_the_ID()).'浏览';
echo '</div>';
echo '</div>';
echo '<div class="information"><div class="comment"><a target="_blank" href="'.get_permalink().'" title="评论" rel="nofollow">评论</a></div>';
echo '<div class="details"><a target="_blank" href="'.get_permalink().'" rel="nofollow" title="查看详情">查看详情</a></div>';
echo '</div>';
echo '</div>';
echo '</article>';
endwhile;
wp_reset_query();
?>
            </ul>
          </div>
          </div>
    </div>
<?php } ?>

版权保护: 本文由小冬SEO编辑发布,转载请保留链接: http://www.myseoyh.cn/shuo/119.html