Request wp_remote_get from a dynamic url (not a json file) - wordpress

I'm currently trying to fetch data from an external source (not a Wp site) to a Wp site using the wp_remote_get method.
The site that i want to get data from doesn't have an actual json file to make the call instead they provide this link to make the request (https://fcc-weather-api.glitch.me/api/current?lat=35&lon=139.
I'm using the below code to make the call.
Is there a way to parse the response in php with html elements so i can properly display it on my website?
Any help would be appreciated.
$url = ' https://fcc-weather-api.glitch.me/api/current?lat=35&lon=139';
$request = wp_remote_get( $url );
if(is_wp_error($request)) {
return false;
} else {
$body = $request['body'];
}
echo $body;
$api_response = json_decode( wp_remote_retrieve_body( $response ), true );
The response
{ "coord":{ "lon":159, "lat":35 }, "weather":[ { "id":500, "main":"Rain", "description":"light rain", "icon":"https://cdn.glitch.com/6e8889e5-7a72-48f0-a061-863548450de5%2F10n.png?1499366021399" } ], "base":"stations", "main":{ "temp":22.59, "pressure":1027.45, "humidity":100, "temp_min":22.59, "temp_max":22.59, "sea_level":1027.47, "grnd_level":1027.45 }, "wind":{ "speed":8.12, "deg":246.503 }, "rain":{ "3h":0.45 }, "clouds":{ "all":92 }, "dt":1499521932, "sys":{ "message":0.0034, "sunrise":1499451436, "sunset":1499503246 }, "id":0, "name":"", "cod":200 }

You can use your $api response to display data. check below code. I did not display all data but can be displayed it by the print echo "<pre>"; print_r($api_response); echo "</pre>";
<?php
$url = 'https://fcc-weather-api.glitch.me/api/current?lat=35&lon=139';
$response = wp_remote_get( $url );
if( is_wp_error( $response ) ) {
return false;
} else {
$api_response = json_decode( wp_remote_retrieve_body( $response ), true );
}
?>
<div>
<h1>Weather reports</h1>
<div>
<label>Lattitude - </label> <?php echo $api_response['coord']['lat']; ?>,
<label>Longitude - </label> <?php echo $api_response['coord']['lon']; ?>
</div>
<div>
<h3>Weather</h3>
<label><?php echo $api_response['weather'][0]['main']; ?><img src="<?php echo $api_response['weather'][0]['icon']; ?>"> - <?php echo $api_response['weather'][0]['description']; ?></label>
</div>
<ul>
<li>Temp - <?php echo $api_response['main']['temp']; ?></li>
<li>Feels like - <?php echo $api_response['main']['feels_like']; ?></li>
<li>Temp min - <?php echo $api_response['main']['temp_min']; ?></li>
<li>Temp max - <?php echo $api_response['main']['temp_max']; ?></li>
<li>Pressure - <?php echo $api_response['main']['pressure']; ?></li>
<li>Humidity - <?php echo $api_response['main']['humidity']; ?></li>
</ul>
<div>
<h3>Wind</h3>
<ul>
<li>Speed - <?php echo $api_response['wind']['speed']; ?></li>
<li>Deg - <?php echo $api_response['wind']['deg']; ?></li>
<li>Gust - <?php echo $api_response['wind']['gust']; ?></li>
</ul>
</div>
</div>
The above code displayed something like this. you can modify based on your requirements.

Does this help? You had $response in there - which was never defined.
$url = ' https://fcc-weather-api.glitch.me/api/current?lat=35&lon=139';
$request = wp_remote_get( $url );
if(is_wp_error($request)) {
return false;
} else {
// Returns the $body as object
$body = json_decode(wp_remote_retrieve_body($request));
}
echo '<div class="weather">' . $body->weather[0]->main . '</div>';
echo '<pre>'; print_r( $body ); echo '</pre>';

Related

Thumbnail and attachment syntax on Wordpress

I use code like this, but unfortunately did not work. The error is the image failed to load, but the code display on the page.
<?php
$nextthumb = get_the_post_thumbnail($nextPost->ID);
$img_urltest = wp_get_attachment_url( $nextthumb,'full' );
$imagetest = aq_resize( $img_urltest, 500,325, true );
$thumb = get_post_thumbnail_id();
$img_url = wp_get_attachment_url( $thumb,'full' );
$image = aq_resize( $img_url, 500,325, true );
?>
<img src="
<?php if($image) {
echo $image;
}
else {
echo $nextthumb;
}
?>
"alt="<?php echo get_bloginfo('name'); ?>"/>

Custom layout for search result

In Wordpress 3.5.1 I want to create & use custom layout to display search result. I googled and found http://codex.wordpress.org/Creating_a_Search_Page but this is not exactly what I want.
Okay I solved my problem and now very happy :)
I write following script in search.php file
while ( have_posts() ) {
the_post();
//get_template_part( 'content', get_post_format() );
$permalink = get_permalink($post->ID);
$title = get_the_title($post->ID);
$feed = truncate( strip_tags( get_the_content($post->ID) ), 0, 300, "[...]");
echo '<div class="posts">';
echo '<div style="float:left">';
echo '<a class="featured-img">
'.the_post_thumbnail('thumbnail').'
</a>';
echo '</div>';
echo '<div class="posts-content">';
echo '<div class="heading">'.$title.'</div>';
echo '<div class="comments"> </div>'.
$feed
.'</div>';
echo '</div>';
}

Wordpress Gallery shortcode: Display one category ID only

Good Day. I am using a Gallery shortcode in my wordpress site to display images.
Problem is that I wasnt the gallery to show only images from a certain category, for example category id 35. How do I specify it in the shortcode?
Shortcode:
[custom_gallery style="1" source="**cat=%cat_id%**" link="image" description="0" size="200x200" limit="10"]
Now I have tried the following, but does not work -
cat=%35%
cat=%cat_id=35%
The shortcode code (because it is a custom shortcode):
/**
* Gallery posts shortcode
*/
function gallery_posts_func($atts, $content = null) {
extract(shortcode_atts(array(
"limit" => '5',
"cat" => '',
"thumb_width" => '',
"thumb_height" => '',
), $atts));
global $wp_query,$paged,$post;
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$query .= '&posts_per_page='.$limit;
$query .= '&post_type=gallery';
$query .= '&taxonomy=gallery_cat';
$query .= '&gallery_cat='.$cat;
$wp_query->query($query);
ob_start();
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<div class="gallery-holder fourthcol shortcode">
<?php
$gogo_gallery_video_url = get_post_meta($post->ID, 'gogo_gallery_video_url', true);
$gogo_gallery_links_to = get_post_meta($post->ID, 'gogo_gallery_links_to', true);
$gogo_gallery_title_links_to = get_post_meta($post->ID, 'gogo_gallery_title_links_to', true);
$gogo_gallery_custom_link = get_post_meta($post->ID, 'gogo_gallery_custom_link', true);
if ($gogo_gallery_title && $gogo_gallery_video_url && $gogo_gallery_title_links_to=="gallery_title_links_image") {
echo '<h5>';
echo '<a href="'.$gogo_gallery_video_url.'" rel="prettyPhoto[mixed]">';
echo ''.get_the_title().'';
echo '</a>';
echo '</h5>';
} elseif ($gogo_gallery_title && $gogo_gallery_title_links_to=="gallery_title_links_image") {
echo '<h5>';
echo '<a href="'.$thumbnail[0].'" rel="prettyPhoto[mixed]">';
echo ''.get_the_title().'';
echo '</a>';
echo '</h5>';
} elseif ($gogo_gallery_title && $gogo_gallery_title_links_to=="gallery_title_links_content") {
echo '<h5>';
echo '<a href="'.get_permalink().'">';
echo ''.get_the_title().'';
echo '</a>';
echo '</h5>';
} elseif ($gogo_gallery_title && $gogo_gallery_title_links_to=="gallery_title_links_link") {
echo '<h5>';
echo '<a href="'.$gogo_gallery_custom_link.'">';
echo ''.get_the_title().'';
echo '</a>';
echo '</h5>';
} elseif ($gogo_gallery_title) {
echo '<h5>';
echo ''.get_the_title().'';
echo '</h5>';
} else {
echo '';
}
?>
<div class="gallery-box">
<div class="gallery-image prettygallery">
<?php if (has_post_thumbnail()) { ?>
<?php
$thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id(), 'large');
if ($gogo_gallery_video_url && $gogo_gallery_links_to=="gallery_links_image") {
echo '<a href="'.$gogo_gallery_video_url.'" rel="prettyPhoto[mixed]">';
echo '<img src="'.get_template_directory_uri().'/timthumb.php?src='.$thumbnail[0].'&w='.$thumb_width.'&h='.$thumb_height.'&zc=1&q=100&s=1" alt="'.get_the_title().'" />';
echo '</a>';
} elseif ($gogo_gallery_links_to=="gallery_links_image") {
echo '<a href="'.$thumbnail[0].'" rel="prettyPhoto[mixed]">';
echo '<img src="'.get_template_directory_uri().'/timthumb.php?src='.$thumbnail[0].'&w='.$thumb_width.'&h='.$thumb_height.'&zc=1&q=100&s=1" alt="'.get_the_title().'" />';
echo '</a>';
} elseif ($gogo_gallery_links_to=="gallery_links_content") {
echo '<a href="'.get_permalink().'">';
echo '<img src="'.get_template_directory_uri().'/timthumb.php?src='.$thumbnail[0].'&w='.$thumb_width.'&h='.$thumb_height.'&zc=1&q=100&s=1" alt="'.get_the_title().'" />';
echo '</a>';
} elseif ($gogo_gallery_links_to=="gallery_links_link") {
echo '<a href="'.$gogo_gallery_custom_link.'">';
echo '<img src="'.get_template_directory_uri().'/timthumb.php?src='.$thumbnail[0].'&w='.$thumb_width.'&h='.$thumb_height.'&zc=1&q=100&s=1" alt="'.get_the_title().'" />';
echo '</a>';
} else {
echo '<img src="'.get_template_directory_uri().'/timthumb.php?src='.$thumbnail[0].'&w='.$thumb_width.'&h='.$thumb_height.'&zc=1&q=100&s=1" alt="'.get_the_title().'" />';
}
?>
<?php } ?>
</div>
<?php if ($gogo_gallery_short_desc) { ?><em><?php echo $gogo_gallery_short_desc; ?></em><?php } ?>
</div>
</div>
<?php endwhile; ?>
<?php $wp_query = null; $wp_query = $temp;
$content = ob_get_contents();
ob_end_clean();
return $content;
}
add_shortcode("gallery_posts", "gallery_posts_func");
What you are looking for are probably:
cat="35"

How do I use an individual checkbox value from a wordpress meta box?

I'm trying to use a metabox checkbox to hide/display on hover. The problem is it shows the div if any of the posts have the check box checked. I need it to toggle the div "on" & "off" depending on if the checkbox is checked. Any help would be greatly appreciated.
Here is the code in my functions.php:
`// Checkbox Meta
add_action("admin_init", "checkbox_init");
function checkbox_init(){
add_meta_box("checkbox", "Check to Show Bubbles", "checkbox", "homefeature", "normal", "high");
}
function checkbox(){
global $post;
$custom = get_post_custom($post->ID);
$field_id = $custom["field_id"][0];
echo '<label>Show Bubbles?</label>';
$field_id_value = get_post_meta($post->ID, 'field_id', true);
if($field_id_value == "yes") {
$field_id_checked = 'checked="checked"';
}
echo ' <input type="checkbox" name="field_id" value="yes" '.$field_id_checked.' />';
}
// Save Meta Details
add_action('save_post', 'save_details');
function save_details(){
global $post;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post->ID;
}
update_post_meta($post->ID, "field_id", $_POST["field_id"]);
}
function custom_content($id) {
$field_id = get_post_meta($id, 'field_id', true);
if($field_id == yes) {
echo '<script type="text/javascript">';
echo '$("this").mouseover(function() {';
echo "$('#mainFeatureFlashBG').css({'display' : 'block'})";
echo '}</script>';
}
else{
echo '<script type="text/javascript">';
echo '$("this").mouseover(function() {';
echo "$('#mainFeatureFlashBG').css({'display' : 'none'})";
echo '}</script>';
}
}`
Here is my php:
<ul>
<!-- Begin Miller Beer Logo Query-->
<?php
$args=array(
'beerlogo'=>'miller',
'post_type' => 'homefeature',
'post_status' => 'publish',
'posts_per_page' => -1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li class="image-rollover"><a href="<?php echo esc_url(get_post_meta($post->ID, 'homefeature_custom_link', true));
?>"><?php the_post_thumbnail('full'); ?>
<?php
//Is the Checkbox for Bubbles Checked?
custom_content(get_the_ID());
?>
</a>
</li>
<?php
endwhile;
}
wp_reset_query();
?>
</ul>
You should check with:
<?php
if(get_post_meta($post->ID,'field_id',true) != '') {
// do something, output div
} ?>
See function reference here:
http://codex.wordpress.org/Function_Reference/get_post_meta

Wordpress post query php custom field conditional

Here's the situation:
In wordpress I'm trying to reset a post WP_Query so that I can rewrite the post link based on whether or not a custom field exists in the post. I'm trying to give the post a NEW link in the custom field.
All I've managed to do here is kill the link entirely. Any and all help is greatly appreciated, I'm pretty green to php.
Here's my WP_Query:
<?php
$recentPosts = new WP_Query();
$recentPosts->query('showposts=3');
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<?php
$attribute = the_title_attribute();
$title = the_title();
$key = 'NewPostLink';
$newLink = get_post_meta( $post->ID, $key, TRUE );
if ($newLink != '') {
$theLink = get_permalink ($post->ID );
if (has_post_thumbnail()) {
$image = get_the_post_thumbnail( $post->ID );
echo '<div class="thumbnailbox"><div class="thumbnail">'.$image.'</div></div>';
echo '<h2>'.$title.'</h2>';
} else {
echo '<h2>'.$title.'</h2>';
}
} else {
$theLink = $newLink;
if (has_post_thumbnail()) {
$image = get_the_post_thumbnail( $post->ID );
echo '<div class="thumbnailbox"><div class="thumbnail">'.$image.'</div></div>';
echo '<h2>'.$title.'</h2>';
} else {
echo '<h2>'.$title.'</h2>';
}
}
?>
<small><?php the_time('F jS, Y') ?></small>
<div class="entry">
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>
I think this is what you need. It's hard to tell. I suppose that the first part of the if statement is what runs if there is no custom post meta? I couldn't tell. Here's what the problem was. The if statement ran the first part if there IS a value returned for the custom post meta, otherwise it ran the second part, using the empty string as the href. (The first part runs if the custom value either doesn't exist or is anything but an empty string). Changing the if statement to check if it's empty is better because it will catch it if it doesn't exist (returns false), or if it does exist but is an empty string (declared but not defined).
I've marked what I edited with comments (just one line).
<?php
$recentPosts = new WP_Query();
$recentPosts->query('showposts=3');
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<?php
$attribute = the_title_attribute();
$title = the_title();
$key = 'NewPostLink';
$newLink = get_post_meta( $post->ID, $key, TRUE );
/* EDITED */ if (empty($newLink)) {
$theLink = get_permalink ($post->ID );
if (has_post_thumbnail()) {
$image = get_the_post_thumbnail( $post->ID );
echo '<div class="thumbnailbox"><div class="thumbnail">'.$image.'</div></div>';
echo '<h2>'.$title.'</h2>';
} else {
echo '<h2>'.$title.'</h2>';
}
} else {
$theLink = $newLink;
if (has_post_thumbnail()) {
$image = get_the_post_thumbnail( $post->ID );
echo '<div class="thumbnailbox"><div class="thumbnail">'.$image.'</div></div>';
echo '<h2>'.$title.'</h2>';
} else {
echo '<h2>'.$title.'</h2>';
}
}
?>
<small><?php the_time('F jS, Y') ?></small>
<div class="entry">
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>

Resources