Post image only with a e keyword - php

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);
}
}
}
?>

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!

PHP showing certain content top of page, rest afterwards

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; ?>

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>';
}
}

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

How do I show an image from the latest post in wordpress

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; ?>

Categories