I'm building a website for a good friend of mine and i'm stuck on this one.
I want an adaptable background image on the homepage so he can customize it on the customize page in the wordpress admin page.
My code now looks like this:
get_header(); ?>
<div id="slides">
<div class="slides-container">
<img src="<?php bloginfo('template_directory'); ?>/images/Home.jpg" alt="Cinelli">
</div>
<nav class="slides-navigation">
<img src="<?php bloginfo('template_directory'); ?>/images/right.png" alt="left">
<img src="<?php bloginfo('template_directory'); ?>/images/left.png"alt="right">
</nav>
</div>
<div class="welcometext">
<h1><?php echo get_bloginfo( 'name' );?></h1>
<?php echo bloginfo('description');?>
</div>
<?php get_footer(); ?>
now the image tag needs to be adaptable.
Is this possible?
It may be easier to pull the image from a WordPress Page's Featured Image than to use the Customize page.
To enable Featured Images, add this line to your theme's functions.php file: add_theme_support( 'post-thumbnails' );
Now upload an image to the Featured Image area of a WordPress page. This can be any page, you just need to take note of the Page's ID. To get the Page ID, go to the edit screen for the Page and look at the URL in your browser. It will end with something like post.php?post=34&action=edit where the number after post= is the Page's ID, in this example it's 34.
Now modify your image code:
<?php $feat_image = wp_get_attachment_url( get_post_thumbnail_id(34, 'full') ); // replace '34' with your Page's ID ?>
<img src="<?php echo $feat_image; ?>" alt="Cinelli">
Replace 34 with your Page's ID. If you are using this code within the Loop, you can replace 34 with $post->ID.
The 'full' in the first line tells it which size of the image to use. 'Full' will use the original image you upload, or you can use 'large', 'medium', 'thumbnail', or any custom image size you've set up.
Related
I have a site in WordPress, and I want to display the WordPress post first image which is displayed in the post but remember not the featured images. so how to show it in the post and i want my post like below
this is how blog design should be
This is the code
<div class="col-md-3 small-post-img">
<a href="<?php the_permalink() ?>">
<?php the_post_thumbnail(); ?>
</a>
</div>
It not taking the the_post_thumbnail code because there is no any featured image in the post, but I have added the images in the inside post content. I want that post images as featured image.
Put this in your theme's functions.php file
function ash_post_first_img($post_content){
preg_match_all('/<img[^>]+>/i', $post_content, $content_all_images);
preg_match_all('/src="([^"]+)"/i', $content_all_images[0][0], $content_image);
if (isset($content_image[1][0])) {
return $content_image[1][0];
} else{
return 'http://example.com/defaultimage.jpg';
}
}
and use this function ash_post_first_img(get_the_content('')); where you want to display image's url "single.php or post.php"
<div class="col-md-3 small-post-img">
<a href="<?php the_permalink() ?>">
<img src="<?php echo ash_post_first_img(get_the_content('')); ?>">
</a>
</div>
Change "http://example.com/defaultimage.jpg" to the default image url if no image is found.
I am using 'Aqua Resizer' (aq_resizer.php) to resize my thumbnails on my Wordpress site. My posts are made from a loop in single.php with
<?php get_template_part( 'content', 'single' ); ?>
This pulls from content-single.php which has the code for my posts
HTML
<div id="post-<?php the_ID(); ?>" <?php post_class('col-md-12'); ?> >
<div>
<h2><?php the_title(); ?></h2>
<h3><?php the_category(', '); ?></h3>
<?php the_content(); ?>
</div>
<?php
$thumb = get_post_thumbnail_id();
$img_url = wp_get_attachment_url( $thumb,'full' ); //get full URL to image (use "large" or "medium" if the images too big)
$image = aq_resize( $img_url, 1200, 720, true ); //resize & crop the image
?>
<?php if($image) : ?>
<img class="img-responsive" src="<?php echo $image ?>"/>
<?php endif; ?>
</div><!-- /#post -->
Everything works fine (the resizer function works on the featured post which is brought in with <?php the_content(); ?>. The image scales up/down as the window resizes
The effect works because class="img-responsive" is applied to the featured-image of the post.
I have images in the content of the post. I want them to act the same way (right now they are just brought in at their original size) I need the class img-responsive to be applied to the images in <?php the_content(); ?>
Applying the CSS classes to images can be done from within the post itself, just edit the image and put CSS class img-responsive.
If you cannot edit/modify posts (or it is just too much work) then second option is to write a small code snippet in your theme's custom function.php file (this is true only when you display post in your loop):
<?php
the_post_thumbnail('thumbnail', array('class' => 'img-responsive'));
See https://codex.wordpress.org/Function_Reference/the_post_thumbnail for more details.
And the third option is to use jQuery in your header.php file, this way all images on page will get responsive class (however this may not be what you want, especially if you have backgrounds, logos, menu images, etc. and it will require JavaScript to be enabled in client's browser):
<script type="text/javascript">
jQuery(function() {
jQuery(img).addClass('img-responsive');
});
</script>
The last option I can think of, is by using pure CSS:
.content img { height: auto; max-width: 100%; }
Where .content is the area that contains your post content.
Note: You may also want to override the .wp-caption class as well like so.
.wp-caption { width: auto !important; }
I am trying to build a simple gallery with WordPress Advanced Custom Fields and the repeater field plugin. I need to display a thumbnail image which you click to enlarge the main image (I am using Fancybox). Does anyone know whether it is possible to get the link to the image thumbnail WordPress automatically generates?
I can get the main image link:
/wp-content/uploads/2014/12/slide1.jpg
But need to get this image link:
/wp-content/uploads/2014/12/slide1-150x150.jpg
Here is my code:
<?php if(get_field('gallery')): ?>
<ul>
<?php while(has_sub_field('gallery')): ?>
<a class="fancybox" href="<?php the_sub_field('image'); ?>" data-fancybox-group="gallery" title="Lorem ipsum dolor sit amet"><img src="<?php the_sub_field('image'); ?>" alt="" width="200px;" height="auto"/></a>
<?php endwhile; ?>
<?php while(has_sub_field('video_link')): ?>
<a class="fancybox-media" href="<?php the_sub_field('video_link_snippet'); ?>"><img src="<?php the_sub_field('video_image'); ?>"/></a>
<?php endwhile; ?>
</ul>
<?php endif; ?>
You can absolutely do this with Advanced Custom Fields and Wordpress. To do so, go into the custom fields and change the sub field 'image' so that it returns the Image Object instead of the Image URL. See 'return value' here.
Once this field returns the object, try var_dumping the field to see your URL options:
var_dump(the_sub_field('image'));
This should show you the different thumbnail sizes enabled in your system and allow you to easily get the thumbnail you'd like. One possible example:
// Get image object with all sizes...
$image = the_sub_field('image');
$size = 'thumbnail';
// Get URL from sizes based on our $size var
$url = $image['sizes'][$size];
// Now we have the thumbnail URL
echo $url;
Let me know if you run into problems! For more documentation and many more options, try the Advanced Custom Fields documentation for the Image field.
I tired to make WordPress featured image responsive with bootstrap. The way to make the image responsive with bootstrap is
<img src="..." class="img-responsive" alt="Responsive image">
I tired to use this method in wordpress when showing the featured image dynamically.
<img src="<?php the_post_thumbnail( 'jumbo_thumbnail'); ?>" class="img-responsive" alt="Responsive image">
But it doesnot work. I know if I put a link there instead of <?php the_post_thumbnail( 'jumbo_thumbnail'); ?> it will work. But I am dynamically getting the featured image, and this method doesnot work.
You're close. Get rid of your img tag altogether and use this:
<?php
the_post_thumbnail( 'jumbo_thumbnail', array(
'alt' => 'Responsive image',
'class' => 'img-responsive'
) );
?>
the_post_thumbnail function will output the img tag for you.
I am using a wordpress theme that has a custom post type called "link" which simply produces a post where the title links to an outside link you specify. It also includes some text in the body of the post. I would like to also be able to display an image. Right now it is not showing the image.
Here is the code:
elseif ( has_post_format( 'link' )) {
if(get_post_meta($post->ID, '_format_link_url', true)!=''){
$link = get_post_meta($post->ID, '_format_link_url', true);
}else{
$link = '';
}
?>
<h2 class="entry-title">
<a href="<?php echo $link; ?>" target="_blank" title="Follow this link">
<?php the_title(); ?>
</a>
</h2>
<p>
Is there anything I can add to make it display an image as well as text?
Thanks!
I can't see anywhere on this example where you are using an tag to insert an image. The img tag has two required attributes, src and alt. Src is the path to your image, whereas alt is the alternate text for the image. So:
<img src="images/myImage.png" alt="This Is My Image"/>
Hope that helps!