admin管理员组文章数量:1278950
Let's say I have this Query to which I fetch all my WP posts and then use PHP to filter out even further:
<?php
$posts = get_posts(array(
'tax_query' => array(
array(
'taxonomy' => 'type',
'field' => 'slug',
'terms' => array( 'campaign' ),
),
),
'post_type' => 'post',
'posts_per_page' => 999999999999999999,
'orderby' => 'rand',
));
if( $posts): ?>
<?php foreach( $posts as $post ): setup_postdata( $post ) ?>
// Filter out users with funds with PHP
<?php
$post_id = get_the_ID();
$advertiser_id = get_post_field( 'post_author', $post_id );
$advertiser_userfunds=get_user_meta( $advertiser_id, 'userfunds' , true );
if ($advertiser_userfunds > 0):?>
//5 posts left in the loop after filtering out users with funds
<?php endif;?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
So let's say I'm left with a loop of 5 posts now.
My question is, is there any way to show just 1 post?
It can be in a random order, or simply showing first or last post from the loop?
I know there's a php code if in_array()
. Maybe I could implement this somehow?
Desperately need help.
Let's say I have this Query to which I fetch all my WP posts and then use PHP to filter out even further:
<?php
$posts = get_posts(array(
'tax_query' => array(
array(
'taxonomy' => 'type',
'field' => 'slug',
'terms' => array( 'campaign' ),
),
),
'post_type' => 'post',
'posts_per_page' => 999999999999999999,
'orderby' => 'rand',
));
if( $posts): ?>
<?php foreach( $posts as $post ): setup_postdata( $post ) ?>
// Filter out users with funds with PHP
<?php
$post_id = get_the_ID();
$advertiser_id = get_post_field( 'post_author', $post_id );
$advertiser_userfunds=get_user_meta( $advertiser_id, 'userfunds' , true );
if ($advertiser_userfunds > 0):?>
//5 posts left in the loop after filtering out users with funds
<?php endif;?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
So let's say I'm left with a loop of 5 posts now.
My question is, is there any way to show just 1 post?
It can be in a random order, or simply showing first or last post from the loop?
I know there's a php code if in_array()
. Maybe I could implement this somehow?
Desperately need help.
Share Improve this question edited Sep 24, 2021 at 20:34 robert0 asked Sep 24, 2021 at 20:24 robert0robert0 2032 silver badges11 bronze badges 5 |1 Answer
Reset to default 0Solved the problem by inserting this:
<?php if (in_array($counter, array(0)) ) :?>
// show content
<?php $counter++; endif;?>
The above basically displays only 1 post that is first in the array.
In case anyone needs to solve a problem like this.
本文标签: phpDisplay just one post from a loop of 5 posts
版权声明:本文标题:php - Display just one post from a loop of 5 posts? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741301731a2371126.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
break
keyword? – Ivan Shatsky Commented Sep 24, 2021 at 21:10orderby => 'rand'
is generally a bad idea, for what it's worth. – vancoder Commented Sep 24, 2021 at 22:051
post, why are you asking for999999999999999999
? This query is extremely slow/expensive – Tom J Nowell ♦ Commented Sep 24, 2021 at 22:30