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!
Related
Wordpress team members have a select in admin that lets you pick a number to display the team in a certain order on Archive page. The Select returns a value of the number, which i retrieve.
However, I want to display all the members that have the value of 1 first, and any members after that show up below. At the moment, the IF statements only shows the content if they exist (obviously), i would like to however make it work somehow so it shows Members with value 1 first, then members with value 2. Thank you!
$images = get_field('image');
$titles = get_the_title();
$positions = get_field('position');
$link = get_permalink($post->ID);
if ($positions[0] == 1) {
echo "<div class='employee'>";
echo "<img src='$images'>";
echo "<h2>$titles</h2>";
echo "</div>";
}
if ($positions[0] == 2) {
echo "<div class='employee'>";
echo "<img src='$images'>";
echo "<h2>$titles</h2>";
echo "</div>";
}
If you want to sort the $positions array you could use
sort($positions);
You you can easily sort by a meta value with get_posts()...
<?php
$args = array(
'post_type' => 'team_members',
'posts_per_page' => -1,
'meta_key' => 'position',
'orderby' => 'meta_value',
'order' => 'ASC'
);
$posts = get_posts( $args );
?>
<?php if( $posts ): ?>
<ul>
<?php foreach( $posts as $post ): setup_postdata( $post ) ?>
<li class='employee'>
<img src='$images'>
<h2>$titles</h2>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
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);
}
}
}
?>
I'm running a multi site and I want to pull post with certain categories from two blogs. Therefore I am running a loop for each of the blog to pull the posts as one of the category is in blog 1 while the other category is in blog 2.
<?php $value = array(); ?>
<?php
// Get the values from $_POST
$original_blog_id = get_current_blog_id(); // get current blog
$bids = array(1,2); // all the blog_id's to loop through EDIT
foreach($bids as $bid):
switch_to_blog($bid);
$args = array(
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'Music',
'field' => 'slug',
'terms' => array('artist', 'club')
),
)
);
$the_query = new WP_Query( $args );
?>
<?php $postids = array(); ?>
<?php if ( $the_query->have_posts() ) { ?>
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<?php $postids[]=get_the_ID(); ?>
<?php endwhile; ?>
<?php $value[] = $postids; ?>
<?php
} else {
// no posts found
echo 'Nothing found.';
}
?>
<?php endforeach; ?>
<?php
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($value));
$list = iterator_to_array($it,false);
$posts = new WP_Query(array(
'post__in' => $list,
'post_type' => 'post',
));
?>
<?php
foreach ($posts as $post) {
setup_postdata($post);
?>
<?php echo the_title(); ?>
<?php
}
wp_reset_postdata(); ?>
<?php switch_to_blog($original_blog_id); ?>
The reason why I'm getting the IDs inside an array:
<?php $postids[]=get_the_ID(); ?>
because I want to fetch random post's. If at this point instead of the above statement I get the title and content of the posts then it will show in sequential order. Something like this:
BLOG1: POST1,POST2, POST : BLOG2: POST1, POST2, POST3
But I want Posts in random order like this:
BLOG1: POST1, BLOG2: POST3, BLOG1: POST2, BLOG2: POST1: BLOG2: POST2
So everything is working fine, I am able to get the posts IDs even outside the foreach loop but the problem is:
I am not able to get post content from those IDs. It only gives me posts from blog 2 because the current blog is 2. But it doesn't show anything from blog1 even though the postID is in the list array.
Can anyone please help?
The solution can be the following, just add the code into your themes footer.php files, although this can be put anywhere within your WordPress theme, example:
<?php
if (!function_exists('display_posts_from_blogs')) {
function display_posts_from_blogs($blog_id) {
global $switched;
switch_to_blog($blog_id); //switched to blog id 2, for example
// Get latest Post
$latest_posts = get_posts('category=-3&numberposts=6&orderby=post_name&order=DSC');
$cnt =0;
?>
<ul>
<?php foreach($latest_posts as $post) : setup_postdata($post);?>
<li>
<?php echo $post->post_title; ?>
</li>
<?php endforeach ; ?>
<?php restore_current_blog(); //switched back to main site
} //end function
}
?>
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
In Wordpress, I want to show the 2 latest posts along with the post thumbnail for just the first post.
I have been playing around with the code below, but an image always ends up being shown for the first post as well as the second, when I only want to show an image for the first.
<?php
$cat_args = array(
'orderby' => 'name',
'order' => 'ASC',
'child_of' => 0
);
$post_args = array(
'numberposts' => 2,
'category' => $category->term_id
);
$posts = get_posts($post_args);
foreach($posts as $post) {
?>
<?php the_title(); ?>
<?php the_post_thumbnail('blog_post_image'); ?>
<?php
}
}
?>
You're sort of missing any condition that would allow you to selectively display the image.
<?php
foreach($posts as $key=>$post) {
the_title();
if (0 == $key) {
the_post_thumbnail('blog_post_image');
}
}
Assuming $posts is a 0-based enumerated array. Notice the addition of $key to the foreach, as well as the if before printing the thumbnail
<?php $loop = 1; ?>
<?php foreach($posts as $post): ?>
<?php the_title(); ?>
<?php if($loop == 1): ?>
<?php the_post_thumbnail('blog_post_image'); ?>
<?php endif; ?>
<?php $loop++; endforeach; ?>