Terms by vocabulary in node.tpl - php

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

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 array splice not working

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

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

How can I echo two items per one time by using PHP Foreach?

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.

How to wrap divs in between an if elseif statement in PHP for Wordpress

This is my first shot at creating a combination filter based on David DeSandro's Isotope. I created this function based on an already existing implementation of the Isotope filter on my Website. What it does is outputs WordPress categories and implements them as Isotope filters. As of right now everything works very well. What I am trying to do is style each if elseif statement into its own column after it extracts the data. For example:
**<div class="column"><--I would like to add a div like this here**
elseif (cat_is_ancestor_of(156, $filtered)) {
$option .= '<li class="child-style" data-link="'.$filtered->slug.'">';
$option .= '<a href="" title="'.$filtered->slug.'">';
$option .= ' ';
$option .= $filtered->name;
$option .= '</a></li>';
}
**</div><--and here**
Below is the beginning of my function that I have written to accomplish this. As of now it works, but it outputs the data (in this case categories & child categories) in a neat single column list. I would ultimately like to style this list of categories so that each parent is in it's own column.
This is where I am running into difficulty as I can't seem to find a way to separate the if, elseif statements with div's without getting this error:
Parse error: syntax error, unexpected 'elseif' (T_ELSEIF) in /Users/djmorigeau/localhost/wordpress/wp-content/themes/skittles-child/functions.php on line 89. This is the error that I receive when I wrap each if, elseif statement with <div class="col_3">code</div> Any ideas on how I can group the results into columns?
function color_filter($categories, $type = 'blog') {
$catArgs = array(
'type' => 'post',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'taxonomy' => 'category',
'pad_counts' => false );
$catList = get_categories($catArgs);
if(isset($categories[0])) {
$categories = unserialize($categories[0]);
if (function_exists('color_filter')) {
$option = '<div class="megamenu_container megamenu_dark_bar megamenu_dark"><ul class="megamenu">';
$option .= '<li><a class="megamenu_drop" href="#">Filter<em class="dropdown-arrow"></em></a>';
$option .= '<div class="dropdown_fullwidth"><div class="mpcth-'.$type.'-categories mpcth-filterable-categories">';
$option .= '<ul>';
$option .= '<li class="active" data-link="post">'.__('All', 'mpcth').'</li>';
foreach ($catList as $filtered) {
if((isset($categories[$filtered->slug]) && $categories[$filtered->slug] == 'on') || !isset($categories[$filtered->slug])) {
if (cat_is_ancestor_of(37, $filtered)) {
$option .= '<li class="child-style" data-link="'.$filtered->slug.'">';
$option .= '<a href="" title="'.$filtered->slug.'">';
$option .= ' ';
$option .= $filtered->name;
$option .= '</a></li>';
}
elseif (cat_is_ancestor_of(156, $filtered)) {
$option .= '<li class="child-style" data-link="'.$filtered->slug.'">';
$option .= '<a href="" title="'.$filtered->slug.'">';
$option .= ' ';
$option .= $filtered->name;
$option .= '</a></li>';
}
elseif (cat_is_ancestor_of(176, $filtered)) {
$option .= '<li class="child-style" data-link="'.$filtered->slug.'">';
$option .= '<a href="" title="'.$filtered->slug.'">';
$option .= ' ';
$option .= $filtered->name;
$option .= '</a></li>';
}
elseif (cat_is_ancestor_of(188, $filtered)) {
$option .= '<li class="child-style" data-link="'.$filtered->slug.'">';
$option .= '<a href="" title="'.$filtered->slug.'">';
$option .= ' ';
$option .= $filtered->name;
$option .= '</a></li>';
}
else {
$option .= '<li data-link="'.$filtered->slug.'">';
$option .= ''.$filtered->name.'</li>';
}
}
}
$option .= '</ul></div></div></li></ul></div>';
echo $option;
}
}
}
I think you mean this:
elseif(...) {
$option .= '<div class="column">';
// your code
$option .= '</div>';
}
or you mean this:
$done = false;
$option .= '<div class="column">';
if (cat_is_ancestor_of(37, $filtered)) {
// your code
$done = true;
}
$option .= '</div>';
$option .= '<div class="column">';
if (!$done && cat_is_ancestor_of(156, $filtered)) {
// your code
$done = true;
}
$option .= '</div>';
$option .= '<div class="column">';
if (!$done && cat_is_ancestor_of(176, $filtered)) {
// your code
$done = true;
}
$option .= '</div>';
$option .= '<div class="column">';
if (!$done && cat_is_ancestor_of(188, $filtered)) {
// your code
$done = true;
}
$option .= '</div>';
$option .= '<div class="column">';
if (!$done) {
// your code
}
$option .= '</div>';

Categories