I’m trying to sort the rows in the while so that they are 3,2,1 instead of 1,2,3. Would be dope if it could be sorted by $week as it returns a numerical value. I read the ACF page on sorting rows but given I already have some custom code in here to exclude the last row in the while I am not sure how to merge it all.
function smile_gallery( $atts ) {
if( have_rows('smile_gallery', 3045) ):
$i = 1;
$html_out = '';
$html_out .= '<div class="smile-container">';
// vars
$rows = get_field('smile_gallery', 3045);
$end_row = end($rows);
$recent = $end_row['smile_week'];
$count = count( get_field('smile_gallery', 3045) );
$html_out .= '<div class="col-md-6">';
$html_out .= '<div class="smile-recent">';
$html_out .= $recent;
$html_out .= '</div>';
$html_out .= '</div>';
$html_out .= '<div class="col-md-6">';
$html_out .= '<div class="smile-thumbs">';
while( have_rows('smile_gallery', 3045) ): the_row();
// vars
$week = get_sub_field('smile_week', 3045);
$img = get_sub_field('smile_img', 3045);
$caption = get_sub_field('smile_caption', 3045);
// Do stuff with each post here
if( $i < $count) :
// vars
$order = array();
$html_out .= '<div class="smile-thumb-container">';
$html_out .= '<div class="smile-thumb-img" style="background: url('. $img .') no-repeat center top; background-size: cover;">';
$html_out .= '</div>'; // smile thumb img
$html_out .= '<a class="smile-thumb-week" href="' . $img . '" data-lightbox="smile-set" ';
if ($caption) :
$html_out .= 'data-title="' . 'Week ' . $week . ' - ' . $caption . '"';
endif;
$html_out .= '><h6>Week ' . $week . '</h6></a>'; // smile thumb week
$html_out .= '</div>'; // smile thumb container
endif;
$i++;
endwhile;
$html_out .= '</div>';
$html_out .= '</div>';
$html_out .= '</div>'; // smile container
endif;
return $html_out;
}
add_shortcode( 'show_smiles', 'smile_gallery' );
Related
I've been trying to read up on this in the manual, but basically I've got an array where I'm trying to reverse it and exclude the last item. So I got 14 items currently in my array and I'm getting it to reverse and it shows 14-2. My code got it to exclude the last item. So I guess it technically works, but I actually want it to output as 13-1. I tried using array_pop and array_shift but I didn't know how to integrate it with array_reverse.
function smile_gallery( $atts ) {
if( have_rows('smile_gallery', 3045) ):
$i = 1;
$html_out = '';
$html_out .= '<div class="smile-container">';
$html_out .= '<div class="fg-row row">';
// vars
$rows = get_field('smile_gallery', 3045);
$count = count( get_field('smile_gallery', 3045) );
$html_out .= '<div class="col-md-8">'; // col-md-offset-1
$html_out .= '<div class="smile-thumbs">';
foreach( array_reverse($rows) as $row) :
// vars
$week = "smile_week";
$img = "smile_img";
$caption = "smile_caption";
// Do stuff with each post here
if( $i < $count) :
$html_out .= '<div class="smile-thumb-container">';
$html_out .= '><h6>Week ' . $row["smile_week"] . '</h6></a>'; // smile thumb week
$html_out .= '</div>'; // smile thumb container
endif;
$i++;
endforeach;
$html_out .= '</div>';
$html_out .= '</div>';
$html_out .= '</div>';
$html_out .= '</div>'; // smile container
endif;
return $html_out;
}
add_shortcode( 'show_smiles', 'smile_gallery' );
I'm reading your question as the following, correct me if I'm wrong.
I've got an array where I'm trying to reverse it and exclude the first and last items.
To do that as you know you're want to use array_pop() and array_shift().
<?php
//
$rows = get_field('smile_gallery', 3045);
$count = count($rows);
array_pop($rows);
array_shift($rows);
foreach (array_reverse($rows) as $row):
...
If you want to reverse first and then do your operations, which is not required if your removing items from both ends. Take out array_reverse from the foreach and then do your manipulations.
<?php
// vars
$rows = get_field('smile_gallery', 3045);
$count = count($rows);
$rows = array_reverse($rows);
array_pop($rows);
array_shift($rows);
foreach ($rows as $row):
...
Let me know if that helps.
Have the following Array that functions perfectly on the site, but before the first output the word "Array" is printed.
I figure it has to be in the $json_object or $fbdata query but cannot isolate or eliminate it from showing.
<?php
$page_id = '{page_id_here}';
$access_token = '{access_token_here}';
//Get the JSON
$json_object = #file_get_contents('https://graph.facebook.com/' . $page_id .
'/posts?fields=full_picture,link,message&limit=3&access_token=' .
$access_token);
//Interpret data
$fbdata = json_decode($json_object);
foreach ($fbdata->data as $post )
{
$posts .= '<div class="col-sm-4">';
$posts .= '<div class="stay-connected-inner">';
$posts .= '<div class="stay-connected-info">';
$posts .= '<div class="stay-connected-left"><i class="fa fa-facebook"></i></div>';
$posts .= '<div class="stay-connected-right">';
$posts .= '<h5>Title</h5>';
$posts .= '<p>' . $post->message . '</p>';
$posts .= '</div>';
$posts .= '</div>';
$posts .= '<div class="stay-connected-fig">';
$posts .= '<p><img src="' . $post->full_picture . '"></p>';
$posts .= '</div>';
$posts .= '</div>';
$posts .= '</div>';
}
//Display the posts
print_r ($posts)
?>
Declare $posts as a String before your foreach loop.
<?php
$page_id = '{page_id_here}';
$access_token = '{access_token_here}';
//Get the JSON
$json_object = #file_get_contents('https://graph.facebook.com/' . $page_id .
'/posts?fields=full_picture,link,message&limit=3&access_token=' .
$access_token);
//Interpret data
$fbdata = json_decode($json_object);
$posts = ''; // Add this
foreach ($fbdata->data as $post )
{
$posts .= '<div class="col-sm-4">';
$posts .= '<div class="stay-connected-inner">';
$posts .= '<div class="stay-connected-info">';
$posts .= '<div class="stay-connected-left"><i class="fa fa-facebook"></i></div>';
$posts .= '<div class="stay-connected-right">';
$posts .= '<h5>Title</h5>';
$posts .= '<p>' . $post->message . '</p>';
$posts .= '</div>';
$posts .= '</div>';
$posts .= '<div class="stay-connected-fig">';
$posts .= '<p><img src="' . $post->full_picture . '"></p>';
$posts .= '</div>';
$posts .= '</div>';
$posts .= '</div>';
}
//Display the posts
print_r ($posts)
?>
Otherwise it is making $posts into an array, so when it is printed, it also prints that it's an array.
Figured it out. I changed my 'posts' variable to 'postst'. The word 'post' was being used in the URL and that seems to have confused the array in some way.
Updated code:
<?php
$page_id = '{page_id_here}';
$access_token = '{access_token_here}';
//Get the JSON
$json_object = #file_get_contents('https://graph.facebook.com/' . $page_id .
'/posts?fields=full_picture,link,message&limit=3&access_token=' .
$access_token);
//Interpret data
$fbdata = json_decode($json_object);
foreach ($fbdata->data as $post )
{
$postst .= '<div class="col-sm-4">';
$postst .= '<div class="stay-connected-inner">';
$postst .= '<div class="stay-connected-info">';
$postst .= '<div class="stay-connected-left"><i class="fa fa-facebook"></i></div>';
$postst .= '<div class="stay-connected-right">';
$postst .= '<h5>Title</h5>';
$postst .= '<p>' . $post->message . '</p>';
$postst .= '</div>';
$postst .= '</div>';
$postst .= '<div class="stay-connected-fig">';
$postst .= '<p><img src="' . $post->full_picture . '"></p>';
$postst .= '</div>';
$postst .= '</div>';
$postst .= '</div>';
}
//Display the posts
print_r ($postst);
?>
I'm using PHP to echo my products from the database. If we just use foreach, we will get the result one item per a loop, but actually I want it echo two items per one times as ub the below function.
This is my PHP function using foreach to fetch data from database.
I've used row item selector to wrap product selector, so I want to echo a block of product two times, and then it should echo the row item.
Example: row item = 1 then product = 2
public function last_upated_products() {
$data = $this->newest_products_from_db('products');
$out = '';
if ($data) {
foreach ($data as $k => $row) {
$out .= '<div class="row item">';
$out .= '<div class="product">';
$out .= '<div class="image">';
$out .= '<img src="' . base_url('asset/img/main/9.jpg') . '" alt="img" class="img-responsive">';
$out .= '<div class="promotion"><span class="discount">' . $row['prod_id'] . '% OFF</span> </div>';
$out .= '</div>';
$out .= '<div class="description"><div class="price"><span>$' . $row['prod_price'] . '</span></div><h4>' . $row['prod_name'] . '</h4>';
$out .= '<p>' . $row['prod_descrip'] . '</p>';
$out .= '</div>';
$out .= '</div>';
$out .= '</div>';
}
}
return $out;
}
This function will echo one item for a loop.
You can not print two times in one iteration of a loop. You can use conditional HTML output to do this job.
Consider this:
function last_upated_products() {
$data = $this->newest_products_from_db('products');
$out = '';
$counter = 1;
$length = count($data);
if ($data) {
foreach ($data as $k => $row) {
if ($counter % 2 != 0) {
$out .= '<div class="row item">';
}
$out .= '<div class="product">';
$out .= '<div class="image">';
$out .= '<img src="' . base_url('asset/img/main/9.jpg') . '" alt="img" class="img-responsive">';
$out .= '<div class="promotion"><span class="discount">' . $row['prod_id'] . '% OFF</span> </div>';
$out .= '</div>';
$out .= '<div class="description"><div class="price"><span>$' . $row['prod_price'] . '</span></div><h4>' . $row['prod_name'] . '</h4>';
$out .= '<p>' . $row['prod_descrip'] . '</p>';
$out .= '</div>';
$out .= '</div>';
if ($counter % 2 == 0 || $length == $counter) {
$out .= '</div>';
}
$counter++;
}
}
return $out;
}
You can use the modulus operator to make the check. If your iterator is a multiple of two it will output the appropriate elements (it does this by checking that the remainder is zero):
public function last_upated_products() {
$data = $this->newest_products_from_db('products');
$out = '';
if ($data) {
$i = 0;
foreach ($data as $k => $row) {
if( ($i % 2) === 0) {
$out .= '<div class="row item">';
}
$out .= '<div class="product">';
$out .= '<div class="image">';
$out .= '<img src="' . base_url('asset/img/main/9.jpg') . '" alt="img" class="img-responsive">';
$out .= '<div class="promotion"><span class="discount">' . $row['prod_id'] . '% OFF</span> </div>';
$out .= '</div>';
$out .= '<div class="description"><div class="price"><span>$' . $row['prod_price'] . '</span></div><h4>' . $row['prod_name'] . '</h4>';
$out .= '<p>' . $row['prod_descrip'] . '</p>';
$out .= '</div>';
$out .= '</div>';
if( ($i + 1) % 2 === 0 || ($i + 1) === count($data) ) {
$out .= '</div>';
}
$i++;
}
}
return $out;
}
Note that the last bit ($i + 1) === count($data) is important in the event that your set holds an uneven number.
I'm trying to display a "featured products slider" on the Category Page of the WooCommerce shop in such a way that only 'featured' products of the current category (or a specific child category) will be displayed in the slider.
I've placed the code below in the functions.php file of my child-theme.
I tried adding this conditional in archive.php:
if(is_product_category('slug'){echo featured_products_slider_fun($atts, $content);}
I can't figure out how to combine this condition with the function below to essentially show 'featured products' of a certain category'.
===========
function featured_products_slider_func( $atts, $content ) {
extract( shortcode_atts( array(
'num' => 4
), $atts ) );
$return = '';
$first_product_title = "";
$first_product_excerpt = "";
function get_the_popular_excerpt(){
$excerpt = get_the_content();
$excerpt = preg_replace(" (\[.*?\])",'',$excerpt);
$excerpt = strip_shortcodes($excerpt);
$excerpt = strip_tags($excerpt);
$excerpt = substr($excerpt, 0, 190);
$excerpt = substr($excerpt, 0, strripos($excerpt, " "));
$excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt));
$excerpt = $excerpt.'. view product';
return $excerpt;
}
$post = new WP_Query(array(
'post_status' => 'publish',
'post_type' => 'product',
'meta_key' => '_featured',
'meta_value' => 'yes',
'posts_per_page' => $num
));
$return .= '<div class="featured-slider-outer box clearfix"><div class="featured-slider-inner">';
$return .= '<ul class="products featured-slider">';
$selected_class = " selected";
if ($post ->have_posts()) : while ($post ->have_posts()) : $post ->the_post();
global $product;
if( $selected_class != "" ) {
$first_product_title = get_the_title();
$first_product_excerpt = get_the_popular_excerpt();
}
$return .= '<li class="product' . $selected_class . '">';
$return .= get_the_post_thumbnail($post->ID, "blog-two-column");
if ($price_html = $product->get_price_html()) :
$return .= '<span class="price">';
$return .= $price_html;
$return .= '</span>';
endif;
$return .= "<div class='none'>";
$return .= "<h3 id='infos-title'>" . get_the_title() . "</h3>";
$return .= "<div id='infos-excerpt'>" . get_the_popular_excerpt() . "</div>";
$return .= "</div>";
$return .= '</li>';
$selected_class = "";
endwhile;
$return .= '</ul>';
$return .= '</div>';
$return .= '<div class="featured-slider-right">';
$return .= '<button class="btn-left"></button><button class="btn-right"></button><h2>' . $first_product_title . '</h2>';
$return .= '<div class="description">' . $first_product_excerpt . '</div>';
$return .= '</div>';
$return .= '</div>';
else:
$return = __("No featured products found!", "shopifiq");
endif;
return $return;
}
add_shortcode( 'featured_products_slider', 'featured_products_slider_func' );
I'm trying to modify a wordpress plugin called Duka Press, and I'm a complete noobie to PHP. It uses shortcodes which produces the template for the product grid, and I basically just need to write out all the categories the product has to create jQuery filter function.
To better understand what I need read the HTML comment in the code underneath
CODE:
<ul>
I need this for each category inside the active category set i the shortcode:
<li>[CategoryNameHere]</li>
</ul>
$products = get_posts($order_string . 'numberposts=' . $per_page . '&post_type=' . $type . '&meta_key=price&category=' . $category . $offset);
if (is_array($products) && count($products) > 0) {
$content .= '<div class="dpsc_grid_display">';
$count = 1;
$all_count = 0;
foreach ($products as $product) {
$output = dpsc_get_product_details($product->ID, $p_b_n, $direct_checkout);
if ($output) {
$attachment_images = &get_children('post_type=attachment&post_status=inherit&post_mime_type=image&post_parent=' . $product->ID);
$main_image = '';
foreach ($attachment_images as $image) {
$main_image = $image->guid;
break;
}
$prod_permalink = get_permalink($product->ID);
$content .= '<div class="dpsc_grid_product">';
$content .= '<div class="dpsc_grid_product_image">';
if ($main_image != '') {
$content .= '<img src="' . DP_PLUGIN_URL . '/lib/timthumb.php?src=' . $main_image . '&w=' . $dp_shopping_cart_settings['g_w'] . '&h=' . $dp_shopping_cart_settings['g_h'] . '&zc=1" >';
}
$content .= '</div>';
<!-- In the following line i need to write out all the categories for the current product -->
$content .= '<div class="dpsc_grid_product_detail [Categories here]">';
$content .= '<p class="title">' . __($product->post_title) . '</p>';
$content .= '<p class="detail">' . $product->post_excerpt . '</p>';
$content .= '<p class="price">' . $output['price'] . '</p>';
$content .= $output['start'];
$content .= $output['add_to_cart'];
$content .= $output['end'];
$content .= '</div>';
$content .= '</div>';
if ($count === intval($column)) {
$content .= '<div class="clear"></div>';
$count = 0;
}
$count++;
$all_count++;
}
}
$content .= '<div class="clear"></div>' . $page_links . '<div class="clear"></div>';
$content .= '</div>';
$content .= '<div class="clear"></div>';
}
return $content;
Shortcode that uses the template above:
[dpsc_grid_display category="22" total="100" column="3" per_page="100" type="duka"]