Wordpress Ordering (boolean?) - php

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.

Related

Check checkbox hit button but checkbox will not stay checked

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.

Insert Clear Fix After Certain Amount of DIVs in PHP

I'm struggling with a problem and I thought someone could help me. I'm trying to insert a clear div after a certain amount div containers. Now in this example I've managed to get it to work that after the first three a clear div is placed, but I want it to be inserted after each set of 3 divs depending on how many featured articles I've setup (3, 6, 9 etc).
$featured_articles = get_field('featured_articles', false, false );
$id = $featured_articles[0];
//var_dump();
$numberOfArticles = count($featured_articles);
//var_dump($featured_articles);
echo "<div class='container'>";
echo "<div class='row-fluid'>";
for ($i=0; $i < $numberOfArticles; $i++) {
if ($featured_articles) {
$id = $featured_articles[$i];
//var_dump($featured_articles);
if ( has_post_thumbnail($id) ) {
echo "<div class='span4'>";
echo "<article class='post__holder recipes'>";
echo "<a href='".get_permalink($id)."' class='image'>";
echo get_the_post_thumbnail( $id, 'medium-thumb' );
$image_large = wp_get_attachment_image_src( get_post_thumbnail_id($id), 'large');
echo "</a>";
}
echo "<a href='".get_permalink($id)."'><h3>".get_the_title($id)."</h3></a>";
echo "<div class='starRating'>";
echo "<img src='".home_url()."/wp-content/themes/BUZZBLOG-theme/images/StarRating.svg' alt='Testergebnis'>";
echo "<div class='bar' style='width:".get_field('rating', $id)."%'></div>";
echo "</div>";/** end starrating **/
echo "<a href='".get_permalink($id)."'><div class='readmore-button'>Zum Testbericht...</div></a>";
echo "</div>";
echo "</article>";
if ($i >= 2) {
if($i == 2) {
echo "<div class='clearfix'>";
}
if ($i == $numberOfArticles) {
echo "</div>";
}
}
}
}
echo "</div>";
echo "</div>";
Try this It will check for multiples of 3 and place a clearfix div
Also dont forget to start the $i from 1 upto $i <= $numberOfArticles
$featured_articles = get_field('featured_articles', false, false );
$id = $featured_articles[0];
//var_dump();
$numberOfArticles = count($featured_articles);
//var_dump($featured_articles);
echo "<div class='container'>";
echo "<div class='row-fluid'>";
for ($i=1; $i <= $numberOfArticles; $i++) {
if ($featured_articles) {
$id = $featured_articles[$i-1];
//var_dump($featured_articles);
if ( has_post_thumbnail($id) ) {
echo "<div class='span4'>";
echo "<article class='post__holder recipes'>";
echo "<a href='".get_permalink($id)."' class='image'>";
echo get_the_post_thumbnail( $id, 'medium-thumb' );
$image_large = wp_get_attachment_image_src( get_post_thumbnail_id($id), 'large');
echo "</a>";
}
echo "<a href='".get_permalink($id)."'><h3>".get_the_title($id)."</h3></a>";
echo "<div class='starRating'>";
echo "<img src='".home_url()."/wp-content/themes/BUZZBLOG-theme/images/StarRating.svg' alt='Testergebnis'>";
echo "<div class='bar' style='width:".get_field('rating', $id)."%'></div>";
echo "</div>";/** end starrating **/
echo "<a href='".get_permalink($id)."'><div class='readmore-button'>Zum Testbericht...</div></a>";
echo "</div>";
echo "</article>";
if($i%3==0) {
echo "<div class='clearfix'></div>";
}
}
}
echo "</div>";
echo "</div>";
You can do like this:-
for ($i=0; $i < $numberOfArticles; $i++) {
// your code
if($i%3==0) echo '<div class="clearfix"></div>';
}
You can use array_chunk() function to divide your array in group of 3 members each and you can iterate over each array.
PHP is a server side scripting language that is embedded in HTML.
So use this feature in your code instead of writing echo "html element" every where in code.
I think you have miss match of div element. Because <article> must be close before the <div class='span4'> but in your code it is not there.
$featured_articles = get_field('featured_articles', false, false );
$dividedIn3GroupsEach = array_chunk($featured_articles, 3);
foreach ($dividedIn3GroupsEach as $array) {
?>
<div class='container'>
<div class='row-fluid'>
<?php
foreach ($array as $key => $value) {
$id = $featured_articles[$key];
if (has_post_thumbnail($id)) {
?>
<div class='span4'>
<article class='post__holder recipes'>
<a href='<?php echo get_permalink($id); ?>' class='image'>
<?php
echo get_the_post_thumbnail($id, 'medium-thumb');
$image_large = wp_get_attachment_image_src(get_post_thumbnail_id($id), 'large');
?>
</a>
<?php } ?>
<a href='<?php echo get_permalink($id); ?>'><h3><?php echo get_the_title($id); ?></h3></a>
<div class='starRating'>
<img src='<?php echo home_url() ?>"/wp-content/themes/BUZZBLOG-theme/images/StarRating.svg' alt='Testergebnis'>
<div class='bar' style='width:<?php echo get_field('rating', $id); ?>%'></div>
</div>
<a href='<?php echo get_permalink($id); ?>'><div class='readmore-button'>Zum Testbericht...</div></a>
</article>
</div>
<?php }
?>
</div> <!-- row-fluid end -->
</div> <!-- container end -->
<div class='clearfix'></div>
<?php } ?>
I hope this may help you.

My Carousel only displays the first thing from the database but wont display the next one and onward

My Carousel only displays the first thing from the database but wont display the next one and onward
this is my code and the query
it wont display the 2nd image
but it displays the 1st image
can anybody help thanks
<?php
require_once("db2.php");
$GardenDBB = new GardenDBB();
$result = ($GardenDBB->LoadAllGallery());
$num_rows = mysql_num_rows($result);
$counter = 1;
while($db_field = mysql_fetch_array($result)){
?>
<div class="item<?php if ($counter <= 1){echo " active"; ?>" style="background-image: url(<?php echo './upload2/'.$db_field['image_name']?>")>;
<?php
echo '<div class="container">';
echo '<div class="row slide-margin">';
echo '<div class="col-sm-6">';
echo '<div class="carousel-content">';
echo '<h1 class="animation animated-item-1">'.$db_field['proName'].'</h1>';
echo '<h2 class="animation animated-item-2">'.$db_field['proDescription'].'</h2>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
$counter++;
}
}
?>
public function LoadAllGallery() {
$db_found = $this->connect_Db();
if ($db_found) {
$SQL = "SELECT * FROM tbl_car";
$result = mysql_query($SQL);
return $result;
mysql_free_result($result);
}
else {
$errorMsg = "Database NOT Found, Please connect your administrator ";
}
$this->close_DB();
}
This should work
if ($counter <= 1)
{
// DIV CLASS="ITEM" wrapper starts here
}
//all the rest of the display code
if ($counter <= 1)
{
echo '</div>'; // DIV CLASS="ITEM" end of wrapper
}
$counter++;

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>';
}

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