include/lcp-widget.php – modified function widget()
function widget($args, $instance) {
global $post;
extract( $args );
$title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']);
$limit = (is_numeric($instance['limit'])) ? $instance['limit'] : 5;
$orderby = ($instance['orderby']) ? $instance['orderby'] : 'date';
$order = ($instance['order']) ? $instance['order'] : 'desc';
// EXCLIDE ARRAY
$exclude = ($instance['exclude'] != '') ? $instance['exclude'] : 0;
if($instance['excludeposts'] == 'current')
$excludeposts = $post->ID;
if(!isset($excludeposts))
$excludeposts = ($instance['excludeposts'] != '') ? $instance['excludeposts'] : 0;
$offset = (is_numeric($instance['offset'])) ? $instance['offset'] : 0;
// CAT ID
$category_id = $instance['categoryid'];
$dateformat = ($instance['dateformat']) ? $instance['dateformat'] : get_option('date_format');
$showdate = ($instance['show_date'] == 'on') ? 'yes' : 'no';
$pagination = ($instance['pagination'] == 'on') ? 'yes' : 'no';
$showmodifieddate = ($instance['show_modified_date'] == 'on') ? 'yes' : 'no';
$showexcerpt = ($instance['show_excerpt'] == 'on') ? 'yes' : 'no';
$excerptsize = (empty($instance['excerpt_size']) ? 55 : $instance['excerpt_size']);
$showauthor = ($instance['show_author'] == 'on') ? 'yes' : 'no';
$showcatlink = ($instance['show_catlink'] == 'on') ? 'yes' : 'no';
$thumbnail = ($instance['thumbnail'] == 'on') ? 'yes' : 'no';
$thumbnail_size = ($instance['thumbnail_size']) ? $instance['thumbnail_size'] : 'thumbnail';
$morelink = empty($instance['morelink']) ? ' ' : $instance['morelink'];
if ( empty( $instance['tags_as_class'] ) ) {
$instance['tags_as_class'] = 'no';
}
$tags_as_class = ($instance['tags_as_class'] == 'yes') ? 'yes' : 'no';
$template = empty($instance['template']) ? 'default' : $instance['template'];
if (is_single() and $excludeposts=="") $excludeposts = $post->ID;
// the_ID() - Displays the ID of the current post. Used inside the WordPress Loop. // the_ID() - Выводит ID текущего поста. Используется внутри Цикла WordPress.
// If you need to get the ID, and not display it on the screen, then use the get_the_ID() function or this code: $post->ID // Если нужно получить ID, а не выводить его на экран то используйте функцию get_the_ID() или такой код: $post->ID
$atts = array(
'id' => $category_id,
'orderby' => $orderby,
'order' => $order,
'numberposts' => $limit,
'date' => $showdate,
'date_modified' => $showmodifieddate,
'author' => $showauthor,
'dateformat' => $dateformat,
'template' => 'default',
'excerpt' => $showexcerpt,
'excerpt_size' => $excerptsize,
'exclude' => $exclude,
'excludeposts' => $excludeposts,
'offset' => $offset,
'catlink' => $showcatlink,
'thumbnail' => $thumbnail,
'thumbnail_size' => $thumbnail_size,
'morelink' => $morelink,
'tags_as_class' => $tags_as_class,
'template' => $template,
'pagination_next' => '>>',
'pagination_prev' => '<<',
'pagination' => $pagination,
'instance' => $this->id
);
$atts = array_merge(ListCategoryPosts::default_params(), $atts);
//////////////////////////////////////////
$cat_echo="";
$category_id_start = $category_id;
// SELECTED CATEGORY
// category_id=1
// exclude=1
// current
// category_id=-1
// exclude=0
// if there are several digits in exclude - shows only the title of the widget // если несколько цифр в exclude - показывает только заголовок виджета
// echo "<H4>category_id=$category_id<br>exclude=$exclude</H4>";
$exclude_arr = explode(" ", $exclude);
// echo "<HR>";
$ew=1;
//-------------------------------------------
if (is_archive() and $category_id_start>0 ) { // CATEG - NOT CURRENT WIDGET
// echo "<p>is_archive() and category_id_start>0 SELECTED CATEG</p>";
// SO THE POSTS PANEL OF THE SELECTED CATEGORY IS NOT DISPLAYED WITHIN THE CURRENT // ЧТОБЫ ПАНЕЛЬ ПОСТОВ ИЗБРАННОЙ КАТЕГОРИИ НЕ ОТОБРАЖАЛАСЬ ПАРАЛЬЕЬНО С ТЕКУЩЕЙ
if (in_array($category_id_start, $exclude_arr))
// $ew=1;
//else
$ew="";
// $category = get_queried_object()->term_id; // DOES NOT WORK! // НЕ РАБОТАЕТ!
// echo "<p>category=". implode("|", $category) ."</p>";
$curr_cat_id = get_queried_object()->term_id; // esc_html( $category->term_id );
// echo "<p>curr_cat_id=". $curr_cat_id ."</p>";
// if (in_array($curr_cat_id, $exclude_arr)) echo "<p>in_array(curr_cat_id, exclude_arr)</p>";
// if ($curr_cat_id==$category_id) echo "<p>curr_cat_id==category_id</p>";
if (in_array($curr_cat_id, $exclude_arr) or ($curr_cat_id==$category_id) ) $ew="";
// $cat_echo = '<a href="'. get_category_link( $category->term_id ) .'" rel="category tag" class="a_current_category">' . esc_html( $category->name ) .'</a>';
$cat_echo = '<a href="'. get_category_link( $category_id_start ) .'" rel="category tag" class="a_current_category">' . get_cat_name( $category_id_start ) .'</a>';
}
//-------------------------------------------
elseif (is_single()) { // POST - CURRENT WIDGET
// echo "<p>is_single()</p>";
if ($category_id_start<0) { // CURRENT WIDGET
// echo "<H4>CURRENT</H4>";
// $cat = get_the_category($post->ID);
// $cat->term_id // ID категории
// $cat->name // Название категории
// $cat->slug // Ярлык категории
$categories = get_the_category($post->ID);
$cat_names=array();
$cat_ids=array();
//$ew=1;
foreach ( $categories as $category ) {
$curr_cat_id = esc_html( $category->term_id );
//$cat_ids[] = esc_html( $category->term_id );
if (in_array($curr_cat_id, $exclude_arr)) $ew=""; // категория в списке запрещённых
$cat_ids[] = $curr_cat_id;
// $cat_names[] = esc_html( $category->name );
$cat_names[] = '<a href="'. get_category_link( $category->term_id ) .'" rel="category tag" class="a_current_category">' . esc_html( $category->name ) .'</a>';
}
// $cat_ids - ALL category ids // $cat_ids - ВСЕ id категорий
$cat_echo = implode("<br>", $cat_names);
// $cat_echo = implode("<br>", $cat_names);
// echo "<p>cat_ids=". implode("|", $cat_ids) ."</p>";
} else { // SELECTED CATEG
// echo "<p>SELECTED CATEG</p>";
//$ew=1;
if (in_array($category_id_start, $exclude_arr))
$ew="";
$cat_echo = '<a href="'. get_category_link( $category_id_start ) .'" rel="category tag" class="a_current_category">' . get_cat_name( $category_id_start ) .'</a>';
}
}
//-------------------------------------------
else {
// echo "<p>!is_single() and !(is_archive() and $category_id_start>0 )</p>";
//$ew=1;
if (in_array($category_id_start, $exclude_arr))
$ew="";
$cat_echo = '<a href="'. get_category_link( $category_id_start ) .'" rel="category tag" class="a_current_category">' . get_cat_name( $category_id_start ) .'</a>';
}
//-------------------------------------------
// https://wp-kama.ru/function/get_the_category
// Вывод ссылок на рубрики поста
/*
foreach ( get_the_category() as $category ) {
printf(
'<a href="%s" class="link link_text">%s</a>', // Link output template // Шаблон вывода ссылки
esc_url( get_category_link( $category ) ), // Link to rubric // Ссылка на рубрику
esc_html( $category->name ) // Category name // Название рубрики
);
}
*/
// echo "<H4>ew=$ew</H4>";
if($ew==1) {
echo $before_widget;
// echo " <Hr> ";
if ($pagination === 'yes') lcp_pagination_css();
// To make the widget title replacement work with "Current category" we need to
// run the displayer here to determine the current cat id.
// Otherwise the id remains set to "-1".
$catlist_displayer = new CatListDisplayer($atts);
$lcp_display = $catlist_displayer->display();
// Fetch the category id from the Catlist instance.
$category_id = $catlist_displayer->catlist->get_category_id();
if ((is_null($category_id) || [0] === $category_id ) &&
($title == 'catlink' || $title == 'catname')) {
$title = '';
// echo "<p>is_null($category_id</p>";
} elseif ($title == 'catlink') {
// If the user has setup 'catlink' as the title, replace it with
// the category link:
$lcp_category = get_category($category_id);
$title = '<a href="' . get_category_link($lcp_category->cat_ID) . '">' .
$lcp_category->name . '</a>';
// echo "<p>title == 'catlink'</p>";
} elseif ($title == 'catname') {
// If the user has setup 'catname' as the title, replace it with
// the category link:
$lcp_category = get_category($category_id);
// $title = $lcp_category->name;
$title = '<a href="' . get_category_link($lcp_category->cat_ID) . '">' .
$lcp_category->name . '</a>';
// echo "<p>title == 'catname'</p>";
} else {
//====================
//if ($cat_echo<>"") {
// if ($category_id_start<0) {
$title = $cat_echo;
// echo "<H4>category_id_start<0</H4>";
//}
//====================
}
echo $before_title . $title . $after_title;
// echo "<H4>================</H4>";
echo $lcp_display;
echo $after_widget;
}