I'm using
if ( has_post_thumbnail() ){}
to check if the post have thumbnail image,
but this
echo get_attached_media('image', $post->ID);
display the word
Array
I need to show the attached image
If you are trying to get just one image in the post and use it as a thumbail, you might want to try this one:
Add this to you're functions.php :
// Get URL of first image in a post
function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
// no image found display default image instead
if(empty($first_img)){
$first_img = "/images/default.jpg";
}
return $first_img;
}
To call it just put
<?php echo catch_that_image() ?>
in your template file within the loop.
I found this brilliant code from this forum thread. Saved me a lot of times.
I found this, and it WORKS
<?php
if( has_post_thumbnail() )
{
// check if the post has a Post Thumbnail
echo ' <a href="';
the_permalink();
echo '" title="';
the_title_attribute();
echo '">';
the_post_thumbnail( 'medium' );
echo '</a>';
}
else
{
$imgs = get_attached_media( 'image' );
if( count( $imgs ) > 0 )
{
$img = array_shift( $imgs );
echo wp_get_attachment_image( $img->ID, 'thumbnail' );
}
}
?>
And all thanks and credit for #birgire on WPSE
You need to use get_attached_media( 'image', $post->ID );
Using
echo get_attached_media( 'image' );
should work.
Related
In a post template I'm using a little code to show the author's image/avatar instead of a featured image.
<?php if ( $logo = get_avatar( get_the_author_meta( 'user_email' ), 200 ) ) : echo $logo; ?>
all well.. the author's logo is there
But now, as I finishing the project, I would like to add 'og:image' metas in the
I hoped to use a variation of this code in the function.php, but that does not work at all.
Now I have this:
add_action('wp_head', 'add_meta_tags',2);
function add_meta_tags(){
global $post;
$author_id = get_post_field('post_author' , $post->ID);
$ogimg = get_avatar_url($author_id->ID, array("size"=>400 )) ;
if( is_single() ) {
echo '<meta property="og:image" content="'. $ogimg .'" />';
}
}
$author_id gives the ID of the author, but I don't get the avatar of the author.
Couldn't figure out how to use the get_avatar_url function, but found a workaround by extracting the src value from the couple img tag like this:
add_action('wp_head', 'add_meta_tags',1);
function add_meta_tags() {
global $post;
$author_id = get_post_field('post_author' , $post->ID);
$rawimg = get_avatar($author_id, 800) ;
$doc = new DOMDocument();
$doc->loadHTML($rawimg);
$imageTags = $doc->getElementsByTagName('img');
foreach($imageTags as $tag) {
$ogimg = $tag->getAttribute('src');
echo '<meta property="og:image" content="'.$ogimg .'" />';
echo '<meta name="twitter:image" content="'. $ogimg .'" />';
}
}
I'm trying to create a WP shortcode that would include both html and php. For example, something like that (that does not work):
function my_first_shortcode() {
$content = <<<EOT
<h1>Some title</h1>
<p><?php the_field('description'); ?></p>
EOT;
return $content;
}
add_shortcode('my_shortcode', 'my_first_shortcode');
The the_field('name_of_field'); normally outputs the content of the specified variable/field (Advanced Custom Fields).
Is the HEREDOC way the right way of doing that? If so, how would I do it? It'd be also great if I could pass variables to the shortcode.
Thank you
First, you can't write PHP tags inside HEREDOC.
You can use it like that:
$the_field = 'the_field';
$content = <<<EOT
<h1>Some title</h1>
<p>{$the_field('description')}</p>
EOT;
In order to pass attributes to a shortcode it's very simple.
for example we have the shortcode:
[my_shortcode att_1="some_value" att_2="some_value"]
function my_first_shortcode($atts)
{
$att_1 = $atts['att_1'];
$att_2 = $atts['att_2'];
}
add_shortcode('my_shortcode', 'my_first_shortcode');
I always prefer to use output buffering for my shortcodes, example below.
function my_first_shortcode() {
ob_start(); ?>
<h1>Some title</h1>
<p><?php echo the_field('description'); ?></p>
<?php
return ob_get_contents();
}
add_shortcode('my_shortcode', 'my_first_shortcode');
add_shortcode('location_start_your_application_group', 'start_your_application_group');
function start_your_application_group() {
ob_start();
$start_your_application_group = '';
$start_your_application_group .= '<section class="start-your-application">';
if ( have_rows( 'start_your_application_group', 'option' ) ) :
while ( have_rows( 'start_your_application_group', 'option' ) ) : the_row();
$heading = get_sub_field( 'heading' );
$content = get_sub_field( 'content' );
if ( $heading !== '' ) {
$start_your_application_group .= '<h3 class="start-your-application__heading">' . $heading . '</h3>';
}
if ( $content !== '' ) {
$start_your_application_group .= '<div class="start-your-application__content">' . $content . '</div>';
}
$image = get_sub_field( 'image' );
if ( $image ) {
$start_your_application_group .= '<div class="start-your-application__image-container"><img class="start-your-application__image" src="' . esc_url($image['url']) .'" alt="' . $image['alt'] . '" /></div>';
}
endwhile;
endif;
$start_your_application_group .= '</section>';
$start_your_application_group = ob_get_clean();
return $start_your_application_group;
}
I'm using Woocommerce for a 1800+ product e-shop. Woocommerce comes with a built-in PrettyPhoto lightbox plugin. By default it loads the original Wordpress image size when you click to zoom the image in the lightbox.
Woocommerce calls for this original image in the template 'product-image.php'. I can modify that one. The variable $image_link calls the original size. I would like to call a custom size that I already made. I just don't know how to call it, I've looked for over a day in docs, forums and sites but can't find it.
I need to modify $image_link so that it calls my custom image size (that I added via add_image_size). But how? I know this is Wordpress basics but I'm unable to get it done...
Example of my site (click to zoom, it shows a reduced image of 1600px wide image, but I want a smaller one): http://www.affichesmarci.com/shop/the-railys-cycling-act/
<?php
if ( has_post_thumbnail() ) {
$image = get_the_post_thumbnail( $post->ID, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ) );
$image_title = esc_attr( get_the_title( get_post_thumbnail_id() ) );
$image_link = wp_get_attachment_url( get_post_thumbnail_id() );
$attachment_count = count( $product->get_gallery_attachment_ids() );
if ( $attachment_count > 0 ) {
$gallery = '[product-gallery]';
} else {
$gallery = '';
}
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '%s', $image_link, $image_title, $image ), $post->ID );
} else {
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<img src="%s" alt="Placeholder" class="bigbox" />', woocommerce_placeholder_img_src() ), $post->ID );
}
?>
<?php do_action( 'woocommerce_product_thumbnails' ); ?>
I found the solution myself.
Original variable that calls the full size image.
$image_link = wp_get_attachment_url( get_post_thumbnail_id() );
New variable that calls a custom size 'zoom'.
$image_link = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'zoom' );
If you already set "add_image_size" in functions, I think you could easily edit
$image = get_the_post_thumbnail( $post->ID, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ) );
to
$image = get_the_post_thumbnail( $post->ID, apply_filters( 'your_thumbnail_name', 'shop_single' ) );
you could also use WP's default sizes which is "thumbnail", "medium", "large" and "full"
ok, adding large size instead of full
$image_link = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large');
Now change $image_link to $image_link[0]
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '%s', $image_link[0], $image_title, $image ), $post->ID );
Thanks for ideas :)
for get src url use this code:
$image_link = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail');
and for use to tag image by alt and size:
echo '<a href="' . get_permalink() . '" class="thumb"><span class="face">
<img src="' . $image_link[0]. '" width="'.$image_link[1].'"
height="'.$image_link[2].'" loading="lazy" alt="'.get_the_title().'" /></span></a>';
I'm using a custom post type plugin that returns an uploaded file's attachment ID instead of its url.
I've been able to get the image to display using wp_get_attachment_image_src as outlined in the codex here http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src but my problem is getting it to play nicely with the code on the template page used to call the information from the custom post type.
Stripping it down to the basics, this is what calls the custom post type info from the template page:
<?php
$slideshowplatform = get_post_meta($post->ID, 'slideshowplatform', true);
foreach($slideshowplatform as $slide) {
echo '<img src="' . $slide['slide'] . '" />';
}
?>
I'm having difficulty reconciling this with what the codex provides:
<?php
$attachment_id = 8; // attachment ID
$image_attributes = wp_get_attachment_image_src( $attachment_id ); // returns an array
?>
<img src="<?php echo $image_attributes[0]; ?>">
It seems like something like the following should work, but I'm obviously missing something with the php syntax
<?php
$slideshowplatform = get_post_meta($post->ID, 'slideshowplatform', true);
foreach($slideshowplatform as $slide) {
$image_attributes = wp_get_attachment_image_src( $slide['slide'] );
echo '<img src="<?php echo $image_attributes[0]; ?>" />';
}
?>
Any thoughts would be appreciated, thanks.
I think this is what you want
if ( $post->post_type == 'slideshowplatform' && $post->post_status == 'publish' )
{
$attachments = get_posts(array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'exclude' => get_post_thumbnail_id()
));
if ($attachments) {
foreach ($attachments as $attachment) {
$thumbimg = wp_get_attachment_link( $attachment->ID, 'thumbnail-size', true );
echo $thumbimg;
//$image_attributes = wp_get_attachment_image_src( $slide['slide'] );
//echo '<img src="' . $image_attributes[0] . '" />';
}
}
}
I recently migrated a Wordpress network of two sites from a local MAMP server to an online one. Everything migrated perfectly, except the functionality of the Post-Thumbnail. The meta box is simply not appearing in any site, in any theme across the network.
I've tested a few different themes with post-thumbnail enabled, and the result is the same. No metabox is visible! I'm rather stumped, does anyone have suggestions?
The functions.php file for one of the sites is:
if(function_exists('add_theme_support')) {
add_theme_support('post-thumbnails', array( 'page', 'post', 'portfolio' ));
/** Register custom size */
add_image_size('page-image', 620, 620, false);
add_image_size('portfolio-image', 795, 575, true);
add_image_size('portfolio-thumbnail', 135, 94, true);
}
//------enable post thumbnail preview for custom columns
if ( !function_exists('fb_AddThumbColumn') && function_exists('add_theme_support') ) {
// for post and investments
function fb_AddThumbColumn($cols) {
$cols['thumbnail'] = __('Thumbnail');
return $cols;
}
function fb_AddThumbValue($column_name, $post_id) {
$width = (int) 200;
$height = (int) 125;
if ( 'thumbnail' == $column_name ) {
// thumbnail of WP 2.9
$thumbnail_id = get_post_meta( $post_id, '_thumbnail_id', true );
// image from gallery
$attachments = get_children( array('post_parent' => $post_id, 'post_type' => 'attachment', 'post_mime_type' => 'image') );
if ($thumbnail_id)
$thumb = wp_get_attachment_image( $thumbnail_id, array($width, $height), true );
elseif ($attachments) {
foreach ( $attachments as $attachment_id => $attachment ) {
$thumb = wp_get_attachment_image( $attachment_id, array($width, $height), true );
}
}
if ( isset($thumb) && $thumb ) {
echo $thumb;
} else {
echo __('None');
}
}
}
// for posts
add_filter( 'manage_posts_columns', 'fb_AddThumbColumn' );
add_action( 'manage_posts_custom_column', 'fb_AddThumbValue', 10, 2 );
// for portfolio
add_filter( 'manage_portfolio_columns', 'fb_AddThumbColumn' );
add_action( 'manage_portfolio_custom_column', 'fb_AddThumbValue', 10, 2 );
}
*/
function the_post_thumbnail_url($post_id) {
echo wp_get_attachment_url( get_post_thumbnail_id($post_id) );
}
You must switch to Network Admin mode
then go to
Settings > Network settings
near the end you'll find
Upload Settings > Media upload buttons
I checked Images to see the Featured Image box appears in my New Post admin page.
I had a similar problem with thumbnails when I migrated a Wordpress Network.
I've fixed it using this code, maybe it can help you. You need to add this code to functions.php
<?php
function get_timthumb_thumbnail($post_id = null) {
global $blog_id;
//we can access it, so why not use it?
$is_mu = WP_ALLOW_MULTISITE;
//if is true it means it's a wordpress MU site and we'll have to do some work
if($is_mu == true):
$thumbnail_id=get_the_post_thumbnail($post_id);
preg_match ('/src="(.*)" class/',$thumbnail_id,$link);
$imageParts = explode('files/', $link[1]);
if (!empty($imageParts[1])):
//check if the image is in the blog directory
if(#getimagesize('./wp-content/blogs.dir/' . $blog_id . '/files/' . $imageParts[1])):
$thumbnail = 'http://'.$_SERVER['HTTP_HOST'].'/wp-content/blogs.dir/' . $blog_id . '/files/' . $imageParts[1];
endif;
//check if the image is in the main uploads directory (you never know)
if(#getimagesize('./wp-content/uploads/' . $imageParts[1])):
$thumbnail = 'http://'.$_SERVER['HTTP_HOST'].'/wp-content/uploads/' . $imageParts[1];
endif;
else:
$imageParts = explode('uploads/', $link[1]);
if(#getimagesize('./wp-content/uploads/' . $imageParts[1])):
$thumbnail = 'http://'.$_SERVER['HTTP_HOST'].'/wp-content/uploads/' . $imageParts[1];
endif;
endif;
else:
$thumbnail_id=get_the_post_thumbnail($post_id);
preg_match ('/src="(.*)" class/',$thumbnail_id,$link);
$thumbnail = $link[1];
endif;
return $thumbnail;
}
?>
To use this function on the loop, I had added something like this:
<?php $thumb = get_timthumb_thumbnail($post->ID); ?>
<?php if(has_post_thumbnail()){ ?>
<img src="<?php echo $thumb;?>" class="thumb" />
<?php }else{
echo '<img src="'.get_bloginfo("template_url").'/img/boyds.png" class="thumbdflt" />';
}?>
Regards.