PHP array splice not working - php

I'm trying to build a shortcode in wp that displays data from 2 RSS feeds.
The problem is that I can't limit the number of items in array. I tried these solutions and none worked.
function rss_posts_func( $atts ){
$feed = fetch_feed(array('rss-feed-1', 'rss-feed-2'));
$feed = array();
$feed = array_splice($feed, 0, 3);
// Loop the results
$content = '<ul class="rss-aggregator">';
foreach($feed->get_items() as $item) {
$content .= '<li class="feed-item">';
$content .= '<a href='.$item->get_permalink().'>';
$content .= $item->get_title();
$content .= '</a></li>';
}
$content .= '</ul>';
return $content;
}

Fixed with:
function rss_posts_func( $atts ){
$i = 1;
$feed = fetch_feed(array('rss-feed-1', 'rss-feed-2'));
$content = '<ul class="rss-aggregator">';
foreach($feed->get_items() as $item) {
$content .= '<li class="feed-item">';
$content .= '<a href='.$item->get_permalink().'>';
$content .= $item->get_title();
$content .= '</a></li>';
if($i++ == 8) break;
}
$content .= '</ul>';
return $content;
}

Related

Counting items in a PHP array_chunk

I have the below function which gets wordpress posts and outputs them in a bootstrap grid (3 posts per row). Each array chunk has 3 posts in it. Basically what I want to do is if the chunk is not complete and only has say 2 posts in it then the first post in that chunk gets "col-sm-offset-2" class added. I believe I need some way off counting the posts in the chunk but I'm not sure how to implement.
function post_events($atts) {
global $post;
$args = array(
'post_type' => 'event',
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'ASC',
);
$posts = get_posts($args);
$posts_chunks = array_chunk($posts, 3);
$output = '';
foreach ($posts_chunks as $row) {
$output .= '<div class="row">';
foreach ($row as $post) {
setup_postdata($post);
$output .= '<div class="col-md-6 col-sm-6 event-item">';
$output .= '' .get_the_post_thumbnail(). '';
$output .= '<div class="event-item-text">';
$output .= '<h3>' .get_the_title(). '</h3>';
$output .= '<span class="event-date">' .get_the_date("d-m-Y"). '</span>';
$output .= '<p>' .wp_trim_words( get_the_content(), 40, '...' ). '</p>';
$output .= '</div>';
$output .= '</div>';
}
$output .= '</div>';
}
return $output;
}
add_shortcode('post_events','post_events');
You can just use count() on $row, and then set the class based on whether it's the first iteration or not:
$class = 'col-md-6 col-sm-6 event-item';
$count = count($row);
foreach( $row as $k => $post )
{
$cls = ($k == 0 && $count < 2) ? $class.' col-sm-offset-2' : $class;
setup_postdata($post);
$output.= '<div class="'.$cls.'">';
//...
}
Since you have chunked, the subarrays will be 0 based and the first post will be 0. So just check that and if the count is less than 3:
foreach ($posts_chunks as $row) {
$output .= '<div class="row">';
foreach ($row as $key => $post) {
$extra = ''; //set extra class to empty
if($key == 0 && count($row) < 3) { //check first post and count less than 3
$extra = 'col-sm-offset-2 '; //set extra class
}
setup_postdata($post);
//use $extra somewhere
$output .= '<div class="'.$extra.'col-md-6 col-sm-6 event-item">';
$output .= '' .get_the_post_thumbnail(). '';
$output .= '<div class="event-item-text">';
$output .= '<h3>' .get_the_title(). '</h3>';
$output .= '<span class="event-date">' .get_the_date("d-m-Y"). '</span>';
$output .= '<p>' .wp_trim_words( get_the_content(), 40, '...' ). '</p>';
$output .= '</div>';
$output .= '</div>';
}
$output .= '</div>';
}

PHP explode function find last child

I have a php plugin that explodes one line into several lines.
$graph_lines = explode( ";", $content );
$output= '';
$output .= '<ul class="lpd-bullet-list';
if($image){
$output .= ' lpd-bl-custom-icon';
}
if($style){
$output .= ' '.esc_attr($style);
}
$output .= '">';
foreach ($graph_lines as $line) {
if($line){
$output .= '<li>';
$output .= $image;
$output .= $line;
$output .= ';';
$output .= '</li>';
}
}
$output .= '</ul>';
echo $output;
After each line in the end it set ; Thats OK, but how to set . for last element? Is it possible?
Mey be i need to use end() function or another?
Help me please!
I am not very sure about your output but yes you can use end function like this:
$last_index = end(array_keys($graph_lines));
foreach ($graph_lines as $index => $line) {
if ($index == $last_index) {
// last index
} else {
// perform other tasks
}
}
Alternatively, you can use like this:
foreach ($graph_lines as $line) {
if($line){
$output .= '<li>';
$output .= $image;
$output .= $line;
$output .= ';';
if (next($graph_lines)==false) $output .= '.';//not sure where you put it.
$output .= '</li>';
}
}
You can get the number of line sizeof($graph_lines) and create a counter in your foreach.
Exemple :
$numberofline = sizeof($graph_lines);
$i=0;
foreach ($graph_lines as $line) {
if($line){
$i++;
$endofline=";";
if($i==$numberofline)
{
$endofline=".";
}
$output .= '<li>';
$output .= $image;
$output .= $line;
$output .= $endofline;
$output .= '</li>';
}
}
$content = "a;b;c;d;s;r;t;g";
$content = rtrim($content, ";");
$content = ltrim($content, ";");
$graph_lines = explode(";", $content);
$output = '';
$output .= '<ul class="lpd-bullet-list">';
$last = count($graph_lines) - 1;
$i = 0;
foreach ($graph_lines as $line) {
if ($line) {
$output .= '<li>';
$output .= $line;
$output .= ($last == $i)? '.' : ";";
$output .= '</li>';
$i++;
}
}
$output .= '</ul>';
echo $output;
You can use an shorthand IF to check if the current line is matching the last element in the array.
foreach ($graph_lines as $line) {
if($line){
$output .= '<li>';
$output .= $image;
$output .= $line;
$output .= ( $line === end ( $graph_lines ) ) ? '.' : ';';
$output .= '</li>';
}
}
An other way is to set a counter and check if the counter is equal to the count() of $graph_lines.
$counter = 1;
$total_items = count ( $graph_lines );
foreach ( $graph_lines as $line ) {
// do your stuff.
$output .= ( $counter === $total_items ) ? '.' : ';';
$counter++;
}
Count the array. and check that with the $i.
$count = count($graph_lines);
$i = 0;
foreach ($graph_lines as $line) {
if ($line) {
if ($i == $count) {
$output .= '<li>';
$output .= $image;
$output .= $line;
$output .= '.';
$output .= '</li>';
} else {
$output .= '<li>';
$output .= $image;
$output .= $line;
$output .= ';';
$output .= '</li>';
}
}
$i++;
}

Php Array list items with different class

This is my current array that displays list items in a unordered list.
function get_bottle_colors() {
if(empty($_GET['cap_id'])) return false;
$constructor_img = get_post_meta($_GET['cap_id'], 'product_constructor_image', true);
if(is_array($constructor_img) && count($constructor_img)>0 && !empty($constructor_img[0]['title'])){
$output = '<label>Bottle Color</label><ul>';
foreach ($constructor_img as $key => $image) {
if(empty($image['image'])) continue;
$output .= '<li><a href="'.$image['image'].'" data-width="'.$img_size[0].'" data-height="'.$img_size[1].'"';
$output .= '</a></li>';
}
$output .= '</ul>';
}else{
$output = '<label>Bottle Color</label><ul></ul>';
}
echo $output;
die();
}
In total there will be up to 16 list items generated by this. I need each list item to have its own class eg: list class="red", list class="green", etc. Any idea how i go about achieving this?
Found the solution thanks to Anant. Had to declare the class array like below.
function get_bottle_colors() {
if(empty($_GET['cap_id'])) return false;
$constructor_img = get_post_meta($_GET['cap_id'], 'product_constructor_image', true);
if(is_array($constructor_img) && count($constructor_img)>0 && !empty($constructor_img[0]['title'])){
$output = '<label>Bottle Color</label><ul>';
$i = 0;
$class_array = array("a","b","c","d","e","f","g","h","i","j","k","l","n","m","n","o","p");
foreach ($constructor_img as $key => $image) {
if(empty($image['image'])) continue;
$category = 9;
$img_size = getimagesize($image['image']);
$output .= '<li class= "'.$class_array[$i].'"><a href="'.$image['image'].'" data-width="'.$img_size[0].'"
data-height="'.$img_size[1].'"';
$output .= 'data-id="'.$_GET['cap_id'].'_'.$key.'" data-part="#constructor-bottles" class="sub-caps">'.$image['title'];
$output .= '</a></li>';
$i++;
}
$output .= '</ul>';
}else{
$output = '
<label>Bottle Color</label><ul></ul>'; } echo $output; die(); }

Writing out the category of a product from Duka Press Plugin

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"]

Terms by vocabulary in node.tpl

I've created a variable in template.php that let's me print terms by vocabulary. The problem is that I want to be able to pass in a vocabulary id to select a specific vocabulary. My code looks like this:
function xnalaraart_classic_print_terms($node, $vocabularies){
foreach($vocabularies as $vocabulary){
if($terms = taxonomy_node_get_terms_by_vocabulary($node, $vocabulary->vid)){
$output .= '<div>';
$output .= '<ul class="links inline">';
foreach ($terms as $term){
$output .= '<li class="taxonomy_term_' . $term->tid . '">';
$output .= l($term->name, taxonomy_term_path($term), array('rel' => 'tag', 'title' => strip_tags($term->description)));
$output .= '</li>';
}
$output .= '</ul>';
$output .= '</div>';
}
}
return $output;
}
and in the preprocess_node function:
$vars['terms_split'] = xnalaraart_classic_print_terms($vars['node']);
How do I write it so that I can pass in an id to $vocabularies?
I think you made this more difficult on yourself than it really is. See below for final function.
function xnalaraart_classic_print_vocab_terms($node, $vid){
if($terms = taxonomy_node_get_terms_by_vocabulary($node, $vid)){
$output .= '<div>';
$output .= '<ul class="links inline">';
foreach ($terms as $term){
$output .= '<li class="taxonomy_term_' . $term->tid . '">';
$output .= l($term->name, taxonomy_term_path($term), array('rel' => 'tag', 'title' => strip_tags($term->description)));
$output .= '</li>';
}
$output .= '</ul>';
$output .= '</div>';
}
return $output;
}
And then call
$vars['terms_split'] = xnalaraart_classic_print_terms($vars['node'], 10); //Where 10 is the vocab ID

Categories