WordPress.org

Ready to get started?Download WordPress

Forums

Post Category Index Generator
Smart Index Generator Widget (4 posts)

  1. peter1000
    Member
    Posted 1 year ago #

    Did you ever consider to use "Post Category Index Generator" in combination with the "page in widget" plugin?

    If you make the Index Generator "listen" to tags in the current post, you can generate a "Smart Index Generator" of your posts (in a widget beside). Just change the code of the following function in "index.php".

    [smart_index = true]

    (Please improve! I'm not a programmer!)

    function pcig_get_category_posts($category_id,$params,$only_direct_descents=false){
    	$sub_categories = get_term_children($category_id,'category');
    	$subcategory_ids_to_exclude = array();
    	$exclude = "";
    	$extra_param="";
    
    	if($params["orderby"]){
    		$extra_param.="&orderby=".$params["orderby"];
    	}
    	if($params["order"]){
    		$extra_param.="&order=".$params["order"];
    	}
    
    	if((is_array($sub_categories)) && $only_direct_descents){
    		foreach($sub_categories as $sub_category){
    				$subcategory_ids_to_exclude[]=-$subcategory->term_id;
    		}
    		$exclude = implode(",",$subcategory_ids_to_exclude);
    		if(!empty($exclude)){
    		  $results = get_posts("cat=".$category_id.",".$exclude."&posts_per_page=999".$extra_param);
    		}
    	}
    
    	// Smart Index
    	// one of the tags of the current post must be included in the posts tags of the index
    
    	if($params["smart_index"]){
    		global $posts_incl;
    		global $post;
    		// Get current post tags
    		$tag_posts_in_ids = array();
    		$tag_ids = array();
    		$tag_ids = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) );
    		if ($tag_ids) {
    			foreach($tag_ids as $tag_id){
    				// arguments for query_posts : http://codex.wordpress.org/Function_Reference/query_posts
    				$args = array(
    						'tag__in' => array($tag_id),
    						'showposts' => 1000, // these are the number of related posts we want to display
    						'ignore_sticky_posts' => 1 // to exclude the sticky post
    						);
    				// WP_Query takes the same arguments as query_posts
    				$related_query = new WP_Query($args);
    				if ($related_query->have_posts()) {
    
    						while ($related_query->have_posts()) : $related_query->the_post();
    						if (in_category($category_id, get_the_ID())) {
    							$tag_posts_in_ids[] = get_the_ID();
    							$posts_incl = "posts_include"; // hide index if there are no posts
    						}
    						endwhile;
    				}
    			}// end foreach
    		}
    		$include = implode(",",$tag_posts_in_ids);
    		$results = get_posts("include=".$include.",posts_per_page=999".$extra_param);
    	// ende 
    
    		} else {
    
    			$results = query_posts("cat=".$category_id."&posts_per_page=999".$extra_param);
    		}
    
    	wp_reset_query();
    
    	return $results;
    }

    http://wordpress.org/extend/plugins/post-category-index-generator/

  2. Marco Constancio
    Member
    Plugin Author

    Posted 1 year ago #

    First thank for that input. It is really good to see someone to use and trying to contribute to this plugin.

    As for your code, after thinking on how to best implement it, I came to the conclusion that there is no need to overcomplicate this ideia. Instead of adding code to make several queries to auto detect tags, I added 2 extra parameters (tags_or and tags_and) that allows a user too choose the tags that he wants to filter.

    I believe that this is the best aproach but if you don't think so, feel free to add arguments how to improve and I will check it.

  3. peter1000
    Member
    Posted 1 year ago #

    Thank You Marco - this is a great idea! Maybe you can include the "page in widget" plugin in your "index generator" and add a widget to the plugin. I think this would be a great combination.

    It was not easy for me to hide the widget, when there is no post with the same tag. Therefor I hade to change the "page to widget" plugin a little bit...

  4. Marco Constancio
    Member
    Plugin Author

    Posted 1 year ago #

    While the both plugin might work great, page in widget is already a plugin on its own so I don't think it is necessary to duplicate that functionality.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic