建站教程

建站教程

Products

当前位置:首页 > 建站教程 >

wordpress获取分类下文章列表的两个方法(调用wordpress指定自定义文章类型分类文章列表的方法)

GG网络技术分享 2025-03-18 16:12 3


wordpress获取分类下文章列表的两个方法

方法一

使用query_posts()函数

showposts表示获取的数量,在执行完query_posts后,必须保证同时执行 wp_reset_query

方法二

使用 get_posts()函数,category获取某个分类下的文章,numberposts获取相应的数目。

调用wordpress指定自定义文章类型分类文章列表的方法

在做wordpress企业网站的时候,常常需要在首页调用分类文章列表,默认的分类文章列表调用,前面的教程里面介绍过了。

今天给大家分享调用指定自定义文章类型分类文章列表。

方法一:通过get_posts函数调用

<?php $posts = get_posts(array('numberposts' => '10', //输出的文章数量'post_type' => 'product',//自定义文章类型名称'tax_query'=>array(array('taxonomy'=>'products', //自定义分类法名称'terms'=>'10' //id为64的分类。也可是多个分类array(12,64))),));?>

<ul>

<?php if($posts): foreach($posts as $post): ?>

<li><a href="<?php the_permalink(); ?>" target="_blank" title="<?php the_title();?>"><?php the_title();?></a></li>

<?php wp_reset_postdata(); endforeach; endif;?></ul>

方法二:通过强大的WP_Query函数调用

<?php     $args = array(        'post_type' => 'product', //自定义文章类型名称        'showposts' => 10,        'tax_query' => array(            array(                'taxonomy' => 'products',//自定义分类法名称                'terms' => 64 //id为64的分类。也可是多个分类array(12,64)                ),            )        );    $my_query = new WP_Query($args);    if( $my_query->have_posts() ) {        while ($my_query->have_posts()) : $my_query->the_post();?>       

<li><a href="<?php the_permalink(); ?>" target="_blank" title="<?php the_title();?>"><?php the_title();?></a></li>

<?php endwhile; wp_reset_query(); } ?>

参数说明:

  • post_type 要调用的自定义文章类型的名称(必须和要调用的自定义分类法关联)
  • taxonomy 要调用的自定义分类法的名称
  • terms 要调用的自定义分类法下创建的分类目录ID

标签:

提交需求或反馈

Demand feedback