How can I use an array of objects with multiple PHP functions?

I’m a bit stuck with my PHP to make something work for a WordPress site that I am developing.

What I want to do is have an array of objects pertaining to social networks that I can pass through multiple functions so I can display them in multiple locations within the same page.

I started working with a solution that is found here but am now stuck: https://stackoverflow.com/a/8612289

The following code works for displaying this information in one location but only once on a page, and I can’t sort out how to pass this array through multiple functions. All of the same data from the array will be used to display social network icons in the header and then again in the sidebar with a slightly different layout of the data and CSS based on the UL class that the LI elements are passed through. I’d also like to be able to show the sidebar function multiple times for long scrolling pages, showing it higher up and again towards the bottom.

Ideally, this would go into functions.php so then the function can be echoed from the corresponding template files.

class Social_Icon {
public $Social_Network;
public $Social_Slogan;
public $Social_URL;
public $Social_CSS;
}

$Social_Twitter = new Social_Icon();
$Social_Twitter->Social_Network = ‘Twitter’;
$Social_Twitter->Social_Slogan = ‘Follow me on Twitter!’;
$Social_Twitter->Social_URL = ‘https://twitter.com/username’;
$Social_Twitter->Social_CSS = ‘tw’;
$Social_Facebook = new Social_Icon();
$Social_Facebook->Social_Network = ‘Facebook’;
$Social_Facebook->Social_Slogan = ‘Like my page on Facebook!’;
$Social_Facebook->Social_URL = ‘https://facebook.com/page’;
$Social_Facebook->Social_CSS = ‘fb’;
$Social_Instagram = new Social_Icon();
$Social_Instagram->Social_Network = ‘Instagram’;
$Social_Instagram->Social_Slogan = ‘Follow me on Instagram!’;
$Social_Instagram->Social_URL = ‘https://instagram.com/username’;
$Social_Instagram->Social_CSS = ‘ig’;

$Social_Icons = array( $Social_Twitter, $Social_Facebook, $Social_Instagram );

foreach ($Social_Icons as $Social_Item) {
echo ‘<li><a class=”‘ . $Social_Item->Social_CSS . ‘” alt=”‘ . get_bloginfo(‘name’) . ‘ on ‘ . $Social_Item->Social_Network . ‘” title=”‘ . get_bloginfo(‘name’) . ‘ on ‘ . $Social_Item->Social_Network . ‘” href=”‘ . $Social_Item->Social_URL . ‘” target=”blank”></a></li>’;
}

Read more here:: How can I use an array of objects with multiple PHP functions?

Leave a Reply

Your email address will not be published. Required fields are marked *