Forum Replies Created

Viewing 9 replies - 1 through 9 (of 9 total)
  • johnandrews

    (@johnandrews)

    @rythmdoctor, I also needed this functionality. I had already written a function to remove unwanted categories from any get_the_category result, so I was able to add just one line to Yoast’s plugin.

    Put the following code in your functions.php. Change 30,31,32 to the ID(s) you wish to exclude.

    function get_the_category_exclusions($categories) {
    	$args = array(
    		'categories' => $categories,
    		'exclude'    => '30,31,32'
    	);
    	return exclude_categories($args);
    }
    
    function exclude_categories($args) {
    	$count = 0;
    	foreach ($args['categories'] as $category) {
    		if(strpos($args['exclude'],$category->cat_ID)){
    			unset($args['categories'][$count]);
    		}
    		$count++;
    	}
    	$categories = $args['categories'];
    	return array_values($categories);
    }

    Then on yoast-breadcrumbs.php add the following after $cats = get_the_category(); (approx line:232).

    $cats = get_the_category_exclusions($cats);

    This solution is tested and working for WP v2.8.4 Plugin v0.8.4

    johnandrews

    (@johnandrews)

    I tested category-fields mentioned above without much success in 2.8.4.

    Perhaps I’m missing something, but what’s wrong with Eric Le Bail’s Category Meta Plugin. It’s rather barebones, but is exactly what I needed for assigning additional attributes to categories.

    I will say while Category Meta works great as is, but I’m really inspired to to extend it with functionality found in the popular more-fields plugin.

    @mitchell you called it with the culprit being the rewrite filters… I changed more-fields-object.php lines 770:778 to the following and it seems to be working now.

    $slug = substr($field['slug'], 1, strlen($field['slug']));
    if (!$slug) continue;
    $key = $field['key'];
    
    $new_rules = array(
    	'$slug/(.+)' => 'index.php?mf_key=$key&mf_value=' .
    	$wp_rewrite->preg_index(1)
    );
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;

    Agreed @marko_j. I’m working on a custom magazine WP instance at the moment and this really seemed more of a flaw then a feature (at least for our use). I’ve yet to check out 2.8.5 but don’t assume this would have been addressed as there are better fish to fry.

    Answer to your first question. Insert this before echo $content.

    $content = rtrim($content,',');

    As for your second question. I’m sure there is 1000 different ways to do the same thing. If this works for you, then go with it.

    …and yet another approach.

    If you’d like to exclude categories from a returned categories array, try the following. This example uses get_the_category, and provides the advantage of selective exclusion, rather than applying a global filter to the get_the_category function.

    Insert this function into your functions.php:

    function exclude_categories($args) {
    	$count = 0;
    	foreach ($args['categories'] as $category) {
    		if(strpos($args['exclude'],$category->cat_ID)){
    			unset($args['categories'][$count]);
    		}
    		$count++;
    	}
    	$categories = $args['categories'];
    	return array_values($categories);
    }

    Get the categories for a given post id:

    $categories = get_the_category($post->ID);

    Call the exclude function using the cat id’s for the exclude argument:

    $args = array(
    	'categories' => $categories,
    	'exclude'    => '30,31,32'
    );
    $categories = exclude_categories($args);

    The result will be the same array as get_the_category provides, with the excludes removed of course. I suspect there are other WP category functions that this will work for as well, but I’ve yet to test.

    Here’s just one example. In your sidebar.php define the post id as a global variable and call a custom function from functions.php

    sidebar.php

    // define globals
    global $constant_post_id;
    $constant_post_id = $post->ID;
    
    // get post tags
    get_content(sidebar_post_tags);

    functions.php

    function get_content($element) {
    	do_action($element);
    };
    function get_sidebar_post_tags() {
    	global $constant_post_id;
    	$content .= "<h2>Tags<h2><ul>";
    	$tags = wp_get_object_terms($constant_post_id, 'post_tag');
    	foreach ($tags as $tag) {
    		$content .= "<li><a href=\"".get_tag_link($tag->term_id)."\">$tag->name</a></li>";
    	};
    	$content .= "</ul>";
    	echo $content;
    }
    add_action('sidebar_post_tags','get_sidebar_post_tags');

    While I don’t recommend hacking core, this is a quick fix for what you need. Mentioned line numbers are specific to WP 2.8.4

    wp-admin/includes/template.php lines 522:532

    foreach( $keys as $k ) {
    	if ( in_array( $categories[$k]->term_id, $args['selected_cats'] ) ) {
    		$checked_categories[] = $categories[$k];
    		// comment out this --> unset( $categories[$k] );
    	}
    }
    
    // Put checked cats on top
    // comment out this --> echo call_user_func_array(array(&$walker, 'walk'), array($checked_categories, 0, $args));
    // Then the rest of them
    echo call_user_func_array(array(&$walker, 'walk'), array($categories, 0, $args));

    Hope that helps.

    Great code input everyone, it’s exactly what I needed for a WP I’m working on. I took it a bit further, perhaps someone who hasn’t implemented will find this useful.

    In your theme’s functions.php insert the following function:

    function get_category_tags($args) {
    	global $wpdb;
    	$tags = $wpdb->get_results
    	("
    		SELECT DISTINCT terms2.term_id as tag_id, terms2.name as tag_name, null as tag_link
    		FROM
    			wp_posts as p1
    			LEFT JOIN wp_term_relationships as r1 ON p1.ID = r1.object_ID
    			LEFT JOIN wp_term_taxonomy as t1 ON r1.term_taxonomy_id = t1.term_taxonomy_id
    			LEFT JOIN wp_terms as terms1 ON t1.term_id = terms1.term_id,
    
    			wp_posts as p2
    			LEFT JOIN wp_term_relationships as r2 ON p2.ID = r2.object_ID
    			LEFT JOIN wp_term_taxonomy as t2 ON r2.term_taxonomy_id = t2.term_taxonomy_id
    			LEFT JOIN wp_terms as terms2 ON t2.term_id = terms2.term_id
    		WHERE
    			t1.taxonomy = 'category' AND p1.post_status = 'publish' AND terms1.term_id IN (".$args['categories'].") AND
    			t2.taxonomy = 'post_tag' AND p2.post_status = 'publish'
    			AND p1.ID = p2.ID
    		ORDER by tag_name
    	");
    	$count = 0;
    	foreach ($tags as $tag) {
    		$tags[$count]->tag_link = get_tag_link($tag->tag_id);
    		$count++;
    	}
    	return $tags;
    }

    In your theme document call the function as follows. Notice it accepts multiple category id’s:

    $args = array(
    	'categories'				=> '12,13,14'
    );
    $tags = get_category_tags($args);

    This will return an array that you could do the following with:

    $content .= "<ul>";
    foreach ($tags as $tag) {
    	$content .= "<li><a href=\"$tag->tag_link\">$tag->tag_name</a></li>";
    }
    $content .= "</ul>";
    echo $content;
Viewing 9 replies - 1 through 9 (of 9 total)