php - get image urls from wordpress posts - php

I have website based on wordpress system and I need to get images urls from every post. I have this code and it's working, but there is problem, because all of posts have same pictures + at the end, there are new ones. Here is example:
post1 - image1.png,image2.png,image3.png
post2 - image1.png,image2.png,image3.png,new1.png,new2.png
post3 - image1.png,image2.png,image3.png,new1.png,new2.png,third.png
etc...
And here is my php code
preg_match_all('/<img[^>]+>/i',$old_content, $imgTags);
for ($i = 0; $i < count($imgTags[0]); $i++) {
// get the source string
preg_match('/src="([^"]+)/i',$imgTags[0][$i], $imgage);
// remove opening 'src=' tag, can`t get the regex right
$origImageSrc[] = str_ireplace( 'src="', '', $imgage[0]);
}
Any ideas, why it's doing? :-)

This might help you out, here's a function that you can throw into your functions.php file in your Wordpress theme.
/*
* Retreive url's for image attachments from a post
*/
function getPostImages($size = 'full'){
global $post;
$urls = array();
$images = get_children(array(
'post_parent' => $post->ID,
'post_status' => 'inheret',
'post_type' => 'attachment',
'post_mime_type' => 'image'
));
if(isset($images)){
foreach($images as $image){
$imgThumb = wp_get_attachment_image_src($image->ID, $size, false);
$urls[] = $imgThumb[0];
}
return $urls;
}else{
return false;
}
}
That will return an array with each image url that is attached to the post/page. To loop thru and show all of the images in an <ul> you could do something like this.
<?php if(have_posts()): while(have_posts()): the_post(); ?>
<ul id="post_images">
<?php $postImages = getPostImages($size = 'full'); ?>
<?php foreach($postImages as $image): ?>
<li><img src="<?php echo $image; ?>" /></li>
<?php endforeach; ?>
</ul>
<?php endwhile; endif; ?>

Related

How to get attachment_url image by comment id on Wordpress

Hi i'm use plugin DCO Comment Attachment to be able to add images to comments, I use code below get list comments. Now I want to get the image link of the comment to handle before showing it to the screen. Currently according to the code below, it only takes all the image files attached to the comment post, not each comment.
I tried instead $comment->comment_post_ID to $comment->comment_ID but it does not work.
Thank everyone!
<?php $comments = get_comments($param);?>
<?php foreach ($comments as $comment): ?>
<?php if ($comment->comment_approved != '0'): ?>
<?php
$attachments = get_posts(array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => 'any',
'post_parent' => $comment->comment_post_ID,
));
if ($attachments) {
foreach ($attachments as $attachment) {
echo wp_get_attachment_url($attachment->ID);
}
}
?>
<?php endif;?>
<?php endforeach;?>
After self-study, I know how to get the url image in the comment with the following code, this code put in the functions.php file.
function get_attachment_url_image_comment($comment_id) {
$meta_key = 'attachment_id';
$attachment_id = get_comment_meta( $comment_id, $meta_key, true );
$full_img_url = wp_get_attachment_image_url( $attachment_id, 'full' );
return $full_img_url;
}
All that's left to do is pass the desired comment ID into the function and get the attached image
<?php $comments = get_comments($param);?>
<?php foreach ($comments as $comment): ?>
<?php if ($comment->comment_approved != '0'): ?>
<?php if(get_attachment_url_image_comment($comment->comment_ID)): ?>
<?php echo get_attachment_url_image_comment($comment->comment_ID) ?>
<?php endif; ?>
<?php endif;?>
<?php endforeach;?>
Result: "http://localhost/wp-content/uploads/2020/07/11439468-3x4-xlarge-2.jpg"
It works fine!

Post image only with a e keyword

I want to post a gallery formed by a number of images from my site that have a certain keyword. Like from a certain location, or time. I thought to use the caption option or the description option from wordpress. Images will have more keywords, something like: "Location" "Sunset". I tryed to use this What is the function got get all the media files wordpress? combined with https://wordpress.org/ideas/topic/functions-to-get-an-attachments-caption-title-alt-description but I cant get them working. Can you help me please. But I am a newbie so can you explain exactly what to write in function.php and what to write in page-name.php
LE: <?php $attachment_meta = wp_get_attachment(wp_get_attachement_id()); ?>
<?php if ($attachement_meta[caption] == 'Ceahlau' ) ?>
echo do_shortcode('[gallery columns="4" link="file" ids=" <?php wp_get_attachement_id() ?>"]')
<?php else: ?>
<?php endif; ?>
This is what I have now in my page-name.php
<?php $attachment_meta = wp_get_attachment(wp_get_attachement_id()); ?>
<?php if ($attachement_meta[caption] == 'Ceahlau' ) ?>
echo do_shortcode('[gallery columns="4" link="file" ids=" <?php
wp_get_attachement_id() ?>"]')
<?php else: ?>
<?php endif; ?>'
This is what i have now in my page-name.php
This is what i have now in my page-name.php
<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => null, // any parent
'post_mime_type' => 'image'
);
$attachments = get_posts($args);
if ($attachments) {
$attachment_meta = wp_get_attachment($attachment->ID);
foreach ($attachments as $post) {
if ($attachment_meta['caption'] == 'Ceahlau' ){
setup_postdata($post);
echo wp_get_attachment_image( $attachment->ID, 'full' );
the_attachment_link($post->ID, false);
}
}
}
?>

Exclude excerpt from wordpress wp_query

Hellow everyone!
I am showing blog of posts in additional wp template and everything works fine, but I've made some kind of gallery from it, and I don't need to show anything except gallery and title.
Inside posts I have this:
[gallery ids="1618,...,1634"]
<h2>...</h2>
<p>...</p>
text without format, etc.
As you can see, I am using a gallery shortcode. I need it to be shown, but all other content to be excluded from the loop.
Really appreciate your help in this question...
My template code:
<?php
/*
* Template name: Блог
*/
$current_page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 9,
'paged' => $current_page,
'cat' => 8
);
query_posts( $args );
$wp_query->is_archive = true;
$wp_query->is_home = false;
while(have_posts()): the_post();
?>
<div class="foto_posts">
<?php the_content() ?>
<?php echo '' . get_the_title() . '';?>
</div>
<?php
endwhile;
if(function_exists('page_navi_slider')) page_navi_slider();
try to replace <?php the_content() ?> to <?php the_title() ?>
if you just want to show gallery shortcode try this
replace
<?php the_content() ?>
to
$pattern = get_shortcode_regex();
preg_match('/'.$pattern.'/s', $post->post_content, $matches);
if (is_array($matches) && $matches[2] == 'gallery') {
$shortcode = $matches[0];
echo do_shortcode($shortcode);
}
Try this for getting galleries and videos use the get_post_meta_key()
<?php get_post_meta($post_id,'key_name',1);?>
If it returns an array then traverse that array to get the images link and video links
add_shortcode('gallery', 'gallery_shortcode_fancybox');
function gallery_shortcode_fancybox($attr) {
$attachment_ids = explode(',',$attr['ids']);
$args = array(
'post__in' => $attachment_ids,
//'cat' => 8 if category needs to be shown pass it in shortcode , same as ids
);
$gallery_posts = query_posts( $args );
foreach ($gallery_posts as $key => $value) {
//do the stuff here
echo '<h2>'. $value->post_title;'</h2>';
$feat_image = wp_get_attachment_url( get_post_thumbnail_id($value->ID);
echo '<p><img src="'.$feat_image.'" ></p>';
}
}

Adding Advanced Custom Fields to Related Posts in Wordpress

I am trying to add a custom field value to related posts functionality, but am currently totally stuck at this:
<div class="relatedposts">
<h3>Related posts</h3>
<?php
$orig_post = $post;
global $post;
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args=array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID),
'posts_per_page'=>4, // Number of related posts to display.
'caller_get_posts'=>1
);
$my_query = new wp_query( $args );
while( $my_query->have_posts() ) {
$my_query->the_post();
?>
<div class="relatedthumb">
<a rel="external" href="<? the_permalink()?>">
<?php
$image = wp_get_attachment_image_src(get_field('image'), 'full');
print_r( $image[0] ); ?>
<?php the_title(); ?>
<img src="<?php echo $image[0]; ?>" />
</a>
</div>
<? }
}
$post = $orig_post;
wp_reset_query();
?>
</div>
This mostly works as it is code found online, it is just where to put the reference to the custom field that I seem to be missing. I am unable to place the variable and print_r of this anywhere to see the results.
foreach($tags as $individual_tag) {
$meta_values = get_post_meta($post->ID,'');
if (in_array('VALUE SEARCHING FOR',$meta_values) {
$tag_ids[] = $individual_tag->term_id;
} // if
} // foreach
Fill in the appropriate blanks and this should filter your tags properly.
HTH,
=C=
With ACF, you can pass a post ID to the get_field and the_field functions:
$image_id = get_field( 'image', $post -> ID );
If you use the functions without a set post ID, the functions may return the field value of some other globally initialized post.
So your attachment image getter (inside the related posts WP_Query loop) would be something like
$image = wp_get_attachment_image_src(get_field('image', get_the_ID()), 'full');
Make sure your ACF field type is set to image and the wanted variant of it: ID, image object, or image URL. get_field will then return either the image's attachment ID, the image object or the image URL.
In your code example, returning an ID would be the correct choice, assuming wp_get_attachment_image_src expects an attachment ID.
EDIT:
I just realized, that the ACF image field may not be saving the images to the WP attachment system in a normal way WP does. Try returning the image as an object and var_dump the object's contents to see where the full size image URL resides there.
I could be wrong and the wp_get_attachment_image_src might work fine as is.
Ok, anyone who may find this useful, I have a solution for getting the ACF field to appear:
<div class="related grid clear">
<h2>Related posts</h2>
<?php
$orig_post = $post;
global $post;
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args=array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID),
'posts_per_page'=>9,
'caller_get_posts'=>1
);
$my_query = new wp_query( $args );
if($my_query->have_posts()) {
while($my_query->have_posts()) {
$my_query->the_post();
$image = get_field('hero_image', get_the_ID());
$introduction = get_field('introduction');
?>
<article class="grid-item" data-permalink="<? the_permalink()?>">
<div style="background-image: url(<?php echo $image; ?>);"></div>
<h3><?php the_title(); ?></h3>
<p><?php echo $introduction; ?></p>
</article>
<?
}
}
$post = $orig_post;
wp_reset_postdata();
wp_reset_query();
}
?>
</div>

Retrieving post thumbnail url from a post array with foreach loop

I'm having troubles retrieving the thumbnail of each post contained in an array.
I have this array that contains every post of a custom post type:
<?php
$clients_array = array(
'post_type' => 'clients',
'sort_order' => 'ASC',
'sort_column' => 'post_title',
'post_status' => 'publish'
);
?>
While I have no problem retrieving the thumbnail using the standard wordpress loop, like this:
<?php
$query = new WP_Query( $clients_array );
while ( $query->have_posts() ) : $query->the_post();
?>
<?php if ( has_post_thumbnail()) : ?>
<?php the_post_thumbnail() ?>
<?php
endif;
endwhile;
?>
I'd like to load the posts with a foreach look, such as:
<?php
$clients = get_pages($clients_array);
foreach ($clients as $page_data) {
$client_id = $page_data->ID;
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($client_id), 'thumbnail' );
echo $thumb;
}
?>
Unfortunately, I can't get it work in any way I tried.
What am I doing wrong?
Most of WordPress's functions prefixed with get_ are to retrieve the specified data and not echo it. Therefore putting the data into a variable or echoing it manually would work for your situation like #jothikannan said:
echo get_the_post_thumbnail($id);
or
$foo = get_the_post_thumbnail($client_id);
//do sowething with $foo
you must use follow to get the thumbnail of the features image
<?php echo get_the_post_thumbnail($client_id); ?>
it is already answered here look at here

Categories