Skip to content
Advertisement

Option selection and return specific object

I have two different methods that output tweets but from two different sources.

$tweets = Twitter::get_tweets();
$lists = Twitter::get_user_first_list_tweets();

Both return objects that can be accessed using ->data for an array of tweets.

So I have $tweets_option = get_field('show_tweets_for'); which outputs either profile or list.

Then I have a loop that goes through:

<?php if ($tweets): ?>
   <?php foreach ($tweets->data as $tweet): ?>
   <?php endforeach; ?>
<?php endif; ?>

What’s the best way for me to define it like this:

  • By default, ‘profile’ is selected on $tweets_option, which I want to output $tweets = Twitter::get_tweets();.
  • Let’s say someone selects ‘list’ from $tweets_option, how can I set $lists to equal $tweets so that it still runs through the tweets?

Thanks all!

Advertisement

Answer

$tweets_option = get_field('show_tweets_for'); 

$tweets =  $tweets_option == 'profile' ? Twitter::get_tweets() : Twitter::get_user_first_list_tweets();
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement