admin管理员组文章数量:1323175
I try to create blog page with specific categories (by ID) and display only newest blog post from this category. I have code like this but it shows only first category. I have work code for display only one category but when I put more id it doesn't work
<?php
/**
* Template Name: Porftfolio
*/
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'cat' => '187,186',
'posts_per_page' => 1,
);
$arr_posts = new WP_Query( $args );
if ( $arr_posts->have_posts() ) :
while ( $arr_posts->have_posts() ) :
$arr_posts->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php
if ( has_post_thumbnail() ) :
the_post_thumbnail();
endif;
?>
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
</header>
<div class="entry-content">
<a href> <?php the_permalink(); ?> </a>
</div>
</article>
<?php
endwhile;
endif;
?>
</main><!-- .site-main -->
</div><!-- .content-area -->
<?php get_footer(); ?>
I try to create blog page with specific categories (by ID) and display only newest blog post from this category. I have code like this but it shows only first category. I have work code for display only one category but when I put more id it doesn't work
<?php
/**
* Template Name: Porftfolio
*/
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'cat' => '187,186',
'posts_per_page' => 1,
);
$arr_posts = new WP_Query( $args );
if ( $arr_posts->have_posts() ) :
while ( $arr_posts->have_posts() ) :
$arr_posts->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php
if ( has_post_thumbnail() ) :
the_post_thumbnail();
endif;
?>
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
</header>
<div class="entry-content">
<a href> <?php the_permalink(); ?> </a>
</div>
</article>
<?php
endwhile;
endif;
?>
</main><!-- .site-main -->
</div><!-- .content-area -->
<?php get_footer(); ?>
Share
Improve this question
edited Sep 8, 2020 at 7:23
cjbj
15k16 gold badges42 silver badges89 bronze badges
asked Aug 26, 2020 at 18:51
Monika KwiatkowskaMonika Kwiatkowska
483 bronze badges
2 Answers
Reset to default 0Please add following code
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category__and' => array( 2, 6 ),
'posts_per_page' => 1,
);
$arr_posts = new WP_Query( $args );
OR
$tax_query = array(
relation => 'AND',
array(
'taxonomy' => 'category',
'field' => 'term_id', // 'term_id' by default, so just here as an example
'terms' => 186,
'include_children' => false, // true by defualt
'operator' => 'IN' // 'IN' by default, so just here as an example
),
array(
'taxonomy' => 'category',
'terms' => 187,
'include_children' => false, // true by defualt
)
)
If what you want is to get a post that belongs to BOTH categories, you need to use category__in
or category__and
parameter instead of cat
.
You could also use 'category_name' => "first-name+second-name"
too, but that takes in the category slugs, not IDs.
Note: Just FYI, that query is fetching 1 post with EITHER of those categories. Not necessarily BOTH at the same time.
For you fetch a post with both categories assigned to it category__in
and category__and
would do the trick. Difference being that category__in
does not show posts in child categories of the ones passed in, while category__and
would show child categories too (if exist)
See Category Parameters for more info
P.S: nikhil hadvani's answer seems right as well, just saw it updated as I sent this. Either those or these should fix it.
本文标签: Blog page with posts from specific categories
版权声明:本文标题:Blog page with posts from specific categories 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742132751a2422236.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论