Вывод количества новостей в категории

Вывод количества новостей в категории

Хак позволит, в любом месте шаблона, вывести количество новостей опубликованных в категории. Это простая адаптация функционала, взятая из актуальной версии DLE и перенесенная на более старые версии движка. Напомню, что этот функционал уже присутствует по умолчанию в DLE 13.0 и выше.


Установка:
1. Открыть engine/init.php и найти:
$banned_info = get_vars ( "banned" );

Добавить выше:
$news_count_in_array = dle_cache ( "news", "newscountcacheincats" );
if ($news_count_in_array) {
	$news_count_in_array = json_decode($news_count_in_array, true);
	if ( !is_array($news_count_in_array) ) $news_count_in_array = array();
} else {
	$news_count_in_array = array();
	if( $config['no_date'] AND !$config['news_future'] ) {
		$thisdate = date( "Y-m-d H:i:s", $_TIME );
		$where_date = " AND date < '" . $thisdate . "'";
	} else $where_date = "";

	$db->query( "SELECT category, COUNT(*) AS count FROM " . PREFIX . "_post WHERE approve=1" . $where_date . " GROUP BY category" );
	while ( $row = $db->get_row() ) {
		if(!$row['category']) continue;
		$cat_array = $temp_cat_array = explode(",", $row['category']);
		foreach ( $temp_cat_array as $value ) {
			if(!isset($news_count_in_array[$value])) $news_count_in_array[$value] = $row['count'];
			else $news_count_in_array[$value] = $news_count_in_array[$value] + $row['count'];
			if( $config['show_sub_cats']) {
				$temp_parent = $cat_info[$value]['parentid'];
				while ( $temp_parent ) {
					if( !in_array($temp_parent, $cat_array) ) {
						if(!isset($news_count_in_array[$temp_parent])) $news_count_in_array[$temp_parent] = $row['count'];
						else $news_count_in_array[$temp_parent] = $news_count_in_array[$temp_parent] + $row['count'];
						$cat_array[] = $temp_parent;
					}
					$temp_parent = $cat_info[$temp_parent]['parentid'];
				}
			}
		}
	}
	create_cache("news", json_encode($news_count_in_array), "newscountcacheincats");
	unset($temp_parent, $temp_cat_array, $cat_array);
}

foreach ($cat_info as $key => $value) {
	$cat_info[$key]['newscount'] = (int)$news_count_in_array[$key];
}
unset($news_count_in_array);

2. Открыть engine/classes/template.class.php и найти:
$this->template = file_get_contents( $this->dir . "/" . $tpl_name );

Добавить ниже:
		if (strpos ( $this->template, "{catnewscount" ) !== false) {
			$this->template = preg_replace_callback ( "#\\{catnewscount id=['\"](.+?)['\"]\\}#i", array( &$this, 'catnewscount'), $this->template );
		}

Найти еще:
$template = file_get_contents( $templatefile );

Добавить ниже:
		if (strpos ( $template, "{catnewscount" ) !== false) {
			$template = preg_replace_callback ( "#\\{catnewscount id=['\"](.+?)['\"]\\}#i", array( &$this, 'catnewscount'), $template );
		}

Найти еще:
	function compile($tpl) {

Добавить выше:
	function catnewscount($matches = array()) {
		global $cat_info;
		$id = (int)$matches[1];
		return (int)$cat_info[$id]['newscount'];
	}

Готово!

Теперь, для вывода количества, можно использовать тег:
{catnewscount id="id_категории"}
 
Версия DLE: 9.х-11.1
Автор: Sander
Источник
Информация
Посетители, находящиеся в группе Гость, не могут оставлять комментарии к данной публикации.