Wordpress PHP Condition - php

My homepage has a vertical slider that displays the titles of 5 posts at the top of my homepage. Each post that displays has a background image that is shown behind it. It is currently working well, although I would to display a different background image behind each post display. I have no clue as to how to do this using PHP code. I have posted the code below that shows the file that contains the JCarousel functionality. I'm guessing I need to setup some type of condition that allows a new background image to display with each 'list' item?
Here's the code:
<div id="home-gallery-wrapper">
<ul id="home-gallery" class="jcarousel-skin-home clearfix">
<?php
global $post;
if ( get_option('minimax_hl_show') == '1' ) {
$my_query = get_pages('include='.get_option('minimax_hl_ID').'');
} else {
$my_query = get_posts('include='.get_option('minimax_hl_ID').'');
}
foreach($my_query as $post) :
setup_postdata($post);
?>
<li style="width:910px"><div style="
background:url('<?php bloginfo('template_directory'); ?>/images/banner3.png')
center top no-repeat;">
<table align="center" style="width:910px;
height:200px">
<tr>
<td style="vertical-align:top">
<div style="width:90%; display:block; margin-left:auto; margin-right:auto">
<h2 style="text-align:center; border:none; text-decoration:none; font-family:calligraffiti;
margin-top:15px; font-size:1.8em; line-height:35px; color:#fff; position:relative; z-index:10">
<a style="color:#fff; border:none; text-decoration:none"
title="Permanent Link to <?php the_title(); ?>" href="<?php the_permalink(); ?>"
rel="bookmark"><?php the_title(); ?></a></h2>
</div>
</td></tr>
</table></div>
<?php
$key="show-video";
if ( get_post_meta($post->ID, $key, true) != '' ) {
?>
<div class="video"><?php echo get_post_meta($post->ID, $key, true); ?></div>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("embed").attr("wmode", "transparent");
});
</script>
<?php
} else if ( has_post_thumbnail() ) {
?>
<a title="Permanent Link to <?php the_title(); ?>"
href="<?php the_permalink() ?>"><?php the_post_thumbnail(); ?></a>
<?php } ?>
</li>
<?php
endforeach;
?>
</ul>
</div><!-- end home-gallery-wrapper -->
<script type="text/javascript">
function mycarousel_initCallback(carousel)
{
// Disable autoscrolling if the user clicks the prev or next button.
carousel.buttonNext.bind('click', function() {
carousel.startAuto(0);
});
carousel.buttonPrev.bind('click', function() {
carousel.startAuto(0);
});
// Pause autoscrolling if the user moves with the cursor over the clip.
carousel.clip.hover(function() {
carousel.stopAuto();
}, function() {
carousel.startAuto();
});
};
jQuery(document).ready(function() {
jQuery('#home-gallery').jcarousel({
vertical: true,
scroll: 1,
easing: 'easeInOutBack',
auto: 2,
wrap: 'last',
initCallback: mycarousel_initCallback,
animation: 800
});
});
</script>

Create 5 different banners with the colours of your choice and call them banner1.png, banner2.png, banner3.png etc.
Then replace
foreach($my_query as $post) :
setup_postdata($post);
?>
<li style="width:910px"><div style="
background:url('<?php bloginfo('template_directory'); ?>/images/banner3.png')
center top no-repeat;">
With this
$i = 0;
foreach($my_query as $post) :
setup_postdata($post);
?>
<li style="width:910px"><div style="
background:url('<?php bloginfo('template_directory'); ?>/images/banner<?php $i++; ?>.png')
center top no-repeat;">

Related

Slider not visible in PHP but in HTML works

I am making a shortcode of a slider that shows my WP featured posts and that has the possibility to open the details of each post through a hidden div. The slider is based on this one from w3schools (from which you can see the code and test online from this address: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_slideshow
The problem is that the slider is only visible in php once I click the next arrow and from there it works correctly. I don't know why, since in HTML it works fine.
The error apppears: Cannot read property 'style' of undefined in
slides[slideIndex-1].style.display = "block";
My CSS:
#slider {
width: 70%;
margin: auto;
position: relative;
font-size: 15px;
}
.slide {display: none;}
My PHP:
<?php $query = new WP_Query( $args );
ob_start();?>
<div id="slider" class="slider">
<div class="arrows">
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a></div>
<?php
while( $query->have_posts() ) :
$query->the_post();
$image = wp_get_attachment_image_src(get_post_thumbnail_id($post-
>ID), '');
$featuredID = get_post_thumbnail_id();?>
<div class="slide fade" id="slide">
<h2 class="zz-sc-jscss"><?php the_title(); ?></h2>
By <?php the_author(); ?> on <?php echo get_the_date( 'l, F d, Y' );
?>
<?php echo the_post_thumbnail();?>
<?php
echo "<div"; echo ' class="n"'; echo "id=".$featuredID.">";
$info = $featuredID;echo "</div>";
?> <div class="open">
<?php echo get_post_field('post_content', $post->ID);?>
<span class="cerrar">X</span></div></div>
<?php endwhile;
wp_reset_postdata();
return ob_get_clean();
}
My JS:
var slideIndex = 1;
showSlides(slideIndex);
// Next/previous controls
function plusSlides(n) {
showSlides(slideIndex += n);
}
// Thumbnail image controls
function currentSlide(n) {
showSlides(slideIndex = n);
}
function showSlides(n) {
var i;
var slides = document.getElementsByClassName("slide");
if (n > slides.length) {slideIndex = 1}
if (n < 1) {slideIndex = slides.length}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
slides[slideIndex-1].style.display = "block";
}
The problem seems to be in the "slider" div, it does not seem to be closed. Also try moving it after the slide generation loop.
<div id="slider" class="slider"></div> <--missing
<div class="arrows">
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
</div>

How to display top sellers by date range in Magento

Is there anyway to display best sellers by date range? In example 30 days, 60 days, etc
This is my current bestsellers.phtml and we have no idea what range this data is pulling from
<?php
$params = $this->getData();
if(array_key_exists('limit', $params)) {
$limit = $this->getData('limit');
} else {
$limit = 3;
}
$products = $this->getBestSellers($limit);
?>
<script type="text/javascript" src="<?php echo $this->getSkinUrl('js/overlay_jquery.js');?>"></script>
<script type="text/javascript" src="<?php echo $this->getSkinUrl('msslider/slider2/stepcarousel.js') ?>"></script>
<script type="text/javascript">
stepcarousel.setup({
galleryid: 'mygallery', //id of carousel DIV
beltclass: 'belt', //class of inner "belt" DIV containing all the panel DIVs
panelclass: 'panel', //class of panel DIVs each holding content
autostep: {enable:false, moveby:1, pause:2000},
panelbehavior: {speed:500, wraparound:true, wrapbehavior:'slide', persist:true},
defaultbuttons: {enable: false, moveby: 1, leftnav: ['<?php echo $this->getSkinUrl('msslider/slider2/arrowl.png')?>', -15, 100], rightnav: ['<?php echo $this->getSkinUrl('msslider/slider2/arrowr.png')?>', -25, 100]},
contenttype: ['inline'] //content setting ['inline'] or ['ajax', 'path_to_external_file']
})
</script>
<div class="category-products home-page-listing">
<div id="mygallery" class="stepcarousel">
<div class="belt products-grid">
<?php if(is_array($products) && count($products)): ?>
<?php $i = 0; foreach($products as $product): ?>
<div class="panel">
<div class="product-topbg">
<div class="product-bottombg">
<div class="product-midbg">
<h2 class="product-name" style="margin: 5px 0px 15px;">
<?php echo $product['category_name']; ?>
</h2>
<div class="overlay"
id="overlay-<?php echo $i; ?>"
>
<a href="<?php echo $product['url']; ?>"
title="<?php echo $product['name']; ?>"
>
<img src="<?php echo $product['image_url']; ?>"
width="160" height="160"
alt="<?php echo $product['name']; ?>"
/>
</a>
</div>
<h2 class="product-name" style="margin: 15px 0px 5px;">
<a href="<?php echo $product['url']; ?>"
title="<?php echo $product['name']; ?>">
<?php echo $product['name']; ?>
</a>
</h2>
<div class="overlay-show"
id="overlay-show-<?php echo $i;?>"
>
<div class="product-price">
<?php echo $product['price']; ?>
</div>
<div class="price-bottombg"></div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
var $i = jQuery.noConflict();
$i("#overlay-<?php echo $i;?>").hover(function () {
if ($i("#overlay-show-<?php echo $i;?>").is(":hidden")) {
$i("#overlay-show-<?php echo $i;?>").slideDown("fast");
} else {
$i("#overlay-show-<?php echo $i;?>").slideUp("fast");
}
});
</script>
</div>
<?php $i++; endforeach;?>
<?php endif; ?>
</div>
</div>
<script type="text/javascript">
decorateGeneric($$('ul.products-grid'), ['odd', 'even', 'first', 'last']);
</script>
</div>
Thank you for any help with this code.
As I can see, this is the block that belongs to some 3-d party extension (or smb might have written it). That's why you should locate the block and correct the collection of products it receives via the getBestSellers method.
If you want to see only the last 3 updated products, the easiest way is to add 'order DESC' by 'updated_at' to the collection
Thus, the first 3 products will be the last updated on the list. It will look like that:
$collection->setOrder('update_at', Varien_Data_Collection_Db::SORT_ORDER_DESC);
If you want to get populated some other field, that is different from the 'updated_at', just use its name instead of 'updated_at'.
Note, that you should implement all the changes before the collection gets downloaded.
The block can be find via 'updated_at' or by means of the layout. Additionally, you can search for updated_at inside of the template. Use the most convenient for you method.

Dynamically change background image in the CSS during WordPress loop (media queries)

I currently have a carousel on my page inside a Wordpress loop. So as it loops through it changes the background image according to which post I am currently on in the loop. However, I want to use media queries to load smaller images if for instance I the screen size is smaller.
This is what I currently have:
while( $loop->have_posts() ) : $loop->the_post();
$class = '';
// Custom Posts
$carousel_image_1 = get_field('carousel_image_1');
$image_1_title = get_field('image_1_title');
if( $slide_counter == 0 ) { $class .= ' active'; }
?>
<div class="item <?php echo $class ?> <?php echo "slide_" . $slide_counter ?>">
<!-- Set the first background image using inline CSS below. -->
<a href="<?php echo get_post_permalink(); ?>" target="_blank">
<div class="fill" style="background-image:url(<?php echo $carousel_image_1['url']; ?>); background-size:cover;" alt="<?php echo $carousel_image_1['alt']; ?>"> </div>
<div class="carousel-caption">
<h2><?php echo $image_1_title; ?></h2>
<img width="80" class="book" src="<?php bloginfo('stylesheet_directory'); ?>/assets/img/book.png" alt="">
</div>
</a>
</div>
<?php $slide_counter++; ?>
<?php endwhile; wp_reset_query(); ?>
You can't use media queries inline, and you won't be able to have the dynamic flexibility of your variables in CSS, because PHP is server-side.
But, you could do this with JavaScript as long as the JS is able to get the PHP variables (my example uses jQuery):
<?php
while( $loop->have_posts() ) : $loop->the_post();
$class = '';
// Custom Posts
$carousel_image_1 = get_field('carousel_image_1');
$carousel_image_2 = get_field('carousel_image_2'); // added this, for different size image
$image_1_title = get_field('image_1_title');
if( $slide_counter == 0 ) { $class .= ' active'; }
?>
<div class="item <?php echo $class ?> <?php echo "slide_" . $slide_counter ?>">
<a href="<?php echo get_post_permalink(); ?>" target="_blank">
<div class="fill" style="background-size:cover;" alt="<?php echo $carousel_image_1['alt']; ?>"> </div>
<div class="carousel-caption">
<h2><?php echo $image_1_title; ?></h2>
<img width="80" class="book" src="<?php bloginfo('stylesheet_directory'); ?>/assets/img/book.png" alt="">
</div>
</a>
</div>
<script>
var resizeTimer;
function resizer() {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(function() {
var windowWidth = $(window).width();
if (windowWidth >= 992) { // for width 992 pixels and up
$('.fill').css({
'background-image': 'url(<?php echo $carousel_image_1["url"]; ?>)';
});
} else { // smaller sizes
$('.fill').css({
'background-image': 'url(<?php echo $carousel_image_2["url"]; ?>)';
});
}
}, 200);
}
resizer();
$(window).on('resize', resizer);
</script>
<?php $slide_counter++; ?>
<?php endwhile; wp_reset_query(); ?>
I haven't tested it but I think it should work. You can also adjust the timeout to your preference (right now it's 200ms).
Or if you wanted to not make it truly responsive, and just set the background on page load, you could just write:
<script>
if (windowWidth >= 992) { // for medium size width and up
$('.fill').css({
'background-image': 'url(<?php echo $carousel_image_1["url"]; ?>)';
});
} else { // smaller sizes
$('.fill').css({
'background-image': 'url(<?php echo $carousel_image_2["url"]; ?>)';
});
}
</script>
The following Html CSS will show image in mobile view only
Html:
foreach($data as $item):
echo "
<div class='col-lg-4'>
<div class='panel panel-primary backImg' style='background-image:url({$item['imageLink']}); background-size:100% 100%;'>
Some Text
</div>
</div>";
endforeach;
Css:
#media(min-width: 480px){
.backImg{
background-image:none !important;
}
}

putting image into background imag e

I have the following code in my Wordpress site
HTML
<div class="test">
<?php if(has_category( 'update' ) || has_category( 'uncategorized' )) { ?>
<img alt="icn" src="<?php bloginfo('template_directory'); ?>/img/logo.png">
<?php } elseif(has_category( 'event' )) { ?>
<img alt="icn" src="<?php bloginfo('template_directory'); ?>/img/logo.png">
<?php } ?>
</div>
This puts an image in the place of the category name. (category name 'update' = logo.png)
My issue is that I want their sizes responsive since they are going to be put overtop a responsive height div. My idea is to turn them into the background image of the 'test' div. I can't seem to rewrite the img tag code to put them as background images of a div instead of just putting the image itself.
EDIT ex.
<?php if(has_category( 'update' ) || has_category( 'uncategorized' )) { ?>
<div style="background-image:url('...');">
...
Have you tried something like this:
<div class="test">
<?php if(has_category( 'update' ) || has_category( 'uncategorized' )) { ?>
<div class="bg-image" style="background-image:url('<?php bloginfo('template_directory'); ?>/img/logo.png');">
<?php } elseif(has_category( 'event' )) { ?>
<div class="bg-image" style="background-image:url('<?php bloginfo('template_directory'); ?>/img/logo.png');">
<?php } else { ?>
<div class="bg-image">
<?php } ?>
... Content in div, if necessary ...
</div>
</div>
Or, to make it a little cleaner:
<?php
$testStyle = "";
if(has_category( 'update' ) || has_category( 'uncategorized' )) {
$testStyle = "style=\"background-image:url('".get_bloginfo('template_directory')."/img/logo.png')\"";
} elseif(has_category( 'event' )) {
$testStyle = "style=\"background-image:url('".get_bloginfo('template_directory')."/img/logo.png')\"";
}
?>
<div class="test">
<div class="bg-image" <?php echo $testStyle; ?>></div>
</div>
<style type="text/css">
.bg-image {
width:100%;
min-width:300px;
height:100px;
background-size:cover;
}
</style>
Realize that if there is nothing inside of the .bg-image div, you'll need to add some CSS to make it "visible": a height and width at minimum.
Also, instead of get_bloginfo('template_directory'), consider using get_template_directory_uri().

get_post_meta() not working after javascript runs

I am trying to retrieve data through my template in wordpress using get_post_meta() function. This works fine if I put the code above the div which is using a javascript, but it doesnt work if I put this function after the java script.
My php template file is as follows.
Here is the div using the javascript file ( lets call it div 1)`
<div style="width: 100%;margin: 0 auto; clear: both; ">
<div style="width: 800px">
<ul class='tabs'>
<?php $the_query = new WP_Query( 'showposts=3' ); ?>
<?php
$i=1;
while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<li><a href='#tab<?php echo $i; ?>'><?php the_title(); ?></a></li>
<?php $i++; endwhile;?>
</ul>
</div>
<div class="clear"></div>
<div style="background:#d0d8e0; position:relative; width: 96%; margin: -1% 4% 4% 2%; padding: 40px 0 20px 0; ">
<?php $the_query = new WP_Query( 'showposts=3' ); ?>
<?php
$i=1;
while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<div id='tab<?php echo $i; ?>'>
<div class="tab" >
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
</div>
</div>
<?php $i++; endwhile;?>
</div>
<div class="clear"></div>
`</div></div>
And the code I am putting after this div to retrieve values from data base is( lets call it div 2)
<div class="group group-custom" >
<?php $url = get_post_meta($post->ID,'wpcf-youtube-url', true); ?>
<div style="float: left">
<iframe type="text/html" width="480" height="315" <?php echo wp_oembed_get( 'http://www.youtube.com/watch?v=' . $url ); ?></iframe>
</div>
<div class="video-area">
<h2><?php echo "" .get_post_meta($post->ID,'wpcf-video-line',TRUE)?></h2>
<br/>
<p><?php echo "" .get_post_meta($post->ID,'wpcf-video-text',TRUE)?></p>
<a class="button" href=""></a></div></div>
If I put the div 2 after div 1 in my php template, it doesn't work but if I switch the divs, it works normally. Here is my javascript file ( which I dont see a problem)
<script language="JavaScript">
$(function() {
$('ul.tabs').each(function(){
// For each set of tabs, we want to keep track of
// which tab is active and it's associated content
var $active, $content, $links = $(this).find('a');
// If the location.hash matches one of the links, use that as the active tab.
// If no match is found, use the first link as the initial active tab.
$active = $($links.filter('[href="'+location.hash+'"]')[0] || $links[0]);
$active.addClass('active');
$content = $($active.attr('href'));
// Hide the remaining content
$links.not($active).each(function () {
$($(this).attr('href')).hide();
});
// Bind the click event handler
$(this).on('click', 'a', function(e){
// Make the old tab inactive.
$active.removeClass('active');
$content.hide();
// Update the variables with the new link and content
$active = $(this);
$content = $($(this).attr('href'));
// Make the tab active.
$active.addClass('active');
$content.show();
// Prevent the anchor's default click action
e.preventDefault();
});
});
});
The live version of this work is http://cmi.pixstreammedia.com.s177734.gridserver.com/
I found the problem. I have to use <?php wp_reset_query() ?> after the new wp_query function is over to restores the $wp_query and global post data to the original main query.
http://codex.wordpress.org/Function_Reference/wp_reset_query

Categories