Targeting content image for responsive sizing - php

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

Related

Secondary featured image as div background

I know that in wordpress it is possible to use a featured image as the background of a div using the following code
background-image: url('<?php echo wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) ); ?>');
The question is: is there a way to use a secondary image as a background for this div?
I am using the Multiple Post Thumbnails plugin and I can display the secondary image with the code bellow
<?php
if (class_exists('MultiPostThumbnails')) :
MultiPostThumbnails::the_post_thumbnail(get_post_type(), 'secondary-image', NULL, 'secondary-featured-thumbnail');
endif;
?>
But is it possible to use this image as bakground?
I wanna something like this
I think what you are looking for is to get the URL of the secondary image, not the full post thumbnail.
In the documentation there is MultiPostThumbnails::get_post_thumbnail_url(get_post_type(), 'secondary-image');
https://github.com/voceconnect/multi-post-thumbnails/wiki/Frequently-Asked-Questions
So then on your page, you can do (Not the nicest):
<?php
if (class_exists('MultiPostThumbnails')) :
image_url = MultiPostThumbnails::get_post_thumbnail_url(get_post_type(), 'secondary-image');
?>
<style>
.myclass{
background-image: url('<?php echo image_url ?>');
}
</style>
<?php
endif;
?>
Or you can add that image_url to the html div container:
<div style="background-image: url('<?php echo image_url ?>');">
</div>
Depends on how you have it setup

wordpress advanced custom fields background image

I am trying to set an image uploaded through custom fields plugin and have it display as the background of a div (which is used in a slider).
However the image is not displaying...I have text in the custom fields and that is showing okay so I think its something to do with the line of code I am using to pull in the image.
I am trying to set the background of .slide1 with the image.
The custom field name is slide1_background.
HTML:
<div class="slide1" style="background-image:url('<?php the_field('slide_bg1'); ?>');">
<div class="slide1-cont"><p class="slide-text">
<h1><?php the_field('slide_title1'); ?></h1>
<img src="<?php bloginfo('template_directory')?>/images/line.png" /></p>
<p><?php the_field('slide_content1'); ?></p></div>
</div>
CSS:
.slide1{
background-repeat:no-repeat;
background-size:cover;
background-position:center;
height: 800px;
}
Look at the difference in your code in your question, where you try to set the background-image, compared to the code in your comment in another answer where you're setting it as an image source.
the_field('slide_bg1') returns an array, so you're trying to set the background image source as a PHP array which gets converted to a string as "Array" so in your HTML it'll look like: background-image:url('Array')
You need to get the field first, then echo the url element of the returned array as the source of the background image:
$image = get_field( 'slide_bg1' );
if ( !empty( $image ) ) { ?>
<div class="slide1" style="background-image:url('<?php echo $image['url']; ?>');">
<?php }
Use echo
<div class="slide1" style="background-image:url('<?php echo the_field('slide_bg1'); ?>');">

Wordpress use the_content() with out gallery?

Is it possible to use the_content() excluding the galleries? I want to make this because I need the gallery displayed with flexslider. I already integrate flexslider. If I want to show the content of the post, for example: some text, separated images and of course a gallery. It will display the gallery with flexslider and then the content with the text, images and the gallery again. I don't want the gallery duplicated.
<div class="entry-content">
<?php $gallery = get_post_gallery( get_the_ID(), false );?>
<div class="flexslider flexslider-gallery">
<ul class="slides slides-gallery">
<?php foreach( $gallery['src'] AS $src ){ ?>
<li>
<img alt="Gallery image" src="<?php echo $src; ?>" />
</li>
<?php } ?>
</ul> <!-- end .slides -->
</div> <!-- end .flexslider -->
<?php the_content(); ?>
</div><!-- .entry-content -->
Ok here is the solution:
Add to functions.php
function remove_shortcode_from($content) {
$content = strip_shortcodes( $content );
return $content;
}
and then call it when you need, in my case in content-gallery.php:
add_filter('the_content', 'remove_shortcode_from');
the_content();
remove_filter('the_content', 'remove_shortcode_from')
Two ways:
1) The WordPress Gallery is contained in a div with class .gallery. If flexsider does not also use that class, you could simply set your CSS to:
.gallery { display: none; }
2) You could get the content with get_the_content, parse it using a DOM parser and delete the Gallery.
Option #1 is less efficient, but avoids the complexity of parsing the DOM.
Neither method is guaranteed to work if the site is use a non-standard gallery plugin that is creating non-WP HTML.

Dynamic home page image in wordpress

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.

Force loaded / displayed image to link to an html ( my post ) page in colorbox

I'm using custom meta data to display images as a post in wordpress. When I click the image on my main page it opens in colorbox and this is perfect.
WHAT IS HAPPENING : now, with the image loaded into the colorbox, when I click on the image it cycles through the images on my home page ( NOT what I would like ).
WHAT I WOULD LIKE TO HAPPEN : with the image loaded into the colorbox, when I click on it, it will act as a link and load the post ( associated with that image ) into the colorbox.
I'm not clear on how to do this. The code I'm using to display the image meta data for the posts is:
<?php
// check for spine image
$spine = get_post_meta($post->ID, 'spine image', $single = true);
// check for spine class
$spine_class = get_post_meta($post->ID, 'spine class', $single = true);
?>
<div style=" #position: center; #left: 50%; height: 650px; display: table-cell; vertical-align: middle;">
<div class="post" id="post-<?php the_ID(); ?>">
<div class="entry">
<?php
// If there is a spine image, display it as the post
if($spine !== '') { ?>
<p>
<a rel="bookmark" href="<?php echo $spine; ?>" title="<?php the_title_attribute(); ?>">
<img src="<?php echo $spine; ?>" class="<?php if($spine_class !== '') { echo $spine_class; } else { echo "left"; } ?>"
height="400" onMouseOver='resizeImg(this, 150)' onMouseOut='resizeImg(this)'
/>
</a>
</p>
<?php } // end if statement
// if there's not a spine image
else { echo ''; }
?>
</div>
</div>
</div>
And the function I'm using for colorbox is:
<script type="text/javascript">
$(document).ready(function(){
//Examples of how to assign the ColorBox event to elements
$("a[rel='bookmark']").colorbox({transition:"fade"});
});
</script>
So that the links with the attributes of 'bookmark' will open in the color box. How now can I make it so that the image in the colorbox links to the associated web page ( or act as a link to any web page... one step at a time )?
I hope this makes sense and thank you for any help,
mlord
My question seems overly complicated once I realized how to do this.
The end result is that I used the ".inline" HTML structured example from the website ( http://colorpowered.com/colorbox/core/example4/index.html ). I can put the image from the wordpress loop href'd to the permalink and call it inline for each image / post to be displayed in the colorbox. Then I included the general colorbox class on that link which opens the post inside of the colorbox as well. Awesome!
Keep care,
Matthew

Categories