Image Flipper Woocommerce image resolution to low - php

I was looking for a way to make a "flip image" in the woocommerce product loop when you hover mouse, I know there are plugins that do this function but I think it's a very simple thing to need to use a plugin, I've been searching and I ended up finding a solution.
here's the code:
add_action( 'woocommerce_before_shop_loop_item_title', 'custom_loop_product_thumbnail', 10 );
function custom_loop_product_thumbnail() {
global $product;
$attachment_ids = $product->get_gallery_image_ids();
$secondary_image_id = $attachment_ids['0'];
echo wp_get_attachment_image($secondary_image_id);
}
and the css:
ul.products li.product a.woocommerce-LoopProduct-link img:nth-child(1){
position: absolute;
top: 0;
left: 0;
z-index: 1;
}
ul.products li.product a.woocommerce-LoopProduct-link img:nth-child(1):hover {
opacity: 0;
transition: opacity 0.5s;
}
my code did the job but the image is coming at a very low resolution
is there a way for it not to appear with such a low resolution?
I'm using a storefront child theme

nevermind I found out that some information was missing in my code here is the solution I found:
you need to set the image size: wp_get_attachment_image();
//set image size full size
wp_get_attachment_image($secondary_image_id, 'full');
//set image size medium size
wp_get_attachment_image($secondary_image_id, 'medium');
//set image size thumbanil size (default)
wp_get_attachment_image($secondary_image_id, 'thumbnail');
add_action( 'woocommerce_before_shop_loop_item_title', 'custom_loop_product_thumbnail', 10 );
function custom_loop_product_thumbnail() {
global $product;
$attachment_ids = $product->get_gallery_image_ids();
$secondary_image_id = $attachment_ids['0'];
echo wp_get_attachment_image($secondary_image_id, 'full');
}

Related

Using WordPress Featured Image as CSS Background Image

I'm trying to use the latest post featured image as a background image for a div on my homepage. Can anyone help with the php?
wp_get_recent_posts() will return a single, most recently published post using the parameters below.
I recommend placing the style in a stylesheet.
/* Get Recent Post */
$recent_post = wp_get_recent_posts(array(
'numberposts' => 1,
'post_status' => 'publish'
));
/* If Featured Image Set */
if ( has_post_thumbnail($recent_post[0]['ID']) ){
/* Get Image */
$image = wp_get_attachment_image_src( get_post_thumbnail_id($recent_post[0]['ID']), 'full');
/* Output Div with Image Set Inline, Use padding Top for Responsive Ratio Size */
echo '
<div class="featured-image-div" style="background-image:url('.$image[0].');"></div>
<style>
.featured-image-div{
padding-top: calc( 450 / 800 * 100% );
background-size: contain;
background-repeat: no-repeat;
}
</stle>
';
}

I making plugin where i wanna create option for user to choose color of badge, and have problem with calling it in css,didnt working

My main php file free-shipping-badge.php
/** REGISTER STYLES **/
add_action( 'wp_enqueue_scripts', 'fsb_stylesheet' );
function fsb_stylesheet() {
wp_register_style( 'view-style', plugins_url('/view/fsb_badge_style.php', __FILE__) );
wp_enqueue_style( 'view-style' );
}
/** ADD FREE SHIPPING BADGE SUFFIX AFTER PRICE **/
add_filter( 'woocommerce_get_price_suffix', 'fsb_suffix', 99, 4 );
function fsb_suffix( $html, $product, $price, $qty ){
$fsb_price = (float) $product->get_price(); // Regular price
$fsb_limit_price = esc_attr( get_option('fsb_limit_price_option') ); // Limit price
$fsb_badge_text = esc_attr( get_option('fsb_badge_text_option') ); // Text to display on badge
$fsb_badge_color = esc_attr( get_option('fsb_badge_color_option') ); // Text to display on badge
if($fsb_price > $fsb_limit_price){
$html .='</br>'.'<fsb_badge class="fsb_badge_view">' .$fsb_badge_text.' '.'</fsb_badge>';
return $html;
}
}
My style file:fsb_badge_style.php. Background did not get value from php variable $fsb_badge_color and it is main problem.
<?php
header('Content-type: text/css');
include('free-shipping-badge.php');
?>
.fsb_badge_view{
display: inline;
padding: .3em .6em .3em;
font-family: Arial;
font-size: 10.5px;
font-weight: bold;
line-height: 1.5;
color: #fff;
text-align: center;
white-space: middle;
vertical-align: baseline;
border-radius: .25em;
background-color: <?php echo $fsb_badge_color; ?>;
}
As #Tony Djukic said you can't enqueue a PHP file as a style sheet, it won't process the PHP. Something that is possible though is outputting the style inline in the header. An example:
function fsb_badge_style()
{
echo "<style type=\"text/css\">.fsb_badge_view{background-color: ".$fsb_badge_color.";}</style>";
}
add_action('wp_head', 'fsb_badge_style', 100);
This will write the style directly in the header.
<style type="text/css">.fsb_badge_view{background-color: #e7e7e7;}</style>
As an added bonus this will also work for users that may not have JavaScript enabled.

php Preventing dynamically added vertical images from being displayed horizontal

I have this problem with a simple cms I'm working on:
I have a simple php function getting image elements from a specified directory, and printing them to the html
<?php if($row["imgs"] == TRUE){ ?>
<div class="imrow">
<?php
$dir = $row["folder"];
$img = glob($dir."*.{jpg,jpeg,png}", GLOB_BRACE);
$tabing = 3;
$scale = sizeof($img);
for ($i = 0; $i < $tabing; $i++) {
echo '<img src="'.$img[$i].'" alt="image" />';
}
?>
</div><?php }//closing the first if of images ?>
(...)
<?php if($row["imgs"] == TRUE) { ?>
<div class="imrow">
<?php
for ($i = $tabing; $i < $scale; $i++) {
if(!($i % $tabing) && ($i!=0)){echo '</div><div class="imrow">';}
echo '<img src="'.$img[$i].'" alt="image" />';
}
?>
</div>
<?php }//second if closing ?>
The style for images and rows:
.imrow {
display: block;
}
.imrow img {
z-index: 10;
float: left;
height: 100%;
cursor: pointer;
transition: transform .5s ease;
box-shadow: 1px 1px 12px rgb(200, 200, 200);
}
And are laid out using a simple jQuery function
$(".imrow").each(function () { // for every row
var row = $(this); //it gives it a name
var rowW = row.width(); //it seals it's width
var imgW = 0; //it sets a image width variable
var fixH = 600; //and get a fixed amount
row.children().each(function () {
$(this).css("height", fixH); //apply fixed height to element in order to get ratio
imgW += $(this).width(); //get the width of this and
$(this).css("height", "100%");
arr.push($(this).attr("src")); // restore
});
row.css("height", rowW / (imgW / fixH) - 2);
});
The problem here is the fact that some of the added Vertical images, turn out horizontal
Here's how it looks in a folder
And how it turns out in the website:
EDIT: This is a php only issue from what I see, because when I analyze the elements in chrome, the images are flipped by default inside, as you all can see here:
So my first bet goes on glob doing something wrong.
Has anyone experienced it, or knows a way to make glob get everything properly?
Bare in mind that this issue only happens to some of the images, and is not depended on the format of the displayed image.
Any help would be extremely useful
It appears the problem was metadata stored in the images that describe the correct orientation.
There is a image-orientation css property that is supposed to be used to display the image in the correct orientation, but it doesn't seem to be supported in all browsers.
The only other solution at the moment is to edit the image's metadata with a metadata editor or, as you have, to open the images in photoshop and save them.

Set Background Image from Custom Meta in WordPress

I'm trying to get the home page of a WordPress theme I am building to change the background image on refresh based on 3 potential background images.
Each image is considered part of a larger "case study" that has a title, link, text, etc.
The case study fields are created through custom meta boxes on the home page. (I am using this to simplify the meta box creation process: https://github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress).
Long story short, the client wants 3 case studies on the home page to have corresponding background images. This is why I didn't use the featured image functionality, as each background goes with specific meta data.
The problem is, I can't figure out how to get the meta ID of the chosen background image and use that to set the background CSS.
Here is what I have so far:
/**
* Home Page Case Randomizer
*/
$GLOBALS['_home_case_number'] = rand(1, 3);
function home_case_number() {
if ( is_front_page() ) : // checks whether this is the home page
// get the meta image
$attachment_image = get_post_meta( get_the_ID(), '_home_case_background_' . $_home_case_number, true );
This is where I get confused. The above returns the appropriate image.jpg file. How do I massage this to work with the below code?
The below code is adapted from http://s2webpress.com/responsive-featured-image-function-in-wordpress-themes/, and I'd like to use it to make sure I'm serving images responsively.
// store the image sizes in an array
$img_sizes = array( 'thumbnail', 'medium', 'large', 'full' );
// grab the URL for each image size and store in a variable
foreach ( $img_sizes as $img_size ) {
${ 'img_src_' . $img_size } = wp_get_attachment_image_src( $attachment_image, $img_size );
}
echo '<style type="text/css">
.featured-image {
width: 100%;
min-height:350px;
background-image: url(' . esc_url( $img_src_medium[0] ) . ');
}
#media screen and ( min-width: 400px ) {
.featured-image {
background-image: url(' . esc_url( $img_src_large[0] ) . ');
}
}
#media screen and ( min-width: 1000px ) {
.featured-image {
background-image: url(' . esc_url( $img_src_full[0] ) . ');
}
}
</style>';
endif;
};
add_action( 'wp_head', 'home_case_number' );
So do I go about this by getting the meta ID of the background image? Or the attachment ID? I don't know how to do either at this point.
Any help is much appreciated!
I figured it out!
1) The CMB "plugin" saves the attachment ID as as another meta key by adding "_id" to the end of it, so I used that to get the attachment ID, and
2) I had variable scope issues. As soon as I called my global variable within my function, things started working. Here's the final code:
/**
* Home Page Case Randomizer
*/
$GLOBALS['_home_case_number'] = rand(1, 3);
function home_case_number() {
global $_home_case_number;
if ( is_front_page() ) : // checks whether this is the home page
// get the attachment thumbnail ID
$attachment_image = get_post_meta( get_the_ID(), '_home_case_background_' . $_home_case_number . '_id', true );
// store the image sizes in an array
$img_sizes = array( 'thumbnail', 'medium', 'large', 'full' );
// grab the URL for each image size and store in a variable
foreach ( $img_sizes as $img_size ) {
${ 'img_src_' . $img_size } = wp_get_attachment_image_src( $attachment_image, $img_size );
}
echo '<style type="text/css">
.featured-image {
width: 100%;
min-height:350px;
background-image: url(' . esc_url( $img_src_medium[0] ) . ');
}
#media screen and ( min-width: 400px ) {
.featured-image {
background-image: url(' . esc_url( $img_src_large[0] ) . ');
}
}
#media screen and ( min-width: 1000px ) {
.featured-image {
background-image: url(' . esc_url( $img_src_full[0] ) . ');
}
}
</style>';
endif;
};
add_action( 'wp_head', 'home_case_number' );
Hopefully this will help someone else out there.

Changing background images in Wordpress theme

I am using a Wordpress theme and want to make it change the background images automatically. So, I used some PHP code and tried to build it into the website, but something is not working and I cannot figure it out, as I am not very good with PHP.
functions.php:
<?php
$bilder = glob( "images/*.jpg" );
shuffle( $bilder );
$zufall = imagecreatefromjpeg($bilder[0]);
imagejpeg($zufall);
?>
... and simply in CSS of my Wordpress:
body {
background: url('<?php print $zufall ?>') no-repeat center center fixed;
}
But for some reason it just does not show the pictures and I am really off with my wisdom now. :(
Can you help me or tell me the error?
Thank you very much in advance.
This should work - you just need the filename of the image which is in the $bilder array
functions.php:
<?php
$bilder = glob( "images/*.jpg" );
shuffle( $bilder );
$zufall = $bilder[0];
?>
CSS:
body {
background: url('image/<?php print $zufall ?>') no-repeat center center fixed;
}

Categories