想要在首页调用wordpress某个栏目的内容,可以按照分类ID来调用,调用出来的内容一般有:调用栏目最新内容、调用栏目推荐内容、调用栏目随机内容这三种形式。简站wordpress小编在此为大家放出三种不同方式调用的代码如下:
通过指定分类目录ID调用该目录下的最新内容
<?php $posts = get_posts( “category=4&numberposts=6” ); ?>
<?php if( $posts ) : ?>
<?php foreach( $posts as $post ) : setup_postdata( post ); ?> <div class="col-md-3"> <a rel="nofollow" href="<?php the_permalink() ?>"><img src="<?php if ( has_post_thumbnail() ) { ?> <?php echo get_the_post_thumbnail_url(post->ID); //wodepress.com ?>
<?php } else {?>
<?php bloginfo(‘template_url’); ?>/images/noneimg-product.jpg
<?php } ?>" alt=“<?php the_title(); ?>” class=“shadow-sm img-fluid”></a>
</div>
<!-- col-lg-3 -->
<?php endforeach; ?>
<?php endif; ?>
<?php wp_reset_query();?>
说明:category=4为分类目录ID numberposts=6为要显示数量
通过指定分类目录ID调用该目录下的推荐内容
<?php
args = array( 'posts_per_page' => 6, 'cat' => 4, 'post__in' => get_option( 'sticky_posts' ) );//wodepress.com sticky_posts = new WP_Query( $args );
while ( $sticky_posts->have_posts() ) : sticky_posts->the_post();?> <div class="col-md-3"> <a rel="nofollow" href="<?php the_permalink() ?>"><img src="<?php if ( has_post_thumbnail() ) { ?> <?php echo get_the_post_thumbnail_url(post->ID); ?>
<?php } else {?>
<?php bloginfo(‘template_url’); ?>/images/noneimg-product.jpg
<?php } ?>" alt=“<?php the_title(); ?>” class=“shadow-sm img-fluid”></a>
</div>
<!-- col-lg-3 -->
<?php endwhile; wp_reset_query();?>
说明:posts_per_page 6为要显示的数量 cat 4 为要调用的分类目录ID
通过指定分类目录ID调用该目录下的随机内容
<?php
c
a
t
=
g
e
t
t
h
e
c
a
t
e
g
o
r
y
(
)
;
f
o
r
e
a
c
h
(
cat = get_the_category(); foreach(
cat=getthecategory();foreach(cat as
k
e
y
=
>
key=>
key=>category){
$catid = category->term_id; } args = array(‘orderby’ => ‘rand’,‘showposts’ => 6,‘cat’ => 4 );
q
u
e
r
y
p
o
s
t
s
=
n
e
w
W
P
Q
u
e
r
y
(
)
;
/
/
w
o
d
e
p
r
e
s
s
.
c
o
m
query_posts = new WP_Query();//wodepress.com
queryposts=newWPQuery();//wodepress.comquery_posts->query(
a
r
g
s
)
;
w
h
i
l
e
(
args); while (
args);while(query_posts->have_posts()) : query_posts->the_post(); ?> <div class="col-md-3"> <a rel="nofollow" href="<?php the_permalink() ?>"><img src="<?php if ( has_post_thumbnail() ) { ?> <?php echo get_the_post_thumbnail_url(post->ID); ?>
<?php } else {?>
<?php bloginfo(‘template_url’); ?>/images/noneimg-product.jpg
<?php } ?>" alt=“<?php the_title(); ?>” class=“shadow-sm img-fluid”></a>
</div>
<!-- col-lg-3 -->
<?php endwhile;?>
<?php wp_reset_query(); ?>
说明:showposts 6为要显示的数量 cat 4为要调用的分类目录的ID
原文
https://www.jianzhanpress.com/?p=7223