admin管理员组文章数量:1130349
currently I am working on some customized blog templating and was wondering if there is any way to find out if the newest post of a category is also the newest post of the whole blog, so I can skip the first post of the category.
- (1) is the newest post of the whole blog, which is in category (B).
- (2) is the newest post of the category (A)
- (3) is the newest post of the category (B), also is (1)
Basically I am asking how I would do this: If (3) = (1), skip (3) and show 2nd newest post in the category (in this case category (B)).
Additional information about my blog specifically, while the information above is more general/universal.
In my blog I also have a category that is excluded from the blog and only shown on a specific page. How would I exclude this category from the whole solution for the initial question? Would it simply be enough to write 'cat' => -123,?
currently I am working on some customized blog templating and was wondering if there is any way to find out if the newest post of a category is also the newest post of the whole blog, so I can skip the first post of the category.
- (1) is the newest post of the whole blog, which is in category (B).
- (2) is the newest post of the category (A)
- (3) is the newest post of the category (B), also is (1)
Basically I am asking how I would do this: If (3) = (1), skip (3) and show 2nd newest post in the category (in this case category (B)).
Additional information about my blog specifically, while the information above is more general/universal.
In my blog I also have a category that is excluded from the blog and only shown on a specific page. How would I exclude this category from the whole solution for the initial question? Would it simply be enough to write 'cat' => -123,?
2 Answers
Reset to default 3So the easiest way to do this would be to store the ID of the first post (1) then in each of your category loops you can use the post__not_in property like so:
// inside the first loop at the top.
$latest_post_id = get_the_ID();
// WP_Query for fetching each category
$category_query = new WP_Query( [
// other parameters
'post__not_in' => [ $latest_post_id ],
] );
Now to exclude a category in WP_Query you can use category__not_in which takes an array of category ID's. It's definitely worth checking out the wordpress codex for WP_Query
Just use ‘post__not_in’ param in your second query.
$query1 = new WP_Query...
$used_posts = array();
while ( $query1->have_posts() ) :
$query1->the_post();
$used_posts[]= get_the_ID();
...
endwhile;
$query2 = new WP_Query( array(
'post__not_in' => $used_posts,
...
) );
currently I am working on some customized blog templating and was wondering if there is any way to find out if the newest post of a category is also the newest post of the whole blog, so I can skip the first post of the category.
- (1) is the newest post of the whole blog, which is in category (B).
- (2) is the newest post of the category (A)
- (3) is the newest post of the category (B), also is (1)
Basically I am asking how I would do this: If (3) = (1), skip (3) and show 2nd newest post in the category (in this case category (B)).
Additional information about my blog specifically, while the information above is more general/universal.
In my blog I also have a category that is excluded from the blog and only shown on a specific page. How would I exclude this category from the whole solution for the initial question? Would it simply be enough to write 'cat' => -123,?
currently I am working on some customized blog templating and was wondering if there is any way to find out if the newest post of a category is also the newest post of the whole blog, so I can skip the first post of the category.
- (1) is the newest post of the whole blog, which is in category (B).
- (2) is the newest post of the category (A)
- (3) is the newest post of the category (B), also is (1)
Basically I am asking how I would do this: If (3) = (1), skip (3) and show 2nd newest post in the category (in this case category (B)).
Additional information about my blog specifically, while the information above is more general/universal.
In my blog I also have a category that is excluded from the blog and only shown on a specific page. How would I exclude this category from the whole solution for the initial question? Would it simply be enough to write 'cat' => -123,?
2 Answers
Reset to default 3So the easiest way to do this would be to store the ID of the first post (1) then in each of your category loops you can use the post__not_in property like so:
// inside the first loop at the top.
$latest_post_id = get_the_ID();
// WP_Query for fetching each category
$category_query = new WP_Query( [
// other parameters
'post__not_in' => [ $latest_post_id ],
] );
Now to exclude a category in WP_Query you can use category__not_in which takes an array of category ID's. It's definitely worth checking out the wordpress codex for WP_Query
Just use ‘post__not_in’ param in your second query.
$query1 = new WP_Query...
$used_posts = array();
while ( $query1->have_posts() ) :
$query1->the_post();
$used_posts[]= get_the_ID();
...
endwhile;
$query2 = new WP_Query( array(
'post__not_in' => $used_posts,
...
) );
本文标签: categoriesIf newest post of category is newest post in generalskip first post of category
版权声明:本文标题:categories - If newest post of category is newest post in general, skip first post of category 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749120326a2318896.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论