Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

wordpress foreach - getting links from the database

Featured Replies

Hi All,

 

Just wondering if anyone can help me to do the following:

 

I am getting the links from the wordpress database and now would like to group 2 links at a time in <ul>'s

 

Not sure how I can do a count with a foreach

 

	foreach ($links as $link) {
 	$linkurl=$link->link_url;
 	$linkname=$link->link_name;


 	echo "<ul><li><a href='$linkurl' target='_blank'><h5>$linkname</h5></a></li>";
 	echo "</ul>";
}

 

Thanks

Edited by ayoungh

  • Author

D'oh. I feel really thick now - I've always done it by if(is_int($count / 2)).

 

I'm not sure how I would do this to be honest. in the foreach loop how do I get every second item with that?

foreach ($links as $key => $link) {  
$linkurl=$link->link_url;
$linkname=$link->link_name;
echo (($key % 2 == 0) ? '<ul><li><a href='.$linkurl.' target="_blank"><h5>' : '$linkname</h5></a></li></ul>');	
}

  • Author

The only problem with the above is that I dont get 2 list items to each ul?

Sorry, I totally misunderstood what you meant in the original post. Not quite sure what I was thinking with that last bit of code! It should have been:

 

foreach ($links as $key => $link) {  
       $linkurl=$link->link_url;
       $linkname=$link->link_name;
       echo (($key % 2 == 0) ? '<ul><li><a href="'.$linkurl.'" target="_blank"><h5>'.$linkname.'</h5></a></li>' : '<li><a href="'.$linkurl.'" target="_blank"><h5>'.$linkname.'</h5></a></li></ul>');  
}

 

However, that's a very quick and dirty way of doing it which will go wrong if there isn't an even number of links in the array. Something like this would be better:

 

$total = count($links);
foreach ($links as $key => $link) {  
       $linkurl=$link->link_url;
       $linkname=$link->link_name;
if($key % 2 == 0){
	echo "<ul>\n";
}
echo "<li><a href=\"".$linkurl."\" target=\"_blank\"><h5>".$linkname."</h5></a></li>\n";
if($key % 2 == 1 || ($key + 1) == $total){
	echo "</ul>\n";
}
}

Edited by Spitfire

  • Author

Sorry, I totally misunderstood what you meant in the original post. Not quite sure what I was thinking with that last bit of code! It should have been:

 

foreach ($links as $key => $link) {  
       $linkurl=$link->link_url;
       $linkname=$link->link_name;
       echo (($key % 2 == 0) ? '<ul><li><a href="'.$linkurl.'" target="_blank"><h5>'.$linkname.'</h5></a></li>' : '<li><a href="'.$linkurl.'" target="_blank"><h5>'.$linkname.'</h5></a></li></ul>');  
}

 

However, that's a very quick and dirty way of doing it which will go wrong if there isn't an even number of links in the array. Something like this would be better:

 

$total = count($links);
foreach ($links as $key => $link) {  
       $linkurl=$link->link_url;
       $linkname=$link->link_name;
if($key % 2 == 0){
	echo "<ul>\n";
}
echo "<li><a href=\"".$linkurl."\" target=\"_blank\"><h5>".$linkname."</h5></a></li>\n";
if($key % 2 == 1 || ($key + 1) == $total){
	echo "</ul>\n";
}
}

 

Perfect, Appreciate it :)

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.