Horje
exclude category frome breadcrumb function - Wordpress Solution
[Solved] - exclude category frome breadcrumb function - Wordpress Solution
Try following codes
I need to exclude a category from the_breadcrumb() function, the get_category function doesn't allow to exclude categories through arguments.


function breadcrumb_cat_path(){
				$f_s_p = '<li class="crumb2">';
$ca = get_query_var('cat');
if(!$ca){
foreach((get_the_category()) as $key => $cat) {
$ca = $cat->term_id;
}
}
//var_dump($ca);
$var = get_category_parents($ca, TRUE, '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="af8eef">[email protected]</a>#$%^&*()');
//echo $var;
//var_dump($var);
$va = explode('<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="587918">[email protected]</a>#$%^&*()', $var);
array_pop($va);
//var_dump($va);
$z_i = 3 + count($va);
$fg = 0;
foreach($va as $k => $v){
	--$z_i;
	if($fg == 0) $f_s_p .= str_replace('<a ', "<a style=\"margin-left: -10px; z-index: $z_i; padding-left: 35px; \" ", $v);
	else $f_s_p .= str_replace('<a ', "<a style=\"margin-left: -10px; z-index: $z_i; padding-left: 16px; \" ", $v);
	++$fg;
}
				/*echo '<a href="">';
				single_cat_title();
				echo '</a>';*/
				$f_s_p .= '</li>';

return $f_s_p;
}

function the_breadcrumb() {
		$i = 0;
		echo '<li class="crumb1">';
		echo '<a href="';
		echo get_option('home');
		echo '">';
		bloginfo('name');
		echo "</a>";
		echo '</li>';
	if (!is_home()) {
		if (is_category() || is_single()) {
			if(is_category()){
$f_s_p = breadcrumb_cat_path();
echo $f_s_p;
			}
			if (is_single()) {
				//f_s_p stands for the footer_single_problem which occurs on single post pages
//var_dump($f_s_p);
				if(defined('F_S_P')):
					echo F_S_P;
				else:

				static $f_s_p = '<li class="crumb2">';
				$count = count(get_the_category());
				if($count == 1){
$f_s_p = breadcrumb_cat_path();
echo $f_s_p;
				}
else{
				foreach((get_the_category()) as $key => $cat) {//var_dump($cat);
					if($key == $count - 1){
						if($key == 0)	$f_s_p .= '<a href="' . get_category_link($cat->term_id) . '">';
						else $f_s_p .= '<a href="' . get_category_link($cat->term_id) . '" class="bclesspadding">';
						$f_s_p .= $cat->cat_name;
						$f_s_p .= '</a>';
					}
					else{
						if($key == 0) $f_s_p .= '<a href="' . get_category_link($cat->term_id) . '" class="noarrow">';
						else $f_s_p .= '<a href="' . get_category_link($cat->term_id) . '" class="noarrow bclesspadding">';
						$f_s_p .= $cat->cat_name;
						$f_s_p .= '</a>';
					}
				}
				$f_s_p .= '</li>';

				echo $f_s_p;
}
				define('F_S_P', $f_s_p);

				endif;
				/*echo '<li class="crumb2">';
				the_category(' ', 'single');
				echo '</li>';*/

				/*echo '<li class="crumb3">';
				echo '<a href="">';
				the_title();
				echo '</a>';
				echo '</li>';*/
			}
		} elseif (is_page()) {
			echo '<li class="crumb2">';
			echo '<a href="">';
			echo the_title();
			echo '</a>';
			echo '</li>';
		}
	}
}
I tried replacing get_the_category with this function:

foreach((get_the_category()) as $cat) { if (!($cat->cat_ID==618)) echo '<a href="' . get_bloginfo('url') . '/category/' . $cat->category_nicename . '/">'. ' - ' . $cat->cat_name . '</a>';}
but it doesn't work in the foreach() condition

Solution - 1

You could try this (i've not tested it though):

function breadcrumb_cat_path(){
$f_s_p='';
	$ca = get_query_var('cat');
	if(!$ca){
		foreach((get_the_category()) as $key => $cat) {
		
			$ca = $cat->term_id;
		}
	}
	if ($ca!=618) {

	$f_s_p = '<li class="crumb2">';

	$var = get_category_parents($ca, TRUE, '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="537213">[email protected]</a>#$%^&*()');
	$va = explode('<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a283e2">[email protected]</a>#$%^&*()', $var);
	array_pop($va);
	$z_i = 3 + count($va);
	$fg = 0;
	foreach($va as $k => $v){
		--$z_i;
		if($fg == 0) $f_s_p .= str_replace('<a ', "<a style=\"margin-left: -10px; z-index: $z_i; padding-left: 35px; \" ", $v);
		else $f_s_p .= str_replace('<a ', "<a style=\"margin-left: -10px; z-index: $z_i; padding-left: 16px; \" ", $v);
		++$fg;
	}
	$f_s_p .= '</li>';
}
	return $f_s_p;

}


Solution - 2

foreach (get_the_category() as $cat){
	if (!($cat->cat_ID==618)) {
		echo '<a href="' . get_bloginfo('url') . '/category/' . $cat->category_nicename . '/">'. ' - ' . $cat->cat_name . '</a>';
	}
}

Date: 2022-10-21 05:22:15
Category: Wordpress
More for You