Show elements with row "lugar" value 1 - php

I have a table with a row called lugar that have the value 1 or 2.
When I do foreach, i want to filter the elements that have the value 1 in the row lugar and the elements that have the value 2.
$descuento->lugar is the variable for the row lugar.
Actually, my code is:
<ul id="slider">
<?php foreach ( $results['descuentos'] as $descuento ) {
$titulo = htmlspecialchars( $descuento->title );
$title_url = limpiarCaracteresEspeciales($titulo);
?>
<li>
<?php if ( $imagePath = $descuento->getImagePath( IMG_TYPE_FULLSIZE ) ) { ?>
<img src="<?php echo $imagePath?>" alt="<?php echo htmlspecialchars( $descuento->title )?>" />
<?php } else {?> <img src="http://lorempixel.com/400/300/sports/" alt="<?php echo htmlspecialchars( $descuento->title )?>" /> <?php } ?>
<div>
<strong><span><?php echo htmlspecialchars( $descuento->title )?></span></strong>
<p><?php echo $descuento->content ?></p>
</div>
</li>
<?php
}
?>
</ul>

Then you could try this:
foreach ( $results['descuentos'] as $descuento )
{
if( $descuento->lugar == 1)
{
$titulo = htmlspecialchars( $descuento->title );
$title_url = limpiarCaracteresEspeciales($titulo);
echo '<li>';
if ( $imagePath = $descuento->getImagePath( IMG_TYPE_FULLSIZE ) )
{
echo '<img src="'. $imagePath. '" alt="'. $titulo. '" />';
}
else
{
echo '<img src="http://lorempixel.com/400/300/sports/" alt="'. $titulo. '" />';
}
echo '<div>'.
'<strong><span>'. $titulo. '</span></strong>'.
'<p>'. $descuento->content. '</p>'.
'</div>'.
'</li>';
}
}

Related

Change the profile cover image dynamically

does someone know how to change an image header dynamically depends by specific categories,
ex: image1 for red category, image2 for blue category, image3 for green category?
Im using ultimate member plugin but don't well php coding
I have found the original code
function um_profile_header_cover_area( $args ) {
global $ultimatemember;
if ( $args['cover_enabled'] == 1 ) {
$default_cover = um_get_option('default_cover');
$overlay = '<span class="um-cover-overlay">
<span class="um-cover-overlay-s">
<ins>
<i class="um-faicon-picture-o"></i>
<span class="um-cover-overlay-t">'.__('Change your cover photo', 'ultimatemember').'</span>
</ins>
</span>
</span>';
?>
<div class="um-cover <?php if ( um_profile('cover_photo') || ( $default_cover && $default_cover['url'] ) ) echo 'has-cover'; ?>" data-user_id="<?php echo um_profile_id(); ?>" data-ratio="<?php echo $args['cover_ratio']; ?>">
<?php do_action('um_cover_area_content', um_profile_id() ); ?>
<?php
if ( $ultimatemember->fields->editing ) {
$items = array(
''.__('Change cover photo', 'ultimatemember').'',
''.__('Remove', 'ultimatemember').'',
''.__('Cancel', 'ultimatemember').'',
);
echo $ultimatemember->menu->new_ui( 'bc', 'div.um-cover', 'click', $items );
}
?>
<?php $ultimatemember->fields->add_hidden_field( 'cover_photo' ); ?>
<?php echo $overlay; ?>
<div class="um-cover-e">
<?php if ( um_profile('cover_photo') ) { ?>
<?php
if( $ultimatemember->mobile->isMobile() ) {
if ( $ultimatemember->mobile->isTablet() ) {
echo um_user('cover_photo', 1000);
echo um_user('cover_photo', 300);
}
} else {
echo um_user('cover_photo', 1000);
}
?>
<?php } elseif ( $default_cover && $default_cover['url'] ) {
$default_cover = $default_cover['url'];
echo '<img src="'. $default_cover . '" alt="" />';
} else {
if ( !isset( $ultimatemember->user->cannot_edit ) ) { ?>
<span class="um-cover-add-i"><i class="um-icon-plus um-tip-n" title="<?php _e('Upload a cover photo', 'ultimatemember'); ?>"></i></span>
<?php }
} ?>
</div></div>
<?php
}}
Thanks in advance

WP Loop Geo + jQuery .hide() not working correctly

I have a WP loop where each post has a collection of 4 country based images (using ACF).
I only would like to output 1 image per country, however it is displaying all of 4 images per post.
<?php
$args = array( 'post_type' => 'quick_links', 'posts_per_page' => 3 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$image_au = get_field("au_image");
$image_nz = get_field("nz_image");
$image_us = get_field("us_image");
$image_gl = get_field("global_image"); //default image
?>
<script type="text/javascript">
var image_au = <?php echo json_encode($image_au['url']); ?>;
var image_nz = <?php echo json_encode($image_nz['url']); ?>;
var image_us = <?php echo json_encode($image_us['url']); ?>;
var image_gl = <?php echo json_encode($image_gl['url']); ?>;
jQuery.get("http://ipinfo.io", function (response) {
if (response.country === "AU"){
jQuery("#resultQLAU").show();
jQuery("#resultQLNZ").hide();
jQuery("#resultQLUS").hide();
jQuery("#resultQLGlobal").hide();
} else if(response.country === "NZ"){
jQuery("#resultQLNZ").show();
jQuery("#resultQLAU").hide();
jQuery("#resultQLUS").hide();
jQuery("#resultQLGlobal").hide();
} else if(response.country === "US"){
jQuery("#resultQLUS").show();
jQuery("#resultQLNZ").hide();
jQuery("#resultQLAU").hide();
jQuery("#resultQLGlobal").hide();
} else {
jQuery("#resultQLGlobal").show();
jQuery("#resultQLNZ").hide();
jQuery("#resultQLUS").hide();
jQuery("#resultQLAU").hide();
}
if(image_au === "" && image_nz === "" && image_us === "" && image_gl !== ""){
jQuery("#resultQLGlobal").show();
}
}, "jsonp");
</script>
<?php
echo '<div class="col-lg-4 col-sm-6" style="padding:2px">';
echo '<a href="' . get_field('page_url') . '" class="portfolio-box">';
?>
<div id="resultQLAU">
<img class="img-responsive" src="<?php echo $image_au['url']; ?>" alt="<?php echo $image_au['alt']; ?>" />
</div>
<div id="resultQLNZ">
<img class="img-responsive" src="<?php echo $image_nz['url']; ?>" alt="<?php echo $image_nz['alt']; ?>" />
</div>
<div id="resultQLUS">
<img class="img-responsive" src="<?php echo $image_us['url']; ?>" alt="<?php echo $image_us['alt']; ?>" />
</div>
<div id="resultQLGlobal">
<img class="img-responsive" src="<?php echo $image_gl['url']; ?>" alt="<?php echo $image_gl['alt']; ?>" />
</div>
<?php
echo '<div class="portfolio-box-caption">';
echo '<div class="portfolio-box-caption-content">';
echo '<div class="project-category text-faded">' . get_the_title() . '</div>';
echo '<div class="project-name">' . get_the_content() . '</div>';
echo '</div>';
echo '</div>';
echo '</a>';
echo '<h6 class="news-title text-center">' . get_the_title() . '<span style=""> <i class="fa fa-angle-double-right"></i></span></h6>';
echo '</div>';
endwhile;
?>
I originally had the code e.g <div id="resultQLAU" style="display:none"> and just had jQuery("#resultQLAU").show(); in script which outputed only the first
GEO image of the first post (so GEO was working correct for that 1 post)
Not sure what problem is?
Your help would be much appreciated. Thanks
You are using ID inside your loop so all the block will have the same ids which isn't good as id need to be unique. You may change this by adding a suffix/prefix depending the iteration and use classes instead.
1) add a new var the increment inside your loop like this :
$i = 0
while ( $loop->have_posts() ) : $loop->the_post();
$i++;
2) for each id append the content of $i, for example :
jQuery(".resultQLAU_<?php echo $i; ?>").show();
do this everywhere you have the id.

Php code between Tab Shortcode

I want to insert php code in between tabby tabs shortcodes.
I am using a plugin tabby tab for tab view and have added this code in my theme template:
<?php echo do_shortcode('[tabby title="Gallary Name"]
name content
[tabby title="Images"]
[tabbyending]'); ?>
I want to use a custom fields gallery under images tab using code like this:
<?php echo do_shortcode('[tabby title="Gallary Name"]
name content
[tabby title="Images"]
<?php
$i = 0;
$images = get_field('vil_pics');
if( $images ): ?>
<div>
<ul>
<?php foreach( $images as $image ): ?>
<li<?php if ( $i % 3 == 0 ) echo ' class="break"' ?>>
<a href="<?php echo $image['url']; ?>">
<img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" />
</a><p>.</p>
</li>
<?php endforeach; ?>
</ul></div>
<?php endif; ?>
[tabbyending]'); ?>
This code is not working, it's showing a blank page. How can I fix this?
Tabby uses a global variable to track what's going on, so I think either one of these will work. The first one is a little more straightforward, but the second one will definitely work.
Option 1: output everything in order:
echo do_shortcode( '[tabby title="Gallery Name"] name content' );
echo do_shortcode( '[tabby title="Images"]' );
// your php code as-is
$i = 0;
$images = get_field('vil_pics');
if( $images ): ?>
<div>
<ul>
<?php foreach( $images as $image ):
$i++ ?>
<li<?php if ( $i % 3 == 0 ) echo ' class="break"' ?>>
<a href="<?php echo $image['url']; ?>">
<img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" />
</a><p>.</p>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php endif;
echo do_shortcode( '[tabbyending]' );
or Option 2: save everything to a variable and output it all at once:
$output = '';
$output .= '[tabby title="Gallery Name"] name content';
$output .= '[tabby title="Images"]';
$i = 0;
$images = get_field('vil_pics');
if ( $images ) {
$output .= '<div><ul>';
foreach( $images as $image ) {
$i++;
$li_class = ( $i % 3 == 0 ) ? ' class="break"' : '';
$output .= '<li' . $li_class . '>';
$output .= '<a href="' . $image['url'] . '">';
$output .= '<img src="' . $image['sizes']['thumbnail'] . '" alt="' . $image['alt'] . '" />';
$output .= '</a><p>.</p></li>';
}
$output .= '</div></ul>';
}
$output .= '[tabbyending]';
echo do_shortcode( $output );
Note that I didn't see anything increasing $i so I added that. Everything else is as-is.

How to loop a php code?

I wanted to do the DRY approach in my code but I'm having a hard time figuring it out. And also, I want to hide the entire code if there's no image_1. Hope you could help me do the trick.
Here's the code
<div class="col-md-4">
<?php
$image = get_field('image_1');
if(get_field('image_1'))
{
echo '<a href="' . get_field('image_link_1') . '">';?>
<img src="<?php echo $image['url']; ?>" title="<?php echo $image['title']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php echo '</a>';
} else {
echo '<img src="http://localhost/image.png">';
} ?>
</div>
<div class="col-md-4">
<?php
$image = get_field('image_2');
if(get_field('image_2'))
{
echo '<a href="' . get_field('image_link_2') . '">';?>
<img src="<?php echo $image['url']; ?>" title="<?php echo $image['title']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php echo '</a>';
} else {
echo '<img src="http://localhost/image.png">';
} ?>
</div>
<div class="col-md-4">
<?php
$image = get_field('image_3');
if(get_field('image_3'))
{
echo '<a href="' . get_field('image_link_3') . '">';?>
<img src="<?php echo $image['url']; ?>" title="<?php echo $image['title']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php echo '</a>';
} else {
echo '<img src="http://localhost/image.png">';
} ?>
</div>
You should put differences to arrays and then wrap everything into for loop:
<?php
$images = array('image_1', 'image_2', 'image_3');
$links = array('image_link_1', 'image_link_2', 'image_link_3');
for($i=0; $i<3; $i++){
?>
<div class="col-md-4">
<?php
$image = get_field($images[$i]);
if(get_field($images[$i])){
echo '<a href="' . get_field($links[$i]) . '">';
?>
<img src="<?php echo $image['url']; ?>" title="<?php echo $image['title']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php echo '</a>';
} else {
echo '<img src="http://localhost/image.png">';
}
?>
</div>
<?php
}
?>
Just a hint...:
<?php
for ($i = 0; $i < 3; $i++) {
echo "<div class='col-md-4'>" . "\n";
$image = get_field("image_" . ($i + 1));
...
echo "</div>" . "\n";
}
?>
Something along these lines should get you started if I'm understanding you correctly:
<?php for ($q = 1; $q <= 3; $q++) {
$image_loop = 'image_' . $q;
echo '<div class="col-md-4">';
if ($image = get_field($image_loop)) {
echo '<a href="' . get_field('image_link_' . $q) . '">';
?>
<img src="<?php echo $image['url']; ?>" title="<?php echo $image['title']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php echo '</a>';
} else {
echo '<img src="http://localhost/image.png">';
} ?>
</div>
<?php } // end loop ?>
The other suggestions here will work as well but here's what I would do:
First arrange the images in an associative array with keys being the image name and values being the image link and then iterate via a foreach loop.
I generally try to not echo HTML unless strictly necessary.
<?php
$array = [
"image_1" => "image_link_1",
"image_2" => "image_link_2",
"image_3" => "image_link_3",
"image_4" => "image_link_4"
];
foreach ($array as $name => $link):
$image = get_field($name);
if ($image): ?>
<div class="col-md-4">
<a href="<?=get_field($link)?>">
<img src="<?= $image['url']; ?>" title="<?= $image['title']; ?>" alt="<?= $image['alt']; ?>" />
</a>
<?php else: ?>
<img src="http://localhost/image.png">
<?php endif; ?>
</div>
<?php endforeach; ?>

Trouble creating checkbox array with 2 items being checked by default in PHP

Im working with a Wordpress theme.
The code I am having trouble with is relevant to a filter divided into "action" and "categories".
I would like to have 2 checkboxes checked by default (1 action & 1 category) and the rest blank.
I'm at a complete loss, any help would be sincerely appreciated.
here is the code:
<?php
$icons = array();
$taxonomy = 'property_action_category';
$tax_terms = get_terms($taxonomy);
$taxonomy_cat = 'property_category';
$categories = get_terms($taxonomy_cat);
// add only actions
foreach ($tax_terms as $tax_term) {
$icon_name = 'wp_estate_icon'.$tax_term->slug;
$icons[$tax_term->slug] = esc_html( get_option($icon_name) );
}
// add only categories
foreach ($categories as $categ) {
$icon_name = 'wp_estate_icon'.$categ->slug;
$icons[$categ->slug] = esc_html( get_option($icon_name) );
}
?>
<div class="gmap_wrapper <?php
if (!is_front_page()) {
print 'gmap_not_home" style="height:295px;"';
}else{
print'"';
}
?>>
<div class="gmap-next <?php if (is_front_page()) print 'is-front'; ?>" id="gmap-next" > </div>
<div class="gmap-prev <?php if (is_front_page()) print 'is-front'; ?>" id="gmap-prev" > </div>
<?php
$geo_status = esc_html ( get_option('wp_estate_geolocation','') );
$custom_image = esc_html( esc_html(get_post_meta($post->ID, 'page_custom_image', true)) );
$rev_slider = esc_html( esc_html(get_post_meta($post->ID, 'rev_slider', true)) );
if($geo_status=='yes' && $custom_image=='' && $rev_slider==''){
print' <div id="mobile-geolocation-button"></div>';
}
?>
<div id="googleMap" <?php
$home_small_map_status= esc_html ( get_option('wp_estate_home_small_map','') );
if (!is_front_page() || ( is_front_page() && $home_small_map_status=='yes' ) ) {
print 'style="height:295px;"';
}
?>
</div>
<div class="tooltip"> <?php _e('click to enable zoom','wpestate');?></div>
<div id="gmap-loading"><?php _e('Loading Maps','wpestate');?>
<span class="gmap-animation">
<img src="<?php print get_template_directory_uri();
?>
</span>
</div>
<?php
////////////////////////// enable /disable map filters
$show_filter_map_status = esc_html ( get_option('wp_estate_show_filter_map','') );
$home_small_map_status = esc_html ( get_option('wp_estate_home_small_map','') );
if($show_filter_map_status!='no'){
?>
<div class="gmap-menu-wrapper" >
<div class="gmap-menu" id="gmap-menu" <?php if (!is_front_page() || (is_front_page() && $home_small_map_status=='yes') ) print 'style="display:none;"'; ?> >
<div id="closefilters"></div>
<div class="action_filter" >
<?php
foreach ($tax_terms as $tax_term) {
print '<div class="checker"><input type="checkbox" checked="checked" name="filter_action[]" id="'.$tax_term->slug.'" class="'.$tax_term- >slug.'" value="'.$tax_term->name.'"/><label for="'.$tax_term->slug.'"><span></span>';
if( $icons[$tax_term->slug]!='' ){
print '<img src="'.$icons[$tax_term->slug].'" alt="'.$tax_term->name.'">' . $tax_term->name . '</label></div>';
}else{
print '<img src="'.get_template_directory_uri().'/css/css- images/'.$tax_term->slug.'icon.png" alt="'.$tax_term->name.'">' . $tax_term->name . '</label></div>';
}
}
?>
</div>
<div class="type-filters">
<?php
foreach ($categories as $categ) {
print '<div class="checker"><input type="checkbox" checked="checked" name="filter_type[]" id="'.$categ->slug.'" class="' . $categ->slug . '" value="' . $categ->name . '"/><label for="' . $categ->slug. '"><span></span>';
if( $icons[$categ->slug]!='' ){
print' <img src="'.$icons[$categ->slug].'" alt="'.$categ->slug.'">' . $categ->name . '</label></div>';
}else{
print' <img src="'.get_template_directory_uri().'/css/css-images/'.$categ->slug.'icon.png" alt="'.$categ->name.'">' . $categ->name . '</label></div>';
}
}
?>
</div>
</div>
</div>
<?php
}
?>
</div> `enter code here`
Thanks
Kyle

Categories