老外的WordPress主题大多自带了 作品集(Portfolio)这个自定义文章类型,用来显示自己的作品集。
那么,如果你想在自己的页面调用WordPress 调用Portfolio下面某个分类的文章该如何操作呢?网上找了很久没有找到类似的文章,今天好哇网就来讲下WordPress 调用 Portfolio 自定义文章类型分类目录文章的方法。
其实 Portfolio 是WordPress的一种自定义文章类型,知道了这个就好办了,我们查阅WordPress文档就可以知道调用方法了。https://developer.wordpress.org/reference/classes/wp_query/
通过 Portfolio 分类目录名称调用 Portfolio 文章
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <?php $args = array( 'post_type' => 'portfolio', 'posts_per_page' => 3, 'tax_query' => array( array( 'taxonomy' => 'portfolio_entries', //编辑分类查看具体名称 Could be anything in your case 'field' => 'slug', 'terms' => 'honor' //通过分类slug名获取 ) ) ); // The Query $the_query = new WP_Query( $args ); // The Loop if ( $the_query->have_posts() ) { echo '<ul>'; while ( $the_query->have_posts() ) { $the_query->the_post(); echo '<li>' . get_the_title() . '</li>'; //输出标题 echo the_post_thumbnail(); //输出缩略图 echo echo the_content(); //输出内容 } echo '</ul>'; } else { // no posts found echo '暂时没有发布内容'; } /* Restore original Post Data */ wp_reset_postdata(); ?> |
通过 Portfolio 分类目录ID调用 Portfolio 文章
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <?php //通过分类目录ID获取portfolio分类目录文章 $args = array( 'post_type' => 'portfolio', 'posts_per_page' => 8, 'term_ID' => array( 2, 19 ) //分类目录ID 多个用,逗号分隔 ); // The Query $the_query = new WP_Query( $args ); // The Loop if ( $the_query->have_posts() ) { echo '<ul>'; while ( $the_query->have_posts() ) { $the_query->the_post(); echo '<li>' . get_the_title() . '</li>'; //输出标题 echo the_post_thumbnail(); //输出缩略图 echo echo the_content(); //输出内容 } echo '</ul>'; } else { // no posts found echo '暂时没有发布内容'; } /* Restore original Post Data */ wp_reset_postdata(); ?> |
以上就是WordPress 调用 Portfolio 自定义文章类型分类目录文章的方法,好哇网首发分享,希望能够帮到大家。