Add and remove default and hover background images on hover through ACF - php

This is my very first thread, I'll try to be as precise as possible.
I'm very new to working with php, and my current project is therefor quite a challenge.
I have 2 products, I would like to be able to, through ACF, to exchange both the default BG image and the hover image.
ATM I can exchange the hover, but its visible at all time.
If I add an image through the wysiwyg editor, it wont set as bg image, but on top of the content.
Heres what I have:
These are the two products. The left one has the hover bg img, shown at all time. The right one doesnt have any for now
Here you can see the ACF on the relevant page
Here is what it looks like, if I add an image through the wysiwyg editor (the hover img shows in the background)
Here is my css, for the products
Here what the code looks like:
<?php
/**
* Variables
*/
$introductiontitle = get_sub_field('introduction_title');
$introductionsubtitle = get_sub_field('introduction_subtitle');
$introductionsubtitle2 = get_sub_field('introduction_subtitle_2');
$hover_image1 = get_sub_field('hover_element_1_background');
$hover_image2 = get_sub_field('hover_element_2_background');
<!-- Introduction
-------------------------------------------------------------->
<div class="container-fluid pt-5 pb-8">
<div class="container">
<div class="row">
<div class="col-12 text-center mb-3">
<h1 class="font-weight-bold mb-3">
<?php echo $introductiontitle; ?>
</h1>
<p>
<?php echo $introductionsubtitle; ?>
</p>
<p>
<?php echo $introductionsubtitle2; ?>
</p>
</div>
<div class="col-12 col-md-6">
<div class="col-12 product chart py-5 mb-3 bg-center bg-cover"
<?php if ($hover_image1) { ?>
style="background-image: url(<?php echo $hover_image1 ?>"
<?php } ?>
>
<?php the_sub_field('hover_element_1'); ?>
</div>
</div>
<div class="col-12 col-md-6">
<div class="col-12 product chart py-5 mb-3 bg-center bg-cover"
<?php if ($hover_image2) { ?>
style="background-image: url(<?php echo $hover_image2 ?>"
<?php } ?>
>
<?php the_sub_field('hover_element_2'); ?>
</div>
</div>
</div>
</div>
I hope this all made sense!? Otherwise just let me know, and I'll explain it better.
Thanks in advance

I would've posted a comment for clarification but don't have enough points for that yet so decided I'd post an answer as it might be what you're trying to achieve.
Using a combination of layered elements and opacity you should be able to achieve an effect where the background image changes.
Here's an example using solid colours, but they can easily be changed to other elements (e.g. your custom background images, and images):
https://codepen.io/epsilon42/pen/NBNVad
HTML:
<div class="product" style="background: #ccc;"><!-- change inline style to to ACF initial background-image -->
<div class="hover-background" style="background: #666;"><!-- change inline style to to ACF hover background-image --></div>
<div class="copy">
<h2>test</h2>
<p>lorem ipsum lorem ipsum</p>
<div class="icon">
<div class="initial-icon" style="background: #f00;"><!-- change this div to initial <img> --></div>
<div class="hover-icon" style="background: #b4d455;"><!-- change this div to hover <img> --></div>
</div>
</div>
</div>
CSS:
.product {
width: 300px;
height: 300px;
position: relative;
}
.product .hover-background,
.product .copy {
width: 100%;
height: 100%;
position: absolute;
}
.product .hover-background {
opacity: 0;
transition: 0.2s all ease-in;
}
.product .copy {
text-align: center;
}
.product .copy .icon {
width: 50px;
height: 50px;
margin: 0 auto;
position: relative;
}
.product .copy .icon .initial-icon,
.product .copy .icon .hover-icon {
width: 100%;
height: 100%;
position: absolute;
}
.product .copy .icon .hover-icon {
opacity: 0;
transition: 0.2s all ease-in;
}
.product:hover .hover-background {
opacity: 1;
}
.product:hover .copy .icon .hover-icon {
opacity: 1;
}
You will need to adjust the CSS a bit to suit your situation.

Related

How to implement mobile menu on desktop

So I have:
<div class="module left site-title-container">
<?PHP
shapely_get_header_logo();
?>
</div>
<div class="module widget-handle mobile-toggle right visible-sm visible-xs">
<i class="fa fa-bars"</i>
Which I changed to
<div class="module left site-title-container">
<?php
shapely_get_header_logo();
?>
</div>
<div class="module widget-handle mobile-toggle right visible-sm visible-xs">
<a id="mobile-nav" href="#">
<div class="container1" onclick="myFunction(this)">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</div>
</a>
<script>
function myFunction(x){
x.classList.toggle('change');
}
</script>
Css
.container {
display: inline-block;
cursor: pointer;
}
.bar1, .bar2, .bar3 {
width: 35px;
height: 5px;
background-color: #333;
margin: 6px 0;
transition: 0.4s;
}
.change .bar1 {
-webkit-transform:
rotate(-45deg)
translate(-9px, 6px);
transform: rotate(-45deg)
translate(-9px, 6px);
}
.change .bar2 {opacity: 0;}
.change .bar3 {
-webkit-transform:
rotate(45deg)
translate(-8px, -8px);
transform: rotate(45deg)
translate(-8px, -8px);
}
This works fine on mobilebut not sure how to implement on desktop. I have show mobile menu on desktop like so:
<header id="masthead" class="site-header<?php echo get_theme_mod( 'mobile_menu_on_desktop', true ) ? ' mobile-menu' : ''; ?>" role="banner">
<div class="nav-container">
but still, it's not showing...
So how do I implement the above menu bars animation on desktop? Is it just a case of changing the a id?
Something is there on desktop but it seems to be hidden as, when I click where the menu bars should be, the menu responds but no bars are actually visible.

PHP images are not displaying properly

Here is my code.
<?php
$q = $db->query("SELECT * FROM images WHERE user_id = $id");
while ($result = $q->fetch_assoc()):
?>
<div class="box">
<div class="panel">
<div class="image" style="background-image: url(uploads/<?= $result['image'] ?>)">
<div class="overlay">
<div class="content">
<strong>Title</strong>
<p>Some text</p>
</div>
</div>
</div>
</div>
</div>
<?php
endwhile;
?>
Having a problem with the images are not displaying the way I expected to, every time I insert or add a new image, it always go to the next row.
This is what I want for the layout.
But instead it keeps on displaying on the next row every time I add new image.
I also tried put it in a row class, but nothing happens.
Here is my CSS code.
.box {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
}
.panel {
height: 230px;
min-width: 250px;
margin: 10px;
flex: 1 1 30%;
}
.image {
background-size: cover;
height: 230px;
}
.overlay {
position: relative;
background-color: rgba(15, 10, 36, 0.5);
height: 230px;
}
.content {
text-align: center;
position: absolute;
color: #fff;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
Solved this by moving the div container outside the php code.
Check the code below.
<div class="box"> <!-- This div -->
<?php
$q = $db->query("SELECT * FROM images WHERE user_id = $id");
while ($result = $q->fetch_assoc()):
?>
<div class="panel">
<div class="image" style="background-image: url(uploads/<?= $result['image'] ?>)">
<div class="overlay">
<div class="content">
<strong>Title</strong>
<p>Some text</p>
</div>
</div>
</div>
</div>
<?php
endwhile;
?>
</div> <!-- and this div -->
I've tried different approach but it did not work with me,
I hope this can help other people who's having the same problem as mine. Just put the div container outside the iteration.

automatic (not hover) css overlay depending on database field content

I want to make the following bootstrap code automatically show an overlay for two out of three options contained in a database field. They are SOLD, CONTRACT, and NO.
I want different overlays for SOLD and CONTRACT, and no overlay for NO
I have searched for overlays, most tutorials have on hover events, and I am lost as to how to do this.
<div class="row">
<div class="col-md-6 col-md-offset-3">
<a href="listing_show.php?ID=<?php echo($Recordset1->getColumnVal("ID")); ?>">
<img src="images/<?php echo($Recordset1->getColumnVal("IMAGE")); ?>" class="img-responsive center-block sold" alt="<?php echo($Recordset1->getColumnVal("TITLE")); ?>"/>
</a>
</div><br><br>
</div>
I worked it out through a couple of js fiddles on other questions here, if it helps anyone here is the code I added...
html
I added a thumbnail div and a caption div underneath the image both conditional on the contents of the database field.
<?php if($Recordset1->getColumnVal("SOLD") == "YES") { ?>
<div class="caption post-content-sold"></div>
<? } ?>
<?php if($Recordset1->getColumnVal("SOLD") == "CON") { ?>
<div class="caption post-content-contract"></div>
<? } ?>
Then in the CSS I added code to make the thumbnail relative, and then added the two classes to style the image relating to the database field
.thumbnail {
position: relative;
}
.post-content-sold {
background: rgba(255, 255, 255, 0.8) none repeat scroll 0 0;
opacity: 0.8;
top:0;
left:0;
min-width: 100%;
max-height: 98%;
position: absolute;
color: #ffffff;
content:url(http://www.mtnlakerealty.com/sold.png);
}
.post-content-contract {
background: rgba(255, 255, 0, 1) none repeat scroll 0 0;
opacity: 1;
top:5%;
left:5%;
max-width: 50%;
max-height: 50%;
position: absolute;
color: #ffffff;
content:url(http://www.mtnlakerealty.com/contract.png);
}

How do I add a href to an image in php on wordpress linking to another page?

I am extremely new to coding and this is my first post here and I cannot figure this out for the life of me. I even had a friend who codes try but he also got stuck. The image uses a hover webkit transform to make the image a little bigger when hovered over. It does this in CSS:
.circle img {
position:relative;
display:block;
width:200px;
height:200px;
-webkit-border-radius: 100px;
border-radius: 100px;
overflow:hidden;
margin:0px auto 20px;
-webkit-transform: scale(1,1);
-webkit-transition-timing-function: ease-out;
-webkit-transition-duration: 250ms;
-moz-transform: scale(1,1);
-moz-transition-timing-function: ease-out;
-moz-transition-duration: 250ms;
}
circle:hover img {
-webkit-transform: scale(1.05,1.07);
-webkit-transition-timing-function: ease-out;
-webkit-transition-duration: 250ms;
-moz-transform: scale(1.05,1.07);
-moz-transition-timing-function:
}
The PHP:
<div class="container">
<div class="sixteen columns">
<div class="five columns fma alpha">
<div class="fma-content">
<div class="circle">
<?php echo(types_render_field("home-fma-1-image", array())); ?>
</div>
<h3><?php echo(types_render_field("home-fma-1-headline", array())); ?></h3>
<p><?php echo(types_render_field("home-fma-1-copy", array())); ?></p>
</div>
</div>
</div>
</div>
The image is the "home-fma-1-image" I think...
EDIT: I tried this:
<div class="circle">
<?php echo(types_render_field("home-fma-1-image", array())); ?>
</div>
It made the whole thing a link, the image, the text below, and the other text below. This is a problem because my theme makes link text yellow and all caps which looks really weird for a paragraph.
Based on the context, I'm assuming this is an image built into the theme? If so, it should be as easy as adding an a tag around the php code, and using a href attribute. See below:
<div class="circle">
<?php echo(types_render_field("home-fma-1-image", array())); ?>
</div>
Also, if you do this, change the css to incorporate the a tag.
.circle a img, rather than .circle img
Let me know if you're looking for something else, of course.

How to I make my the height of my sidebar and main content consistent?

Currently I'm using wordpress for my website. The height of my sidebar and main content area doesn't align. How to I make the height of my sidebar and the main content consistent? Example pageA has longer content but the sidebar is shorter, so the unused sidebar area will fill up with blank white space.
Below is my index.php code.
<?php get_header(); ?>
<div id="table">
<div id="mainsidebar">
<?php get_sidebar(); ?>
</div>
<div id="maincontent">
<div valign="top">
<?php wowslider(9); ?>
</div>
<div valign="bottom">
<?php include('main-content.php'); ?>
</div>
</div>
</div>
<?php get_footer(); ?>
</body>
Here the sample image link that I've uploaded.
Thanks.
If you don't mind using javascript:
$(function(){
var main = $('#maincontent').height(),
sidebar = $('#mainsidebar').height();
if(main > sidebar){
$('#mainsidebar').css({'min-height':main});
}
});
I use equalize.js for this. Super simple to setup and works fantastically!
Put <div id="mainsidebar"></div> and <div id="maincontent"></div> in another div. Make that div as same height of maincontent div. and give height:100% for mainsidebar div.
Idea from here.
As posted here, you can do this with CSS:
.container {
overflow: hidden;
}
.column {
float: left;
margin: 20px;
background-color: grey;
padding-bottom: 1000px;
margin-bottom: -1000px;
}
HTML:
<div class="container">
<div class="column">Some content!
<br/>Some content!
<br/>Some content!
<br/>Some content!
<br/>Some content!
<br/>
</div>
<div class="column">Something</div>
</div>
Or try the following:
From: Two floating divs side by side, same height
HTML:
<div id="table">
<div id="mainsidebar">
<p>Main Sidebar Content</p>
</div>
<div id="maincontent">
<p>Main Content</p>
</div>
</div>​
CSS:
#table { position: relative; }
#mainsidebar { /* shorter column */
position: absolute;
top: 0; bottom: 0; left: 0;
width: 38%;
padding: 2%;
}
#maincontent { /* taller column */
margin: 0 0 0 42%;
width: 58%;
}​
You don`t need to make it the same height.
Set table background "white" and it will look like both have the same height
#table {background: #fff;}

Categories