Only show first image in foreach loop
Depending of what you are exactly trying to achieve accessing the first element of the $posts array may work for you. No need for the foreach loop. echo ‘<img src=”‘.$posts[0][‘img’][‘url’].'”>’;
Depending of what you are exactly trying to achieve accessing the first element of the $posts array may work for you. No need for the foreach loop. echo ‘<img src=”‘.$posts[0][‘img’][‘url’].'”>’;
Your call to get_user_by() has a small hiccup in it. It needs to be login rather than user_login // Search these Usernames $usernames = array(‘user1’, ‘user2’); // Fetch the User IDs $prof_ids = array(); foreach ($usernames as $prof_id) { $user = get_user_by(‘login’, $prof_id); $prof_ids[] = $user->ID; } // WP_User_Query arguments $args = array( ‘include’ => … Read more
You can’t order by values inside serialised arrays in post meta. When you store an array like this: $value = [ [ ‘plan_name’ => ‘name 1’, ‘plan_price’ => ‘1’, ‘theType’ => ‘yesPaid’, ‘plan_space’ => ‘Unlimited’, ], [ ‘plan_name’ => ‘name 2’, ‘plan_price’ => ‘3’, ‘theType’ => ‘yesFree’, ‘plan_space’ => ‘5000’, ], [ ‘plan_name’ => ‘name2’, … Read more
$boat_safety_terms_cleaned_sorted = sort( $boat_safety_terms_cleaned ); error_log( print_r( $boat_safety_terms_cleaned_sorted, TRUE ) ); PHP’s sort accepts an array reference and sorts in-place, and returns success or failure – hence the 1 you’re getting. You want: sort( $boat_safety_terms_cleaned ); error_log( print_r( $boat_safety_terms_cleaned, TRUE ) ); Also, I’m assuming I’ll want to use update_post_meta to save the array back … Read more
So this is different from your code (and the one in the accepted answer to the other question), but it worked well for me: You can remove the “\t” . and . “\n” which I added for pretty formatting purposes so that you could better inspect the generated HTML markup. And let me know if … Read more
Here’s one way to solve this: sort both arrays loop through them and compare them item by item (they should be equal, because arrays are sorted) . sort( $evaltypesReq ); sort( $evaltypesSch ); $containsAllValues = true; foreach ( $evaltypesReq as $k => $v ) { if ( $evaltypesSch[$k] !== $v ) $containsAllValues = false; }
In it’s current state you would directly get the value from the associative PHP array by directly referencing the key like so: var_dump( $form_data[‘key’] ); It’s wise to note that when you dump variables using var_dump you get information on the value type, and length. So in this case you get string(33) which indicates the … Read more
You can use get_category_link() for that: $allCat = get_the_category(); if( ! empty( $allCat ) ){ $lastCat = array_reverse( $allCat ); $last_cat_link = get_category_link( $lastCat[0] ); if( ! is_wp_error( $last_cat_link ) ){ echo ‘<a href=”‘ . $last_cat_link . ‘”>’ . $lastCat[0]->name . ‘</a>’; } } https://developer.wordpress.org/reference/functions/get_category_link/
To get the post author ID you can try: $revisions = wp_get_post_revisions($post->ID); //For first author $first_author = $revisions[0]->post_author; //to get all revision authors foreach( $revisions as $revision ){ echo $revision->post_author; }
No, that’s because you are treating an array as an object and PHP doesn’t convert those types automatically. To get an individual element from an object you use: $my_object->Element1; To get an individual element from an array you use: $my_array[Element1]; You can even nest arrays. So if you have $my_array, where each element is another … Read more