Check checkbox hit button but checkbox will not stay checked - php

I am not sure how to keep my checkbox checked after I hit the Apply button. I am thinking I have to add is_set to the output but I am not sure how or where to add it in. Here is my code below any help would be great.
<div id="product-filter-options">
<form method="GET">
<?php
$scooters = $_GET['scooters'];
$product_type = $_GET['product_type'];
$bike_sub_category = $_GET['bike_sub_category'];
$model_year = $_GET['model_year'];
?>
<?php
function get_terms_chekboxes($taxonomies, $args) {
$terms = get_terms($taxonomies, $args);
foreach($terms as $term) {
$output .= '<span><label for="'.$term->slug.'"><input type="checkbox" id="'.$term->slug.'" name="'.$taxonomies.'" value="'.$term->slug.'" >' .$term->name.'</label></span>';
}
return $output;
}
echo '<div class="check-contain">';
echo '<h4>Bicycle Brand</h4>';
echo get_terms_chekboxes('scooters', $args = array('hide_empty'=>true));
echo '<span class="slide-show-more">See More</span>';
echo '</div>';
echo '<div class="check-contain">';
echo '<h4>Bicycle Type</h4>';
echo get_terms_chekboxes('product_type', $args = array('hide_empty'=>true));
echo '<span class="slide-show-more">See More</span>';
echo '</div>';
echo '<div class="check-contain">';
echo '<h4>Bicycle Sub-Type</h4>';
echo get_terms_chekboxes('bike_sub_category', $args = array('hide_empty'=>true));
echo '<span class="slide-show-more">See More</span>';
echo '</div>';
echo '<div class="check-contain">';
echo '<h4>Model Year</h4>';
echo get_terms_chekboxes('model_year', $args = array('hide_empty'=>true));
echo '<span class="slide-show-more">See More</span>';
echo '</div>';
?>
<button type="submit">Apply</button>
Thanks in advance.

Related

How to rewrite an echo function to a return value for a Wordpress theme

I am new to PHP and I am glad that I have written the following function (which works as it should) on my own so far:
function get_latest_posts() {
echo '<div class="latest-posts">';
echo '<div class="brick_list">';
$args = array(
post_type => 'post',
posts_per_page => 3
);
$latestposts_query = new WP_Query($args);
if ( $latestposts_query->have_posts() ) : while ( $latestposts_query->have_posts() ) : $latestposts_query->the_post();
echo ' <div ';
post_class("brick_item" . $termString );
echo ' onclick="location.href=\'';
the_permalink();
echo ' \'"> ';
echo ' <div class="brick_item-image"> ';
if ( has_category( 'sample' ) ) {
echo ' <img src="/wp-content/uploads/placeholder_1.png" /> ';
} elseif ( has_post_thumbnail() ) {
the_post_thumbnail();
} else {
echo ' <img src="/wp-content/uploads/placeholder_2.png" /> ';
}
echo ' </div> ';
echo ' <div class="brick_item-header"> ';
echo ' <h4><a href=" ';
the_permalink();
echo ' "> ';
the_title();
echo ' </a></h4> ';
echo ' </div> ';
echo ' </div> ';
endwhile; else :
get_template_part('template_parts/content','error');
endif;
wp_reset_postdata();
echo '</div>';
echo '</div>';
}
add_shortcode( 'get_latest_posts', 'get_latest_posts' );
But now I definitly need your help...
As you can see I want to use this function in a shortcode. Currently it displays first on my page, and Google says, this is because I am echoing the content and I would need to give a return value.
How do I do this?? Is it just replacing the word "echo" with the word "return" ? I have no idea...
I have condensed a few statements together for readability but here's what that would look like:
function get_latest_posts() {
$return = '<div class="latest-posts"><div class="brick_list">';
$args = array(
post_type => 'post',
posts_per_page => 3
);
$latestposts_query = new WP_Query($args);
if ( $latestposts_query->have_posts() ) : while ( $latestposts_query->have_posts() ) : $latestposts_query->the_post();
$return .= ' <div ' . post_class("brick_item" . $termString ) . ' onclick="location.href=\'' . get_permalink() . '\'"> ';
$return .= '<div class="brick_item-image">';
if ( has_category( 'sample' ) ) {
$return .= '<img src="/wp-content/uploads/placeholder_1.png" />';
} elseif ( has_post_thumbnail() ) {
$return .= get_the_post_thumbnail();
} else {
$return .= '<img src="/wp-content/uploads/placeholder_2.png" />';
}
$return .= '</div> <div class="brick_item-header"> <h4>' . the_title('','',false) . '</h4></div></div>';
endwhile; else :
get_template_part('template_parts/content','error');
endif;
wp_reset_postdata();
$return .= '</div></div>';
return $return;
}
add_shortcode( 'get_latest_posts', 'get_latest_posts' );

Changing active class using echo in php comand

I have a problem with next code:
<?php
include("db.php");
$sql = mysql_query("SELECT COUNT(id_produs) FROM My_Products WHERE `id_produs_categorie`='$id_categorie' ");
$total_produse = mysql_result($sql, 0);
$total_pagini = ceil($total_produse / $produse_pe_pagina);
if(isset($_GET['ID'])!="")
$id_categorie=$_GET['ID'];
echo '<div class="row">';
echo '<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">';
echo '<div class="bs-example margin-top20">';
echo '<ul class="pagination">';
echo '<li class="disabled"></li>';
echo '<li ( basename($_SERVER["REQUEST_URI"]) == "categorie.php?ID=$id_categorie&pagina=1" ? class="active" : "" )>1</li>';
for ($i=2; $i<=$total_pagini; $i++) {
echo '<li ( basename($_SERVER["REQUEST_URI"]) == "categorie.php?ID=$id_categorie&pagina=$i" ? class="active" : "" )>'.$i.'</li>';
};
echo '<li class="disabled"></li>';
echo '</ul>';
echo '</div>';
echo '</div>';
echo '</div>';
?>
I'm trying to change the classes based on the page user is on.
The code is good if i echo the address its ok with variables but the problem is that even if i'm on page 1 it adds also the class active to page 2 or 3.
What i'm doing wrong here?
Thank you for your help!
Try:
<?php
include("db.php");
$sql = mysql_query("SELECT COUNT(id_produs) FROM My_Products WHERE `id_produs_categorie`='$id_categorie' ");
$total_produse = mysql_result($sql, 0);
$total_pagini = ceil($total_produse / $produse_pe_pagina);
if(isset($_GET['ID'])!="")
$id_categorie=$_GET['ID'];
if(isset($_GET['pagina'])!="")
$pagina = $_GET['pagina'];
echo '<div class="row">';
echo '<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">';
echo '<div class="bs-example margin-top20">';
echo '<ul class="pagination">';
echo '<li class="disabled"></li>';
for ($i=1; $i<=$total_pagini; $i++) {
$class = $pagina == $i ? 'class="active"' : "";
echo '<li ' . $class .'>'.$i.'</li>';
};
echo '<li class="disabled"></li>';
echo '</ul>';
echo '</div>';
echo '</div>';
echo '</div>';
?>

Endforeach loop different class for first item

I'm not that good at php. What i have is a list of items looped with endforeach. What I want is that the first loop will have a class of col-lg-12 and from the second one that class will become col-lg-6. How can I achive that?
Here's the code:
<?php $firstLoop = true;
foreach( $network_value as $key => $value ){
if( $firstLoop ){
echo '<div class="col-lg-12">';
}
echo '<div class="col-lg-6">';
$firstLoop = false;
} ?>
^This is the code that I've tried, but it's not working how i wanted.
<?php if ($img): ?>
<img src="<?php echo $thumb->src ?>" width="<?php echo $thumb->width ?>" height="<?php echo $thumb->height ?>" alt="" />
<?php endif; ?>
<h4>
<div class="circle"><?php echo $datePublic = date('d M', strtotime($page->getCollectionDatePublic())); ?></div>
<br>
<a class="blogTitle" href="<?php echo $url ?>" target="<?php echo $target ?>"><?php echo $title ?></a></h4>
<h6>Posted by <?php echo $author; ?></h6>
<br>
<p><?php echo $description ?></p>
</div>
<?php endforeach; ?>
The most simple way to do it is to add a counter and check if it is the first value in the counter.
<?php
$counter = 0;
foreach( $network_value as $key => $value )
{
if($counter == 0){
echo '<div class="col-lg-12">';
} else {
echo '<div class="col-lg-6">';
}
$counter++;
}
?>
Also I want to add that there are two ways of using foreach and if-statements, but you are trying to mix them, which is wrong.
The first method is using brackets "{" and "}":
foreach($users as $user) {
// do things for each user
echo $user->name; // Example of writing out users name
}
And if-statement:
if(true) {
// do something
}
The second method is using "foreach(statement):" and "endforeach;"
foreach($users as $user):
// do things for each user
echo $user->name; // Example of writing out users name
endforeach;
And if-statement:
if(true):
// do something
endif;
Edited:
In your case you can use:
<?php
foreach ($pages as $key=>$page):
if($key==0){
echo '<div class="col-lg-12">';
} if($key==1){
echo '<div class="col-lg-6">';
}
endforeach; ?>
Or you can use:
<?php
foreach ($pages as $key=>$page):
if($key==0){
echo '<div class="col-lg-12">';
} else {
echo '<div class="col-lg-6">';
}
endforeach; ?>
OLD:
foreach($array as $key=>$row){
if($key==0){
echo '<div class="col-lg-12"></div>';
}
if($key==5){
echo '<div class="col-lg-6"></div>';
}
// OR try use %
if($key%2 == 0){
echo '<div class="col-lg-12"></div>';
} else {
echo '<div class="col-lg-12"></div>';
}

Wordpress Ordering (boolean?)

Hi I would like to order custom fields in wordpress. I've got it displaying the single project. But can't figure out how I can get it to display in order. I've created a field called project_order which works correctly on another page.
But i'm not used to this sort of php with order by. This is a project designed by someone else so i'm trying to pick up and learn how it was built.
<?php
//associate sector to product projects page
$sector['construction'] = 131;
$sector['timber-frame'] = 235;
$sector['industrial'] = 253;
$sector['agriculture'] = 263;
//link to view all projects for product
echo '<p>View all projects</p>';
?>
</div>
<div class="span5">
<div id="latestproject">
<h2>Latest Project</h2>
<?php
//get latest projects
$rows = get_field('projects', $sector [$post->post_name] , '&order=ASC');
//display latest project
if ($rows) {
echo '<div class="row">';
if ($rows[0]['project_pageturner']) {
echo '<a href="'.$rows[0]['project_pageturner'].'" target="_blank">';
} else if ($rows[0]['project_pdf']) {
echo '<a href="'.wp_get_attachment_url($rows[0]['project_pdf']).'" target="_blank">';
} else {
echo "<a href=\"javascript:alert('No PDF uploaded for this item.')\">";
}
echo '<div class="span2">';
if ($rows[0]['project_thumbnail']) {
echo wp_get_attachment_image($rows[0]['project_thumbnail'], 'full');
} else {
echo '<img src="'.get_bloginfo('template_directory').'/images/defaultproject.jpg" alt="">';
}
echo '</div>';
echo '<div class="span3">';
echo '<p><strong>'.$rows[0]['project_title'].'</strong></p>';
echo '<p>View project »</p>';
echo '</div>';
echo '</a>';
echo '</div><!--row-->';
} else {
echo '<p>No projects to display currently.</p>';
}
?>
I'm guessing it will have something to do with.
$rows = get_field('projects', $sector [$post->post_name]);
Try below code, may be this resolve your problem.
<?php
wp_reset_query();
//associate sector to product projects page
$sector['construction'] = 131;
$sector['timber-frame'] = 235;
$sector['industrial'] = 253;
$sector['agriculture'] = 263;
//link to view all projects for product
echo '<p>View all projects</p>';
?>
</div>
<div class="span5">
<div id="latestproject">
<h2>Latest Project</h2>
<?php
//get latest projects
$rows = get_field('projects', $sector [$post->post_name] , '&order=ASC');
//display latest project
if ($rows) {
echo '<div class="row">';
if ($rows[0]['project_pageturner']) {
echo '<a href="'.$rows[0]['project_pageturner'].'" target="_blank">';
} else if ($rows[0]['project_pdf']) {
echo '<a href="'.wp_get_attachment_url($rows[0]['project_pdf']).'" target="_blank">';
} else {
echo "<a href=\"javascript:alert('No PDF uploaded for this item.')\">";
}
echo '<div class="span2">';
if ($rows[0]['project_thumbnail']) {
echo wp_get_attachment_image($rows[0]['project_thumbnail'], 'full');
} else {
echo '<img src="'.get_bloginfo('template_directory').'/images/defaultproject.jpg" alt="">';
}
echo '</div>';
echo '<div class="span3">';
echo '<p><strong>'.$rows[0]['project_title'].'</strong></p>';
echo '<p>View project »</p>';
echo '</div>';
echo '</a>';
echo '</div><!--row-->';
} else {
echo '<p>No projects to display currently.</p>';
}
wp_reset_query();
?>
Thanks.

Display multiple image on separated div with repeater field

I´m trying to display a list of div with images using the repeater field of ACF.
this is the code i´m using:
<?php $query = new WP_Query( 'post_type=artworks_post&posts_per_page=-1&order=DESC' ); ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<?php
$slides = get_field('project_thumbnails'); // Grabs the array
// Check if there is any data in the array before looping
if($slides) {
echo '<div id="project_slider" class="item">';
foreach($slides as $s) {
echo '<a href="#">';
echo '<img src="'.$s['project_thumb'].'" alt="" />';
echo '</a>';
}
echo '<div class="art_title">';
echo '<p>SWEET LIFE2</p>';
echo '</div>';
echo '<div class="mask">';
echo '</div>';
}
?>
<?php endwhile; // end of the loop. ?>
The problem with this is that it displays all images in the same div, instead of display every image of the repeater field in a new div.
what i´m doing wrong?
Im not a php expert but,i think is just a matter of missing closing tags for the first div, also im gonna put another div within the loop, it looks like this:
<?php $slides = get_field('project_thumbnails');
// Grabs the array
// Check if there is any data in the array before looping
if($slides) {
//we need to close this div
echo '<div id="project_slider" class="item">';
foreach($slides as $s) {
echo '<div class="aimagediv" >'; //adding the start tag of the div
echo '<a href="#">';
echo '<img src="'.$s['project_thumb'].'" alt="" />';
echo '</a>';
echo '</div>'; //CLOSING THE DIV JUST ADDED
}
echo '<div class="art_title">';
echo '<p>SWEET LIFE2</p>';
echo '</div>';
echo '<div class="mask">';
echo '</div>';
echo '</div>'; //closing the first div,not sure if you want this, its optional
}
?> <?php endwhile; // end of the loop. ?>
Another alternative would be this (just figuring out what's the resulting layout you're expecting):
<?php $slides = get_field('project_thumbnails');
// Grabs the array
// Check if there is any data in the array before looping
if($slides) {
//we need to close this div
foreach($slides as $s) {
echo '<div id="project_slider" class="item">';
echo '<a href="#">';
echo '<img src="'.$s['project_thumb'].'" alt="" />';
echo '</a>';
echo '</div>'; //CLOSING THE DIV JUST ADDED
}
echo '<div class="art_title">';
echo '<p>SWEET LIFE2</p>';
echo '</div>';
echo '<div class="mask">';
echo '</div>';
}
?> <?php endwhile; // end of the loop. ?>
Hopefully this would be enough.

Categories