showing an image from post in wordpress - php

I am working on a wordpress website. I have different category for one type of posts (call flowers for example). Suppose I have 10 posts in this category. I want to show 10 Images from these posts at home page (one image from one post )
After clicking on the image user will be redirected to the corresponding post.
Is this feasible in wordpress ? SHould i need to use custom fields or something which i found after googling.
Thanks

Setup a loop and query_posts() by the category that they are by the cat id
query_posts( 'cat=1' );
Then you could display a featured image for the post in your homepage loop, as so:
<?php if ( has_post_thumbnail() )
{
$image_id = get_post_thumbnail_id();
$alt_text = get_post_meta($image_id, '_wp_attachment_image_alt', true);
$image_url = wp_get_attachment_image_src($image_id,'large');
$image_url = $image_url[0];
?>
<a href="<?php the_permalink(); ?>">
<img src='<?php echo $image_url;?>' alt='<?php echo $alt_text; ?>' title='<?php echo $alt_text; ?>' />
</a>
<?php
}
else
{
?>
<a href="<?php the_permalink(); ?>">
<img src='<?php bloginfo('template_url'); ?>/images/no_image.jpg' />
</a>
<?php
}
?>

Related

Use image ACF with get_previous_post

In an article I want to have the link of the previous article, including the title of the article and a ACF custom image.
I now how to make the link of the previous title project :
<?php
$prev_post = get_previous_post();
if (!empty( $prev_post )): ?>
<a href="<?php echo $prev_post->guid ?>">
<?php echo $prev_post->post_title ?>
</a>
<?php endif ?>
Here's my ACF code for the current image :
<?php
$image = get_field('image_cover');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
But how to do the same thing (get_previous_link) for my ACF image ?
You can use
$image = get_field('image_cover', $prev_post->ID);
This will get the custom field image_cover from the previous post using the ID supplied by the previous post object.

WordPress Default Featured Image

I’ve got a WordPress site and have added the following code to the Single.php file, which means if I publish a blog post without selecting a ‘Featured Image’, it will display a default ‘Featuered Image’ instead (tellymedia-temporary-image.jpg).
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} else { ?>
<img src="<?php bloginfo('template_directory'); ?>/branding/tellymedia-temporary-image.jpg" alt="<?php the_title(); ?>" />
This works fine on the actual page displaying the individual blog post (which shows the default image), but I’m also using the following code in my Functions.php file, to display a ‘Featured Image’ thumbnail for each post in the Sidebar:
<span class="widget-listing-thumbnail"><?php the_post_thumbnail(); ?></span><?php get_the_title() ? the_title()+the_subtitle('<span class="widget-subtitle">', '</span class="widget-subtitle">') : the_ID(); ?>
While this works for all other posts it doesn’t work for any where I haven’t selected a ‘Featued Image’. Is there a way to tweak the code in my Functions.php file please, so it will also display a thumbnail of the default ‘Featuered Image’ in the Sidebar for posts where I haven’t selected one please?
Thanks!
try this one.
<a href="<?php the_permalink(get_the_id()); ?>">
<span class="widget-listing-thumbnail">
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} else { ?>
<img src="<?php bloginfo('template_directory'); ?>/branding/tellymedia-temporary-image.jpg" alt="<?php the_title(); ?>" />
?>
</span>
<?php get_the_title() ? the_title() + the_subtitle('<span class="widget-subtitle">', '</span class="widget-subtitle">') : the_ID(); ?>
</a>

Exclude Base Image from More Views using PHP (Not with Exclude Box)

I'm having a Magento issue where the image selected as the base image is shown twice, once as the main image and once as a "more views" image. The problem is that the base image hasn't been checked as "Exclude" on all the thousands and thousands of products.
Is there a Magento way to either pull the full gallery of images, regardless of if "Exclude" is checked or not, or to pull all the images except the one selected as "base image".
Current Code
<a href="<?php echo $helpImg->getImg($_product, $imgWidth, $imgHeight); ?>" title="<?php echo $this->escapeHtml($this->getImageLabel());?>" />
<img src="<?php echo $helpImg->getImg($_product, $imgWidth, $imgHeight); ?>" alt="<?php echo $this->escapeHtml($this->getImageLabel());?>" title="<?php echo $this->escapeHtml($this->getImageLabel());?>" />
</a>
<?php if (count($this->getGalleryImages()) > 1): ?>
<?php foreach ($this->getGalleryImages() as $_image): ?>
<a href="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize($additionalImageSize[0], $additionalImageSize[1]); ?>" class="swipebox" title="<?php echo $this->escapeHtml($this->getImageLabel());?>" />
<img data-src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize($additionalImageSize[0], $additionalImageSize[1]); ?>" alt="<?php echo $subtitle; ?>" title="<?php echo $this->escapeHtml($this->getImageLabel());?>"/>
</a>
<?php endforeach; ?><?php endif; ?>
Currently it pulls the main image, and then it pulls the gallery. I'm trying to just pull the ENTIRE gallery regardless of "Exclude", or pull the gallery except the base image.
Ideas?
Solution has been found
PHP
<?php
if ($_product->getImage() == $_image->getFile())
continue;
?>
Added this directly after the foreach statement, now it pulls all images except the first one.

How to add Title Attribute to Wordpress Image on Attachment Page in TwentyEleven Theme?

This is a WP/SEO question related to adding the title attribute to the on an attachment page. We are using the twentyeleven theme and default image.php code, as below.
We want to pull in the title tag (or even automatically set the title for all images, so user doesn't have to individually set, or just use post title for the title - any of these).
The html from View Source looks like this:
The image.php code:
<a href="<?php echo esc_url( $next_attachment_url ); ?>" title="<?php the_title_attribute(); ?>" rel="attachment"><?php
$attachment_size = apply_filters( 'twentyeleven_attachment_size', 848 );
echo wp_get_attachment_image( $post->ID, array( $attachment_size, 1024 ) ); // filterable image width with 1024px limit for image height.
?></a>
I also wanted all my attachments to automatically have a title attribute, so I wrote the following PHP code:
<?php if(wp_attachment_is_image($post->id)) : $att_image = wp_get_attachment_image_src($post->id, "full"); ?><p class="attachment"><a class="show_image" rel="bookmark" href="<?php echo wp_get_attachment_url($post->id); ?>" title="<?php echo get_the_title(); ?>"><img class="img-responsive attachment-medium" src="<?php echo $att_image[0]; ?>" height="<?php echo $att_image[2];?>" width="<?php echo $att_image[1];?>" alt="<?php echo get_the_title(); ?>"></a></p><?php endif; ?>
This PHP code has been tested on WordPress 3.7.1. A live example of this code being used on a custom WordPress image.php file can be found on my Web Development & Design Blog

How to Create Nextgen Custom gallery with album name

i am trying so long to making one normaly gallery useing by Nextgen gallery. but i can't make it yet. i found some way to show gallery thumbnail for speciy gallery and show album name but can't make that dynamicly and show together. bellow some code may be help
for show custom template file.
<?php
/*
Template Name: Gallery
*/
get_header(); ?>
<?php
$NextG = new NextGEN_Shortcodes;
echo $NextG->show_gallery( array("id"=>2,"template"=>"myshow") );
?>
<?php the_post_thumbnail(); ?>
<?php
if ( have_posts() ) while ( have_posts() )
{
the_post();
the_content();
}
?>
<?php get_footer(); ?>
for make custom nextgen gallery file
gallery-mytemplate.php
<ul id="slideshow">
<!-- Thumbnails -->
<?php foreach ( $images as $image ) : ?>
<li>
<a href="<?php echo $image->imageURL ?>" title="<?php echo $image->description ?>" <?php echo $image->thumbcode ?> >
<?php if ( !$image->hidden ) { ?>
<img title="<?php echo $image->alttext ?>" alt="<?php echo $image->alttext ?>" src="<?php echo $image->thumbnailURL ?>" <?php echo $image->size ?> />
<?php } ?>
</a>
</li>
<?php endforeach; ?>
</ul>
what i want ?
actuly i want my gallery page will have all gallery name top always and if i click any gallery name then show that gallery. see http://www.evokephotoandvideo.com/2013-photo-gallery/ this url something like that. if can be possible also ajax load like ref. line then would be more nicer...
Simplest way for retrieving data from NextGen is to use NextGEN Gallery Database Class stored in ngg-db.php file. Open that file and there are all functions and explanations for taking the data directly from the database.
Example:
$NextG = new nggdb();
// get all galleries
$NextG->find_all_galleries();
// get single gallery
$NextG->get_gallery( 2 );
Results are arrays, to examine them use print_r or print_r2:
function print_r2($val){
echo '<pre>';
print_r($val);
echo '</pre>';
}
That way you can take id or slug from all galleries and create custom url's like http://www.evokephotoandvideo.com/gallery/slug.
Additional info:
WP Rewrite

Categories