Display taxonomy of a post in Wordpress - php

I have a page where all my WP posts are displayed. The category of the post displays fine but when I try to also display the taxonomy, it doesn't work.
This is working :
<span><i class="fa fa-list"></i><?php $categories = get_the_category();
$separator = ", ";
$output = '';
if($categories){
if($link_on_cat == "yes"){
foreach($categories as $category){
$output .= '' . $category->cat_name . '' . $separator;
}
} else {
foreach($categories as $category){
$output .= $category->cat_name . $separator;
}
}
echo trim($output, $separator);
} ?></span>
This is not working :
<span><i class="fa fa-list"></i><?php $categories = get_the_category();
$separator = ", ";
$output = '';
global $post, $post_id;
$post_type = $post->post_type;
$taxonomies = get_object_taxonomies($post_type);
if($categories){
if($link_on_cat == "yes"){
foreach($categories as $category){
$output .= '' . $category->cat_name . '' . $separator;
}
foreach ($taxonomies as $taxonomy) { $out .= "<li>".$taxonomy.": "; $terms = get_the_terms( $post->ID, $taxonomy );
if ( !empty( $terms ) ) {
foreach ( $terms as $term )
$out .= ''.$term->name.' ';
} }
} else {
foreach($categories as $category){
$output .= $category->cat_name . $separator;
}
}
echo trim($output, $separator);
} ?></span>
Of course I'm doing something wrong, but what ?

Related

How to remove the last comma of a Wordpress php foreach output?

I already tried some things with trim and so on but nothing works.
Maybe I made a mistake and you can help me? The code looks currently like that:
$post = get_post();
$terms = wp_get_post_terms($post->ID, 'genre');
if ($terms)
{
$output = '';
foreach ($terms as $term) {
$output .= '' . $term->name . ', ';
}
};
echo $output;
Try putting in substr_replace($term->name,"",-1), check documentation for more info, this should delete comma from last string.
just add a counter to check the last item of the array
$post = get_post();
$terms = wp_get_post_terms($post->ID, 'genre');
if ($terms)
{
$output = '';
$i = 0;
foreach ($terms as $term) {
$output .= '' . $term->name . ' ';
$output .= ($i == count($terms) - 1) ? '' : ',';
$i++;
}
};
echo $output;

Wordpress menu function

Ok so i made my own theme and i used this code for my menu:
function the_nav_menu() {
$menu_name = 'nav-primary';
if (($locations = get_nav_menu_locations()) && isset($locations[$menu_name])) {
$menu = wp_get_nav_menu_object($locations[$menu_name]);
$menu_items = wp_get_nav_menu_items($menu->term_id);
$current_item = 0;
foreach ((array) $menu_items as $key => $menu_item) {
$current_item++;
$title = $menu_item->title;
$url = $menu_item->url;
if($current_item == 3)
$menu_list .= "\t\t\t\t\t". '<li>'. $title .'<i class="material-icons right">arrow_drop_down</i><ul class="dropdown-content" id="dropdown-nav">' ."\n";
else
$menu_list .= "\t\t\t\t\t". '<li>'. $title .'</li>' ."\n";
}
} else {
// $menu_list = '<!-- no list defined -->';
}
echo $menu_list . '</ul></li>';
}
When i try to use that menu in the header.php i get the following error:
Notice: Undefined variable: menu_list in /home/u191493746/public_html/wp-content/themes/startedu/functions.php on line 56
Any ideeas?
before if inizialize $menu_list="";
function the_nav_menu() {
$menu_name = 'nav-primary';
$menu_list="";
if (($locations = get_nav_menu_locations()) && isset($locations[$menu_name])) {
$menu = wp_get_nav_menu_object($locations[$menu_name]);
$menu_items = wp_get_nav_menu_items($menu->term_id);
$current_item = 0;
foreach ((array) $menu_items as $key => $menu_item) {
$current_item++;
$title = $menu_item->title;
$url = $menu_item->url;
if($current_item == 3)
$menu_list .= "\t\t\t\t\t". '<li>'. $title .'<i class="material-icons right">arrow_drop_down</i><ul class="dropdown-content" id="dropdown-nav">' ."\n";
else
$menu_list .= "\t\t\t\t\t". '<li>'. $title .'</li>' ."\n";
}
} else {
// $menu_list = '<!-- no list defined -->';
}
echo $menu_list . '</ul></li>';
}

Related posts, wordpress query

The theme that I have been using for my wp site has related posts shortcode which works, but it doesn't hide the currently viewed post. I had some idea to add a variable before the query to a post so the query sees that post which is currently viewed has that variable set to true and skip it. Here is the code:
<?php
global $post;
$categories = get_the_category();
$ceker=false;
foreach ($categories as $category[0]) {
if ($ceker == false){
$ceker=true;
?>
content etc...
I use this in my own themes and it works. How can I do something similar to these shortcode that this theme uses:
#BLOG LIST...
if(!function_exists('dt_blog_posts')) {
function dt_blog_posts( $atts, $content = null ) {
extract(shortcode_atts(array(
'show_feature_image' => 'true',
'excerpt_length' => 35,
'show_meta' => 'true',
'limit' => -1,
'categories' => '',
'posts_column' => 'one-column', // one-column, one-half-column, one-third-column
), $atts));
global $post;
$meta_set = get_post_meta($post->ID, '_tpl_default_settings', true);
$page_layout = !empty($meta_set['layout']) ? $meta_set['layout'] : 'content-full-width';
$post_layout = $posts_column;
$article_class = "";
$feature_image = "";
$column = ""; $out = "";
//POST LAYOUT CHECK...
if($post_layout == "one-column") {
$article_class = "column dt-sc-one-column blog-fullwidth";
$feature_image = "blog-full";
}
elseif($post_layout == "one-half-column") {
$article_class = "column dt-sc-one-half";
$feature_image = "blog-twocolumn";
$column = 2;
}
elseif($post_layout == "one-third-column") {
$article_class = "column dt-sc-one-third";
$feature_image = "blog-threecolumn";
$column = 3;
}
//PAGE LAYOUT CHECK...
if($page_layout != "content-full-width") {
$article_class = $article_class." with-sidebar";
$feature_image = $feature_image."-sidebar";
}
//POST VALUES....
if($categories == "") $categories = 0;
//PERFORMING QUERY...
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }
$args = array('post_type' => 'post', 'paged' => $paged, 'posts_per_page' => $limit, 'cat' => $categories, 'ignore_sticky_posts' => 1);
$wp_query = new WP_Query($args);
$pholder = dt_theme_option('general', 'disable-placeholder-images');
if($wp_query->have_posts()): $i = 1;
while($wp_query->have_posts()): $wp_query->the_post();
$temp_class = $format = "";
if($i == 1) $temp_class = $article_class." first"; else $temp_class = $article_class;
if($i == $column) $i = 1; else $i = $i + 1;
$out .= '<div class="'.$temp_class.'"><!-- Post Starts -->';
$out .= '<article id="post-'.get_the_ID().'" class="'.implode(" ", get_post_class("blog-post", $post->ID)).'">';
if($show_meta != "false"):
$out .= '<div class="post-details"><!-- Post Details Starts -->';
$out .= '<div class="date"><span>'.get_the_date('d').'</span>'.get_the_date('M').'<br />'.get_the_date('Y').'</div>';
$out .= '<div class="post-comments">';
$commtext = "";
if((wp_count_comments($post->ID)->approved) == 0) $commtext = '0';
else $commtext = wp_count_comments($post->ID)->approved;
$out .= ''.$commtext.' <i class="fa fa-comment"></i>';
$out .= '</div>';
$format = get_post_format();
$out .= '<span class="post-icon-format"> </span>';
$out .= '</div><!-- Post Details ends -->';
endif;
$out .= '<div class="post-content"><!-- Post Content Starts -->';
$out .= '<div class="entry-thumb">';
if(is_sticky()):
$out .= '<div class="featured-post">'.__('Featured','iamd_text_domain').'</div>';
endif;
//POST FORMAT STARTS
if( $format === "image" || empty($format) ):
if( has_post_thumbnail() && $show_feature_image != 'false'):
$out .= '<a href="'.get_permalink().'" title="'.get_the_title().'">';
$attr = array('title' => get_the_title()); $out .= get_the_post_thumbnail($post->ID, $feature_image, $attr);
$out .= '</a>';
elseif($pholder != "true" && $show_feature_image != 'false'):
$out .= '<a href="'.get_permalink().'" title="'.get_the_title().'">';
$out .= '<img src="http://placehold.it/840x340&text='.get_the_title().'" alt="'.get_the_title().'" />';
$out .= '</a>';
endif;
elseif( $format === "gallery" ):
$post_meta = get_post_meta($post->ID ,'_dt_post_settings', true);
$post_meta = is_array($post_meta) ? $post_meta : array();
if( array_key_exists("items", $post_meta) ):
$out .= "<ul class='entry-gallery-post-slider'>";
foreach ( $post_meta['items'] as $item ) { $out .= "<li><img src='{$item}' alt='gal-img' /></li>"; }
$out .= "</ul>";
endif;
elseif( $format === "video" ):
$post_meta = get_post_meta($post->ID ,'_dt_post_settings', true);
$post_meta = is_array($post_meta) ? $post_meta : array();
if( array_key_exists('oembed-url', $post_meta) || array_key_exists('self-hosted-url', $post_meta) ):
if( array_key_exists('oembed-url', $post_meta) ):
$out .= "<div class='dt-video-wrap'>".wp_oembed_get($post_meta['oembed-url']).'</div>';
elseif( array_key_exists('self-hosted-url', $post_meta) ):
$out .= "<div class='dt-video-wrap'>".wp_video_shortcode( array('src' => $post_meta['self-hosted-url']) ).'</div>';
endif;
endif;
elseif( $format === "audio" ):
$post_meta = get_post_meta($post->ID ,'_dt_post_settings', true);
$post_meta = is_array($post_meta) ? $post_meta : array();
if( array_key_exists('oembed-url', $post_meta) || array_key_exists('self-hosted-url', $post_meta) ):
if( array_key_exists('oembed-url', $post_meta) ):
$out .= wp_oembed_get($post_meta['oembed-url']);
elseif( array_key_exists('self-hosted-url', $post_meta) ):
$out .= wp_audio_shortcode( array('src' => $post_meta['self-hosted-url']) );
endif;
endif;
else:
if( has_post_thumbnail() && $show_feature_image != 'false'):
$out .= '<a href="'.get_permalink().'" title="'.get_the_title().'">';
$attr = array('title' => get_the_title()); $out .= get_the_post_thumbnail($post->ID, $feature_image, $attr);
$out .= '</a>';
elseif($pholder != "true" && $show_feature_image != 'false'):
$out .= '<a href="'.get_permalink().'" title="'.get_the_title().'">';
$out .= '<img src="http://placehold.it/840x340&text='.get_the_title().'" alt="'.get_the_title().'" />';
$out .= '</a>';
endif;
endif;
//POST FORMAT ENDS
$out .= '</div>';
$out .= '<div class="entry-detail">';
$out .= '<h2>'.get_the_title().'</h2>';
if($excerpt_length != "" || $excerpt_length != 0) $out .= dt_theme_excerpt($excerpt_length);
$out .= '</div>';
if($show_meta != "true"):
$out .= '<div class="post-meta">';
$out .= '<div class="opsirnije">';
$out .= ''.'Opširnije »'.'';
$out .= '<ul>';
$out .= '<li><span class="fa fa-user"></span>'.get_the_author_meta('display_name').'</li>';
$categories = get_the_category();
$thiscats = "";
if($categories) {
foreach($categories as $category) {
$thiscats .= ''.$category->cat_name.', '; }
$thiscats = substr($thiscats,0, (strlen($thiscats)-2));
$out .= '<li><span class="fa fa-thumb-tack"></span>'.$thiscats.'</li>';
}
$out .= get_the_tag_list('<li><span class="fa fa-pencil"></span>', ', ', '</li>');
$out .= '</ul>';
$out .= '</div>';
endif;
$out .= '</div><!-- Post Content Ends -->';
$out .= '</article>';
$out .= '</div><!-- Post Ends -->';
endwhile;
if($wp_query->max_num_pages > 1):
$out .= '<div class="pagination-wrapper">';
if(function_exists("dt_theme_pagination")) $out .= dt_theme_pagination("", $wp_query->max_num_pages, $wp_query);
$out .= '</div>';
endif;
wp_reset_query($wp_query);
else:
$out .= '<h2>'.__('Nothing Found.', 'iamd_text_domain').'</h2>';
$out .= '<p>'.__('Apologies, but no results were found for the requested archive.', 'iamd_text_domain').'</p>';
endif;
return $out;
}
add_shortcode('dt_blog_posts', 'dt_blog_posts');
add_shortcode('dt_sc_blogposts', 'dt_blog_posts');
}
Answer was simple. Just added
'post__not_in' => array( get_the_ID() ))
in array.
Thx all...

How to filter featured products slider by category?

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

Wrapping foreach result in a div

I have this function working great
<?php
$categories = get_categories(array(
'hide_empty' => 0,
'taxonomy' => 'category',
'orderby' => 'title',
'order' => 'ASC'
));
foreach ($categories as $category) {
$firstletter = strtoupper(substr($category->name,0,1));
$link = get_category_link( $category->term_id );
if ($firstletter != $current) {
$postlist .= "<b><a name='$firstletter'> $firstletter </a></b><br>\n";
$nav .= "<a href='#$firstletter'> $firstletter </a> ";
$current = $firstletter;
}
$postlist .= "<a href='$link'>" . $category->cat_name . "</a><br>\n";
}
print $nav . "<br>" . $postlist;
?>
What I need, is to wrap the entire list starting with the letter. For example, Letter M will be wrapped in a div with all the categories starting with M.
This is what I currently get: http://i.imgur.com/PEmJw03.png - Any help would be appreciated, thank you.
You seem to be on the right path. I think this is what you are requesting to do:
$closeLastDiv = false;
foreach ($categories as $category) {
$firstletter = strtoupper(substr($category->name,0,1));
$link = get_category_link( $category->term_id );
if ($firstletter != $current) {
if ($firstletter == ""){ //first time through
$postlist .= "<div>";
$closeLastDiv = true;
} else {
$postlist .= "</div><div>";
}
$postlist .= "<b><a name='$firstletter'> $firstletter </a></b><br>\n";
$nav .= "<a href='#$firstletter'> $firstletter </a> ";
$current = $firstletter;
}
$postlist .= "<a href='$link'>" . $category->cat_name . "</a><br>\n";
}
if ($closeLastDiv) {
$postlist .= "</div>";
}
All you need is comparte the current letter with before letter, and if changed print the starting wrapper + content. When the letter changes, close the wrapper and open the next wraper.
Something like this:
$current = "-";
$postlist .= "<div>";
foreach ($categories as $category) {
$firstletter = strtoupper(substr($category->name,0,1));
$link = get_category_link( $category->term_id );
if ($firstletter != $current) {
$postlist .= "</div><div id='{$firstletter}'>";
$postlist .= "<b><a name='{$firstletter}'>".$firstletter."</a></b><br>\n";
$nav .= "<a href='#{$firstletter}'>".$firstletter."</a> ";
$current = $firstletter;
}
$postlist .= "<a href='$link'>" . $category->cat_name . "</a><br>";
}
$postlist .= "</div>";
The first div remain empty, but all other are ok.

Categories