OK, I'm trying to integrate an API that lists adoptable pets into a wordpress website. I've done lots of googling and read through tutorials, and so far have managed to put together a super basic plugin that seems to do what I'm trying to accomplish. Currently I'm trying to pull in an image, but just the first image. Each animal may have 5 images associated with it, but I only want to pull in the first (default). Currently my code brings them all. Now I realize the problem is that I'm using "foreach()". But, this is new to me and my googling is not going well, and any other way I've tried to do it is just not getting me ANY pictures. Any advice is appreciated....and if I'm doing anything else wrong, feel free to let me know :) I also need to figure out how to paginate it, but I'm thinking that's a separate question! Thanks!
<?php
add_shortcode('pets', 'petsshortcode');
function petsshortcode() {
$request = wp_remote_get( 'https://petstablished.com/api/v2/public/pets?public_key=UlEK4EWvDAoOjXeQXSCQZAyBywWfqfOg&search[status]=available,foster&pagination[limit]=20&pagination[page]=1' );
if( is_wp_error( $request ) ) {
return false; // Bail early
}
$body = wp_remote_retrieve_body( $request );
$data = json_decode( $body );
if( ! empty( $data ) ) {
foreach( $data->collection as $collection ) {
echo '<div id="pet-block"><ul class="pet-profile"><li class="pet-name">'. $collection->name; echo '</li>';
echo '<li class="pet-meta">'.'<span class="sex">' . $collection->sex; echo '</span>' . '<span class="breed">' . $collection->breed; echo '</span><span class="age">' . $collection->age; echo '</span></li>';
echo '<li><p>' . $collection->description; echo '</li></p>';
foreach( $collection->images as $images ) {
echo '<div class="pet-photo"><img src="' . $images->image->url; echo '" width="200"></div>';}
echo '</ul></div>';
}
}
}
You don't need nested foreach loops in this case, simply use just one foreach loop like this:
// your code
foreach( $data->collection as $collection ) {
echo '<div id="pet-block"><ul class="pet-profile"><li class="pet-name">'. $collection->name; echo '</li>';
echo '<li class="pet-meta">'.'<span class="sex">' . $collection->sex; echo '</span>' . '<span class="breed">' . $collection->breed; echo '</span><span class="age">' . $collection->age; echo '</span></li>';
echo '<li><p>' . $collection->description; echo '</li></p>';
echo '<div class="pet-photo"><img src="' . $collection->images[0]->image->url; echo '" width="200"></div>';
echo '</ul></div>';
}
// your code
Related
I am displaying a list of filters using Isotope. The functionality works when the first part of the code works however, when the screen is resized the posts don't filter when it executes the last part... Any help is much appreciated!
<?php
$filterLinks = array();
$newarray = array();
$starter = array('name'=>'View All','slug'=>'*');
$filterLinks[] = $starter;
$taxterms = get_terms( $customTax );
if ( ! empty( $taxterms ) && ! is_wp_error( $taxterms ) ){
foreach ( $taxterms as $taxterm ) {
$datafilter = '.' . $taxterm->slug;
$newarray = array(
'name' => $taxterm->name,
'slug' => $datafilter,
);
$filterLinks[] = $newarray;
}
}
echo '<ul id="filters" class="desk-filters button-group">' ."\n";
foreach ($filterLinks as $links) {
echo '<li><button class="button" data-filter="' . $links['slug'] . '">' . $links['name'] . '</button></li>'."\n";
}
echo '</ul>';
// Drop down menu on mobile
echo '<div id="filters" class="resp-filters button-group">'."\n";
echo '<div class="resp-filter-btn">Select Filter</div>'."\n";
echo '<div class="resp-filter-content">'."\n";
foreach ($filterLinks as $links) {
echo '<a class="button" data-filter="' . $links['slug'] . '">' . $links['name'] . '</a>'."\n";
}
echo '</div>'."\n";
echo '</div>';
?>
Functionality Works:
echo '<ul id="filters" class="desk-filters button-group">' ."\n";
foreach ($filterLinks as $links) {
echo '<li><button class="button" data-filter="' . $links['slug'] . '">' . $links['name'] . '</button></li>'."\n";
}
echo '</ul>';
Functionality Doesn't Work:
// Drop down menu on mobile
echo '<div id="filters" class="resp-filters button-group">'."\n";
echo '<div class="resp-filter-btn">Select Filter</div>'."\n";
echo '<div class="resp-filter-content">'."\n";
foreach ($filterLinks as $links) {
echo '<a class="button" data-filter="' . $links['slug'] . '">' . $links['name'] . '</a>'."\n";
}
echo '</div>'."\n";
echo '</div>';
Is this because i'm executing it twice, just hiding one on desktop and showing the other on mobile?
Looking for some direction. Thanks in advance!
You're using id="filters" twice, which is improper use of the id attribute, since it should be unique. Not sure if that is of any concern?
I am trying to display a grid list of registered users under specific roles such as subscriber, editor, etc with custom metadata. I've started with this below code but it only displays the avatar and the name when need to display some more data of each user such as website link, description, email.
Here is the code snippet I am using-
function wpb_recently_registered_users() {
global $wpdb;
$recentusers = '<ul class="recently-user">';
$usernames = $wpdb->get_results("SELECT user_nicename, user_url, user_email FROM $wpdb->users ORDER BY ID DESC LIMIT 5");
foreach ($usernames as $username) {
if (!$username->user_url) :
$recentusers .= '<li>' . get_avatar($username->user_email, 45) . '' . $username->user_nicename . "</li>";
else :
$recentusers .= '<li>' . get_avatar($username->user_email, 45) . '' . $username->user_nicename . "</li>";
endif;
}
$recentusers .= '</ul>';
return $recentusers;
}
add_shortcode('wpb_newusers', 'wpb_recently_registered_users');
I need to get these user meta key data also -
mepr_email, mepr_your_bio, mepr_youtube_channel
but not sure how to integrate with the above code snippets. Can anyone help me find a better solution, please?
So I am using this code snippet and getting all the required data but I am not sure how to run this as a loop so as soon as a new user register it will get the data like posts query? Sorry I have limited knowledge in this.
'function user_data_code(){
$blogusers = get_users( array( 'search' => 'admin' ) );
// Array of WP_User objects.
foreach ( $blogusers as $user ) {
echo '<span>' . get_avatar($user->user_email, 45) . '</span> <br>';
echo '<span>' . esc_html( $user->user_email ) . '</span> <br>';
echo '<span>' . esc_html( $user->display_name ) . '</span> <br>';
echo '<span>' . esc_html( $user->first_name ) . '</span> <br>';
echo '<span>' . esc_html( $user->mepr_bio ) . '</span> <br>';
echo '<span>' . esc_html( $user->mepr_website_link) . '</span>';
}
}
add_shortcode('wpb_newusers', 'user_data_code');'
I am using Advanced Custom Fields plugin on a WordPress site. I have a repeater field with 25+ quotes that I am displaying on a per page basis. Each page has unique quotes.
The code I currently have to pull one random quote from that pool of 25 works just as it is suppose to when I am logged in. When I log out, it does pull one random quote but will not switch it out to a different one on page refresh like it does when I am logged in.
I am using this random row code from here:
https://gist.github.com/wesrice/1924934
<?php
if (is_singular('post')) {
$repeater2 = get_field( 'quote_info', 11 );
$random_rows2 = array_rand( $repeater2, 1 );
if( is_array( $random_rows2 ) ){
foreach( $random_rows2 as $random_row2 ){
echo '<div class="newquote">
<div class="newinnerwrapperquote">"' . $repeater2[$random_row2]['bottom_quote_text'] . '"</div>';
echo '<div class="newquoteby">' . $repeater2[$random_row2]['bottom_quote_author'] . '</div></div>';
}
} else {
echo '<div class="newquote">
<div class="newinnerwrapperquote">"' . $repeater2[$random_rows2]['bottom_quote_text'] . '"</div>';
echo '<div class="newquoteby">' . $repeater2[$random_rows2]['bottom_quote_author'] . '</div></div>';
} wp_reset_query();
} else {
$repeater = get_field( 'quote_info' );
$random_rows = array_rand( $repeater, 1 );
if( is_array( $random_rows ) ){
foreach( $random_rows as $random_row ){
echo '<div class="newquote">
<div class="newinnerwrapperquote">"' . $repeater[$random_row]['bottom_quote_text'] . '"</div>';
echo '<div class="newquoteby">' . $repeater[$random_row]['bottom_quote_author'] . '</div></div>';
}
} else {
echo '<div class="newquote">
<div class="newinnerwrapperquote">"' . $repeater[$random_rows]['bottom_quote_text'] . '"</div>';
echo '<div class="newquoteby">' . $repeater[$random_rows]['bottom_quote_author'] . '</div></div>';
}
}
?>
The first if statement is if it is on a WordPress post rather than a page. I hope I explained this well.
Internal caching plugin was activated. Turned it off and works now when not logged in.
I have this complex loop:
<?php
$args = array(
'cat' => 54,
'order' => 'ASC',
'posts_per_page' => -1
);
$query = new WP_Query($args);
$q = array();
while ( $query->have_posts() ) {
$query->the_post();
$a = '<h2>' . get_the_title() .'</h2>'
. get_the_post_thumbnail() .
'<p>' . get_the_content("...plačiau") . '</p>';
$categories = get_the_category();
foreach ( $categories as $key=>$category ) {
$b = '<h1 class="thetitle">' . $category->name . '<span>Išskleisti <i class="fa fa-arrow-circle-down"></i></span></h1>';
}
$q[$b][] = $a; // Create an array with the category names and post titles
}
/* Restore original Post Data */
wp_reset_postdata();
foreach ($q as $key=>$values) {
echo $key;
echo '<div class="straipsniai">';
foreach ($values as $value){
if (count($values) == 1) {
echo '<div class="vienas">' . $value . '</div>';
} else if (count($values) == 2) {
echo '<div class="du">' . $value . '</div>';
} else if (count($values) == 3) {
echo '<div class="trys">' . $value . '</div>';
} else {
echo '<div>' . $value . '</div>';
}
}
echo '</div>';
}
?>
Which worked for me, gave me this nice list/accordion:
http://bruzienesklinika.lt/web/gydytojai/
Now, each person in the category has some articles as posts and I want a list of their articles under their description. (basic Title + exerpt + read more link)
I've tried to do this by using "List category posts" plugin which allows me to use [catlist id=24] shortcode, but the problem is that browser prints it as plain text, source shows [catlist id=24](You can open the most bottom "GYDYTOJA REUMATOLOGĖ" tab to see that). The shortcode does work inside page, which is rendered by single.php, but it does not show when rendered in the loop I gave you in the beginning of the question.
SO, the question is, how do I make the shortcode work in the initial list, where all the categories are listed with posts inside the accordion.
Now it is not the problem of this certain plugin because no shortcodes work in that accordion list.
Or maybe you have an idea how to do this the other way?
This is a wordpress question. I am trying to use a bit of code that works just fine on my home page on my inner page templates:
query_posts('cat=4');
// The Loop
echo '<div id="cal_details"><ul>';
while ( have_posts() ) : the_post();
$cal_date_j = date('j', intval(get_post_meta($post->ID, 'date_value', true)));
$cal_date_n = date('n', intval(get_post_meta($post->ID, 'date_value', true)));
$my_array[] = date('j, n', intval(get_post_meta($post->ID, 'date_value', true)));
$issetdate = get_post_meta($post->ID, 'date_value', true);
if (isset($issetdate)) {
echo '<li class="cal_event_li list_item_' . $cal_date_j . '_' . $cal_date_n . '">';
echo '<a href="' . get_permalink() . '">';
the_title();
echo '</a></li>';
}
endwhile;
echo '</ul></div>';
However, this doesn't seem to work on the inner-pages. All the titles links are being outputted correctly but it won't print the get_post_meta part correctly.
The list items all display something like <li class="cal_event_li list_item_1_1">
I think there is perhaps some issue with the way I have tried to use $post->ID but Im not sure whats going on here. Any ideas?
When you use query_posts you have to call global $post to get the post_meta. If you're only calling one category why don't you just use an archive template?
Also if you're going to use query_posts make sure you reset the query afterwords so plugins, sidebars, etc can still interact with the loop for conditionals etc..
global %post;
query_posts('cat=4');
// The Loop
//more stuff
endwhile;
wp_reset_query();
try replacing $post->ID with the_ID() in the inner pages. something like this
query_posts('cat=4');
// The Loop
echo '<div id="cal_details"><ul>';
while ( have_posts() ) : the_post();
$cal_date_j = date('j', intval(get_post_meta(the_ID(), 'date_value', true)));
$cal_date_n = date('n', intval(get_post_meta(the_ID(), 'date_value', true)));
$my_array[] = date('j, n', intval(get_post_meta(the_ID(), 'date_value', true)));
$issetdate = get_post_meta(the_id(), 'date_value', true);
if (isset($issetdate)) {
echo '<li class="cal_event_li list_item_' . $cal_date_j . '_' . $cal_date_n . '">';
echo '<a href="' . get_permalink() . '">';
the_title();
echo '</a></li>';
}
endwhile;
echo '</ul></div>';