Limiting Results from an Array of values - php

I am using advanced custom fields in Wordpress. Without going into the details of how it works, I have a "repeater field" which lets the users add as many images to an aera of the backend as they please. To show these images, I use the following code (inside wordpress loop)
<?php if(get_field('slider_images')): ?>
<?php while(the_repeater_field('slider_images')): ?>
<?php
$attachment_id = get_sub_field('work_slider_image');
$size = "homepage";
$image = wp_get_attachment_image_src( $attachment_id, $size );
echo $image[0];
?>
<?php endwhile; endif; ?>
The goal here is to create an array of image URLS and to only display the FIRST one. On other pages they will all be used, but on this page, I want to grab ONLY the first image, hence the echo $image[0];
For some reason, it is showing all of the uploaded images, and when I print the variable $image, it returns:
Array ( [0] => http://sitename.com/agsinfo/wp-content/uploads/2012/07/1.jpg [1] => 392 [2] => 165 [3] => )
After seeing this, it would make sense to me that echo $image[0]; would work, but for some reason its not. Any ideas?

<?php if(get_field('slider_images')): ?>
<?php while(the_repeater_field('slider_images')): ?>
<?php
$attachment_id = get_sub_field('work_slider_image');
$size = "homepage";
$image[] = wp_get_attachment_image_src( $attachment_id, $size );
?>
<?php endwhile;
echo $image[0];
endif; ?>

Related

How to resize an image attached in the page in wordpress

I need to decrease the height of the image attached in the wordpress page. i had tried several methods but i am unable to solve it.It is not getting any change when i am adding image size Topbannerimg in img src.
my wp_query to dispaly image
<?php
$homepage = get_page_by_title('Top Banner');
$attachments = get_posts( array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_parent' => $homepage->ID,
) );
if ( $attachments ) {
echo '<section class="topBanner">';
foreach ( $attachments as $attachment ) { ?>
<a href="<?php if($attachment->post_excerpt){echo $attachment->post_excerpt;}else{echo "#";}?>">
<img src="<?php echo wp_get_attachment_url($attachment->ID); ?>">
</a>
<?php }
echo '</section>';
}
wp_reset_postdata();?>
size mentioned in functions.php
add_image_size( 'TopBannerImg', 1800, 500, true );
You can use wp_get_attachment_image function to display image. Please refer: https://developer.wordpress.org/reference/functions/wp_get_attachment_image/.
Pass image thumbnail name as second paremeter.
Checkout this code:
First parameter for width and second for height.
<?php the_post_thumbnail( array( 480, 277 ) ); ?>

Where to add code to custom gallery shortcode

I'm using the information here http://ieg.wnet.org/2016/02/replacing-default-wordpress-gallery-shortcode/ as the basis for a custom gallery shortcode, and trying to create this carousel slider https://www.jssor.com/demos/image-gallery.slider. Problem is I don't know how exactly to implement it.
The images and everything up until then shows in the source code, but then nothing after it.
<div data-u="slides" style="cursor:default;position:relative;top:0px;left:0px;width:800px;height:356px;overflow:hidden;">
<?php
foreach ( $images as $image ) {
$thumbnail = wp_get_attachment_image_src($image->ID, 'homepage-thumb');
$thumbnail = $thumbnail[0];
$fullsize = wp_get_attachment_image_src($image->ID, 'service-page');
$fullsize = $fullsize[0];
$gallery .= "<div><img data-u='image' src='".$thumbnail."'><img data-u='thumb' src='".$fullsize."'></div>";
}
return $gallery;
?>
</div>
It's probably something simple, this is just far past my base of knowledge when it comes to this.
You are returning the value instead of printing it to the web page. Use echo instead of return. Also make sure you echo it in each iteration of the loop so you don't just print one image, but all of them.
<div data-u="slides" style="cursor:default;position:relative;top:0px;left:0px;width:800px;height:356px;overflow:hidden;">
<?php
foreach ( $images as $image ) {
$thumbnail = wp_get_attachment_image_src($image->ID, 'homepage-thumb');
$thumbnail = $thumbnail[0];
$fullsize = wp_get_attachment_image_src($image->ID, 'service-page');
$fullsize = $fullsize[0];
$gallery .= "<div><img data-u='image' src='".$thumbnail."'><img data-u='thumb' src='".$fullsize."'></div>";
echo $gallery;
}
?>
</div>

I am trying to use ACF repeater to call a random image on a wordpress site

This is my code, as aforesaid - i'm trying to get a single picture to echo inside a premade div out of repeater element. Despite working directly with the example on the website - I would love some help.
<?php
$repeater = get_field('left_column_repeater' ); // get all the rows
$rand_row = $repeater[ array_rand( $repeater ) ]; // get a random row
$rand_row_image = $rand_row['left_column_image' ]; // get the sub field value
// Note
// $first_row_image = 123 (image ID)
$image = wp_get_attachment_image_src( $first_row_image, 'full' );
// url = $image[0];
// width = $image[1];
// height = $image[2]; ?>
<div class="circular" style="background-image: url('<?php echo $image[0]; ?>');"> </div>
Where did I go wrong?

wordpress different banner images for each pages - dynamically

My website consist of 5 inner pages, and I want to use different banner images for each page. The page is with side bar, but I want a full width banner, so I used a code which I got from wordpress and its working
this is the code..
<div class="banner">
<?php
if( is_page('About') ) $img = 'bannerAbout.jpg';
elseif( is_page('Services') ) $img = 'bannerServices.jpg';
elseif( is_page('Testimonials') ) $img = 'bannerTestimonials.jpg';
elseif( is_page('Testimonials') ) $img = 'bannerTestimonials.jpg';
elseif( is_home() ) $img = 'bannerBlog.jpg';
else $img = 'banner.jpg';?>
<img alt="" src="<?php bloginfo('stylesheet_directory'); ?>/images/<?php echo $img;?>" />
</div>
My question is, How Can I call 'Featured Image' of each page in this code? between $img=""
Or any plug-for this?
If I can call the featured image, then its easy to upload images, otherwise I need to use FTP all the time to change.
Please help me.
Thanks in Advance
Add this code to your theme's functions.php
add_theme_support( 'post-thumbnails', array( 'post', 'page' ) );
this will enable featured image for post and page
Then in your page.php file add this code
before content and sidebar
<?php
global $post;
echo get_the_post_thumbnail($post->ID, 'full'); // visit http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail for more info
?>
Place below code in each condition
global $post
echo get_the_post_thumbnail( $post->ID,'full' );
full will take the full size of image what ever you have uploaded

how to get post thumbnail using post id in wordpress?

I am trying to get the post thumbnail using post_id,but i am getting so many problems.
Iam calling the function in a separate php file in theme directory
echo get_the_post_thumbnail('637');
Fatal error: Call to undefined function get_the_post_thumbnail() in ...
1)can we get the thumbnail using post_id
or
2)can we get the image source using post_id
please any body help me
Thanks in advance
Try this
global $post;
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'post');
echo $thumb[0];
In your case you make a small mistake that you put the single quote inside the function when function require an integer value.
echo get_the_post_thumbnail('637');
Bellow code are valid try it.
Simple Form
echo get_the_post_thumbnail(637);
Size Specified Form where second argument is the size of the image.
echo get_the_post_thumbnail(637, array(100,100));
also you can try bellow code also
get_the_post_thumbnail(637); // without parameter -> Thumbnail
get_the_post_thumbnail(637, 'thumbnail'); // Thumbnail
get_the_post_thumbnail(637, 'medium'); // Medium resolution
get_the_post_thumbnail(637, 'large'); // Large resolution
get_the_post_thumbnail(637, 'full'); // Original resolution
Also you can refer to the WordPress codex Here.
I am also going to write a full post on this topic on my blog
Use Require_once Or include_once
require_once('/the/path/to/your/wp-blog-header.php');
include_once('wp-blog-header.php' );
get_the_post_thumbnail($post_id); // without parameter -> Thumbnail
get_the_post_thumbnail($post_id, 'thumbnail'); // Thumbnail
get_the_post_thumbnail($post_id, 'medium'); // Medium resolution
get_the_post_thumbnail($post_id, 'large'); // Large resolution
get_the_post_thumbnail($post_id, 'full'); // Original resolution
get_the_post_thumbnail($post_id, array(100,100) ); // Other resolutions
Out side of loop
global $post;
if (has_post_thumbnail( $post->ID ) ){
//
get_the_post_thumbnail($post->ID);
//
}
Vallabh's solution works. This is how I use it as a background image:
<?php if (has_post_thumbnail( $post->ID ) ) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id(637), 'thumbnail' );
$image = $image[0];
} ?>
<div style="background-image: url(<?php echo $image; ?>)"> ... </div>
Create a post template..look like this(post_temp.php)
<?php
$args=array('order'=> 'DESC', 'posts_per_page'=>get_option('posts_per_page'));
$query=new WP_Query($args);
if( $query->have_posts()):
while( $query->have_posts()): $query->the_post();
{
echo get_the_post_thumbnail($post->ID);
}
endwhile;
else:
endif;
?>

Categories