Related posts, wordpress query - php

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...

Related

Display taxonomy of a post in Wordpress

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 ?

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

Wordpress shortcode ouput via function

What is the best approach for a shortcode to get its output from another function. echo works but it generate the shortcode before all content. before ending file there is a php switch that I am loading different functions in base of which style selected. This is the function:
staticMarkup($titleRendered, $contentRendered, $feedbackRendered, $roleRendered, $companyRendered, $image_urlRendered, $slider, $color);
<?php
add_shortcode('sample', 'sample_shortcode');
function sample_shortcode($atts)
{
global $tesio;
$markup = '';
// Attributes
$atts = shortcode_atts(array(
'style' => '',
'testio_id' => '',
'slider' => '',
'colors' => '',
'color' => '',
'wide' => 'no',
'bgcolor' => '',
'dpadding' => '',
'tpadding' => '',
'spadding' => '',
'instance' => 1
) , $atts);
wp_enqueue_script('testio-front-js');
wp_localize_script('testio-front-js', 'testio_var', $atts);
$testio_id = $explodeID = $style = $slider = $colors = $bgcolor = $vpadding = $color = $wide = $colorContrast = '';
$testio_id = $atts['testio_id'];
$explodeID = explode(',', $testio_id);
$style = $atts['style'];
$slider = $atts['slider'];
$colors = $atts['colors'];
$bgcolor = $atts['bgcolor'];
$dpadding = $atts['dpadding'];
$tpadding = $atts['tpadding'];
$spadding = $atts['spadding'];
$color = $atts['color'];
$wide = $atts['wide'];
$testioAmount = count(explode(',', $testio_id));
if (!empty($colors))
{
$colors = explode(',', $colors);
}
if (!empty($bgcolor))
{
$colorContrast = getContrast50($bgcolor);
}
static $uniqueID;
if ($testio_id !== '')
{
if ($testio_id == 'all')
{
$args = array(
'post_type' => 'testio_wp',
'post_status' => 'publish',
'posts_per_page' => - 1
);
}
else
if ($testioAmount > 1)
{
$args = array(
'post_type' => 'testio_wp',
'post_status' => 'publish',
'post__in' => $explodeID,
'posts_per_page' => $testioAmount,
'orderby' => 'post__in'
);
}
else
if ($testioAmount == 1)
{
$args = array(
'post_type' => 'testio_wp',
'post_status' => 'publish',
'p' => $testio_id,
'posts_per_page' => 1
);
}
$posts = get_posts($args);
$wrapperClass = 'staticWrap';
$wideClass = '';
$sliderClass = '';
$masonryContainer = '';
$masonryContainerClose = '';
$backgroundStyle = '';
$sliderMarkup = '';
$sliderCloseMarkup = '';
$wrapperClassExtra = '';
$dataAttr = 'data-padding = ' . $dpadding . ',' . $tpadding . ',' . $spadding;
switch ($style)
{
case 'static':
if ($wide == 'yes')
{
$wideClass = ' wide';
}
default:
break;
}
if ($testioAmount == 1)
{
$slider == 'no';
$sliderContainer = '';
}
$markup.= '<div class="testioWrap ' . $wrapperClass . $wrapperClassExtra . '" ' . $backgroundStyle . ' data-id="' . $wrapperClass . $uniqueID . '" ' . $dataAttr . '>';
$markup.= $sliderMarkup;
$markup.= $masonryContainer;
$x = 0;
$colorClass = '';
$postCount = '';
foreach($posts as $post)
{
$titleRendered = $post->post_title;
$contentRendered = $post->post_content;
$idRendered = $post->ID;
$feedbackRendered = get_field('testio_rate', $idRendered);
$roleRendered = get_field('testio_role', $idRendered);
$companyRendered = get_field('testio_company', $idRendered);
$image_urlRendered = get_the_post_thumbnail($idRendered, 'thumbnail');
if (!empty($colors))
{
$colorClass = $colors[$x % 5];
}
$x++;
$postCount++;
$freeMessage = '<div class="testio-notice"> Please get the pro version to unlock ' . $style . ' style Upgrade</div>';
switch ($style)
{
case "static":
staticMarkup($titleRendered, $contentRendered, $feedbackRendered, $roleRendered, $companyRendered, $image_urlRendered, $slider, $color);
default:
break;
}
}
$markup.= $masonryContainerClose;
$markup.= $sliderCloseMarkup;
$markup.= '</div>';
return $markup;
}
else
{
$markup.= '<div class="testio">';
$markup.= __('Please put an id to the shortcode', 'testio-wp');
$markup.= '</div>';
}
$uniqueID++;
}
?>
This is the codes inside the staticMarkup function:
<?php
function staticMarkup($title, $content, $feedback, $role, $company, $image_url, $slider, $color){
$markup ='';
$colorStyle = 'style="color: '.$color.';"';
if($slider == 'yes'){
$markup .= '<div>';
}
$markup .= '<div class="testio testioStatic" data-color="'.$color.'">';
$markup .= '<div class="leftBorder"></div>';
if($image_url !== '' ){
$markup .= '<div class="thumbnailWrapper">';
$markup .= $image_url;
$markup .= '</div>';
}
$markup .= feedbackMarkup($feedback);
$markup .= '<div class="testio-content">';
$markup .= '<p>'.$content.'</p>';
$markup .= '</div>';
$markup .= '<h3 class="testio-name" '.$colorStyle.'>~ ' .$title. '</h3>';
$markup .= '<h4 class="testio-role">' .$role. ' <span class="role-divider icon-at-sign"></span> ' .$company. '</h4>';
$markup .= '</div>';
if($slider == 'yes'){
$markup .= '</div>';
}
echo $markup;
}
?>
Within the staticMarkup function replace:
echo $markup;
with:
return $markup;
This will return the variable where it has been positioned/called as opposed to above all content.

how can I image (.png) hide when the metabox empty

I want the button hide when it is empty remain unfortunately only the HTML before, even though the button is already empty, how can I rewrite the code so the button is hidden? I posted more details maybe someone can help me me
function cspm_infobox_content(){
$post_id = esc_attr($_POST['post_id']);
$infobox_type = esc_attr($_POST['infobox_type']);
$map_id = esc_attr($_POST['map_id']);
$status = esc_attr($_POST['status']);
$carousel = esc_attr($_POST['carousel']);
$telnr = get_post_meta( $post_id, 'telnr', true );
$telnr1 = get_post_meta( $post_id, 'telnr1', true );
$email = get_post_meta( $post_id, 'email', true );
$hane = get_post_meta( $post_id, 'hane', true );
$marte = get_post_meta( $post_id, 'marte', true );
$merkure = get_post_meta( $post_id, 'merkure', true );
$enjte = get_post_meta( $post_id, 'enjte', true );
$premte = get_post_meta( $post_id, 'premte', true );
$shtune = get_post_meta( $post_id, 'shtune', true );
$djele = get_post_meta( $post_id, 'djele', true );
$shteti = get_post_meta( $post_id, 'shteti', true );
$qyteti = get_post_meta( $post_id, 'qyteti', true );
$rajoni = get_post_meta( $post_id, 'rajoni', true );
$rruganr = get_post_meta( $post_id, 'rruganr', true );
$no_title = array(); // Infoboxes to display with no title
$no_link = array(); // Infobox to display whit no link
$no_description = array('square_bubble', 'rounded_bubble', 'cspm_type2', 'cspm_type3', 'cspm_type4'); // Infoboxes to display with no description
$no_image = array('cspm_type4'); // Infoboxes to display with no image
if(!in_array($infobox_type, $no_title)) $item_title = apply_filters('cspm_custom_infobox_title', stripslashes_deep($this->cspm_items_title($post_id, $this->items_title)), $post_id);
if(!in_array($infobox_type, $no_description)) $item_description = apply_filters('cspm_custom_infobox_description', stripslashes_deep($this->cspm_items_details($post_id, $this->items_details)), $post_id);
if(!in_array($infobox_type, $no_link)) $the_permalink = $this->cspm_get_permalink($post_id);
if(!in_array($infobox_type, $no_image)){
if($infobox_type == 'square_bubble' || $infobox_type == 'rounded_bubble')
$parameter = array( 'style' => "width:50px; height:50px;" );
elseif($infobox_type == 'cspm_type1')
$parameter = array( 'style' => "width:100px; height:80px;" );
elseif($infobox_type == 'cspm_type2')
$parameter = array();
elseif($infobox_type == 'cspm_type3' || $infobox_type == 'cspm_type5')
$parameter = array( 'style' => "width:60px; height:60px;" );
elseif($infobox_type == 'cspm_type4')
$parameter = array();
if($infobox_type == 'square_bubble' || $infobox_type == 'rounded_bubble'){
$infobox_thumb = get_the_post_thumbnail($post_id, 'cspacing-marker-thumbnail', $parameter);
}elseif($infobox_type == 'cspm_type1'){
$infobox_thumb = get_the_post_thumbnail($post_id, 'cspacing-infobox1-thumbnail', $parameter);
}else $infobox_thumb = get_the_post_thumbnail($post_id, 'cspacing-horizontal-thumbnail', $parameter);
if(empty($infobox_thumb))
$infobox_thumb = get_the_post_thumbnail($post_id, 'cspacing-horizontal-thumbnail', $parameter);
}
$post_thumbnail = apply_filters('cspm_infobox_thumb', $infobox_thumb, $post_id, $infobox_type, $parameter);
$this->infobox_external_link = $this->cspm_get_setting('infoboxsettings', 'infobox_external_link', 'same_window');
$target = ($this->infobox_external_link == 'new_window') ? ' target="_blank"' : '';
$the_post_link = ($this->infobox_external_link == 'disable') ? $item_title : '<a href="'.$the_permalink.'" title="'.$item_title.'"'.$target.'>'.$item_title.'</a>';
$output = '';
$output .= '<div class="cspm_infobox_content_container '.$status.' infobox_'.$map_id.' '.$infobox_type.'" data-map-id="'.$map_id.'" data-post-id="'.$post_id.'" data-show-carousel="'.$carousel.'">';
if($infobox_type == 'square_bubble' || $infobox_type == 'rounded_bubble'){
$output .= '<div class="cspm_infobox_img">';
$output .= ($this->infobox_external_link != 'disable') ? '<a href="'.$the_permalink.'" title="'.$item_title.'"'.$target.'>'.$post_thumbnail.'</a>' : $post_thumbnail;
$output .= '</div>';
$output .= '<div class="cspm_arrow_down '.$infobox_type.'"></div>';
}elseif($infobox_type == 'cspm_type1'){
$output .= '<div class="cspm_infobox_img">'.$post_thumbnail.'</div>';
$output .= '<div class="cspm_infobox_content">';
$output .= '<div class="title">'.$the_post_link.'</div>';
$output .= '<div class="description">'.$item_description.'</div>';
$output .= '</div>';
$output .= '<div style="clear:both"></div>';
$output .= '<div class="cspm_arrow_down"></div>';
}elseif($infobox_type == 'cspm_type2'){
$output .= '<div class="cspm_infobox_img">'.$post_thumbnail.'</div>';
$output .= '<div class="cspm_infobox_content">';
$output .= '<div class="title">'.$the_post_link.'</div>';
$output .= '</div>';
$output .= '<div class="cspm_arrow_down"></div>';
}elseif($infobox_type == 'cspm_type3'){
$output .= '<div class="cspm_infobox_img">'.$post_thumbnail.'</div>';
$output .= '<div class="cspm_infobox_content">';
$output .= '<div class="title">'.$the_post_link.'</div>';
$output .= '</div>';
$output .= '<div class="cspm_arrow_down"></div>';
}elseif($infobox_type == 'cspm_type4'){
$output .= '<div class="cspm_infobox_content">';
$output .= '<div class="title">'.$the_post_link.'</div>';
$output .= '</div>';
$output .= '<div class="cspm_arrow_down"></div>';
/**
* #since 2.7 */
}elseif($infobox_type == 'cspm_type5'){
$output .= '<div class="cspm_infobox_content">';
$output .= '<div>';
$output .= '<div class="cspm_infobox_img">'.$post_thumbnail.'</div>';
$output .= '<div class="title">'.$the_post_link.'</div>';
$output .= '<div class="telnr"><a class="wm-button color-read" href="tel:'.$telnr.'"><img src="http://test.com/test/wp-content/uploads/2015/06/Phone-18-18-dimenzionet.png" height="18" width="18"></a>';
$output .= '<a class="wm-button color-read telnr1" style="left: 10px;" href="tel:'.$telnr1.'"><img src="http://test.com/test/wp-content/uploads/2015/06/Phone-18-18-dimenzionet.png" height="18" width="18"></a>';
$output .= '<a class="wm-button color-blue email" style="left: 20px;" href="mailto:'.$email.'?Subject=Un%20jam%20informuar%20për%20ju%20nga%20www.test.com "><img src="http://test.com/pinfo/wp-content/uploads/2015/06/email-ico.png" height="18" width="18"></a></div>';
if the value $ telnr1 is empty I want which is the whole HTML hidden below
<a class="wm-button color-read telnr1" style="left: 10px;" href="tel:'.$telnr1.'"><img src="../../wp-content/uploads/2015/06/Phone-18-18-dimenzionet.png" height="18" width="18"></a>

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

Categories