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'); ?>"/>
Related
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>';
I am running a WordPress website with both traditional and AMP pages. Both versions are using GTM, but the AMP version is not able to find my custom variables. The AMP pages are validating, so I think there is an issue with how I am defining my custom variables
I am using the following code to define the variables on AMP pages. Is there anything out of place that I am missing. to see an example of an AMP page visit https://cellculturedish.com/evolution-stem-cell-culture-cell-derived-extracellular-matrices/amp/.
<body>
<!-- Google Tag Manager -->
<amp-analytics config="https://www.googletagmanager.com/amp.json?id=GTM-xxxxxxxxx.url=SOURCE_URL" data-credentials="include">
<?php if ( is_singular() ) { ?>
<?php global $post; ?>
<?php $categories = get_the_category( $post->ID ); ?>
<?php $primary_category = $categories[0]; ?>
<?php $post_sponsorship = get_field('post_sponsorship', $post->ID); ?>
<?php $is_sponsored = $post_sponsorship['is_sponsored']; ?>
<?php $author = 'Multiple Authors'; ?>
<?php $author_options = get_field('author_options', $post->ID); ?>
<?php if ( $author_options == 'ccd' && get_field('ccd_author') ) { ?>
<?php $author_obj = get_field('ccd_author', $post->ID); ?>
<?php $author = get_userdata( $author_obj ); ?>
<?php $author = $author->data->display_name; ?>
<?php } elseif ( $author_options == 'guest_post' && count( get_field('contributing_experts', $post->ID) ) == 1 ) { ?>
<?php $author_array = get_field('contributing_experts', $post->ID); ?>
<?php $author = get_expert_title( $author_array[0] ); ?>
<?php } ?>
<script type="application/json">
{
"vars": {
"publishDate": "<?php echo get_the_date('', $post->ID); ?>",
"postCategory":"<?php echo $primary_category->name; ?>",
"isSponsored": "<?php echo $sponsored = $post_sponsorship['is_sponsored'] == true ? 'true' : 'false'; ?>",
<?php if ( $post_sponsorship['is_sponsored'] == true && $post_sponsorship['sponsor'] != null ) { ?>
"postSponsor": "<?php echo get_the_title( $post_sponsorship['sponsor'] ); ?>",
<?php } ?>
"postAuthor": "<?php echo $author; ?>"
}
}
</script>
<?php } ?>
Create AMP Variables in GTM for all the custom dimensions you have under "vars" like so:
Next go to your GA Tag, under "more settings" > "custom dimensions" add in the correct index number for your custom dimensions and the amp variable name you just created for them:
Sir,
I have this code to show all post of category and thumbnail for 1st post of them.
<?php $recent = new WP_Query(); ?>
<?php $recent->query( 'cat=1&showposts=5' ); ?>
<?php $is_first_post = true; ?>
<?php while( $recent->have_posts() ) : $recent->the_post(); ?>
<ul>
<li>
<?php
if ( $is_first_post && has_post_thumbnail() ) {
the_post_thumbnail();
$is_first_post = false;
}
?>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
</ul>
<?php endwhile; ?>
But I want show this using shortcode. which using category & post number
but I can not make shortcode. Please help me.
// Add Shortcode
function recentpost_shortcode_func() {
$recent = new WP_Query();
$recent->query( 'cat=1&showposts=5' );
$is_first_post = true;
$html = '';
while( $recent->have_posts() ) : $recent->the_post();
$html .='<ul><li>';
if ( $is_first_post && has_post_thumbnail() ) {
$html .=get_the_post_thumbnail();
$is_first_post = false;
}
$html .='<a href="'.get_the_permalink().'">';
$html .=get_the_title();
$html .='</a></li></ul>';
endwhile;
return $html;
}
add_shortcode( 'recentpost', 'recentpost_shortcode_func' );
I'm trying to integrate the Simple Lightbox plugin into Advanced Custom Fields image fields.
Basically I want any images on a page (populated via acf image fields) to open in the lightbox when clicked, and also be grouped together with all other images within the lightbox so they can be navigated through like a slideshow. This is built-in functionality for the Simple Lightbox plugin, but I don't know how to integrate it with my template code for image fields.
For acf WYSIWYG fields I'm using this...
<?php
$content = get_field('wysiwyg');
if ( function_exists('slb_activate') )
$content = slb_activate($content);
echo $content;
?>
...which works great, but adapting that to image fields is eluding me.
Here's my template code for image fields (without any attempts at integrating Simple Lightbox):
<?php
$image = get_field( 'img' );
if( ! empty( $image ) ) {
$url = $image['url'];
$alt = $image['alt'];
$caption = $image['caption'];
$size = 'large';
$thumb = $image['sizes'][ $size ];
$width = $image['sizes'][ $size . '-width' ];
?>
<?php if( $caption ): ?>
<div class="wp-caption" style="width: <?php echo $width; ?>px">
<?php endif; ?>
<a href="<?php echo $url; ?>">
<img src="<?php echo $thumb; ?>" alt="<?php echo $alt; ?>" />
</a>
<?php if( $caption ): ?>
<p class="wp-caption-text"><?php echo $caption; ?></p>
</div>
<?php endif; ?>
<?php } ?>
Any suggestions for how to integrate Simple Lightbox into this? Thanks in advance!
Use php output buffering to store your template in a variable. Put ob_start() at the top, then at the bottom ob_get_clean() will stop the output buffering, and you can save the return value to a variable. Then pass that to slb_activate.
<?php
ob_start();
$image = get_field( 'img' );
if( ! empty( $image ) ) {
$url = $image['url'];
$alt = $image['alt'];
$caption = $image['caption'];
$size = 'large';
$thumb = $image['sizes'][ $size ];
$width = $image['sizes'][ $size . '-width' ];
?>
<?php if( $caption ): ?>
<div class="wp-caption" style="width: <?php echo $width; ?>px">
<?php endif; ?>
<a href="<?php echo $url; ?>">
<img src="<?php echo $thumb; ?>" alt="<?php echo $alt; ?>" />
</a>
<?php if( $caption ): ?>
<p class="wp-caption-text"><?php echo $caption; ?></p>
</div>
<?php endif; ?>
<?php } ?>
<?php $content = ob_get_clean(); ?>
<?php echo slb_activate($content); ?>
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; ?>