WordPress.org

Forums

Facetious
[resolved] Only show top level categories? (9 posts)

  1. Blake Imeson
    Member
    Posted 2 years ago #

    Is it possible to get this to display only top-level categories in the dropdown?

    Also how would I change the text displayed as "All Categories" on the dropdown?

    Any help is way appreciated!

    http://wordpress.org/extend/plugins/facetious/

  2. Blake Imeson
    Member
    Posted 2 years ago #

    Ok, I used this and was able to change the text in the dropdown. However, I still need to figure out how to exclude sub categories from appearing...

    <?php
    	do_action( 'facetious', array(
        'submit' => 'Search',
        'fields' => array(
            's',
            'category' => array(
    		'label' => 'Filter:',
    		'all'   => 'Search All Sites'
    		)
    	)
    ) );?>

    I would have thought something like
    'depth'=>1

    would work...

  3. John Blackbourn
    WordPress Core Developer
    Plugin Author

    Posted 2 years ago #

    Hi Blake,

    Facetious doesn't pass any extra parameters on to the function that fetches the terms (get_terms()) so an argument like depth=>1 won't do anything. It's an interesting point though and sounds like something that Facetious should definitely support. I'll look at adding it into a future version.

    For now, you can use the 'options' argument and pass your own list of categories:

    <?php
    $category_options = array();
    
    foreach ( get_terms( 'category', array( 'depth' => 1 ) ) as $category )
    	$category_options[$category->slug] = $category->name;
    
    do_action( 'facetious', array(
        'submit' => 'Search',
        'fields' => array(
            's',
            'category' => array(
    		'label'   => 'Filter:',
    		'all'     => 'Search All Sites',
    		'options' => $category_options
    		)
    	)
    ) );?>

    Hope this helps.

    John

  4. Blake Imeson
    Member
    Posted 2 years ago #

    Hi John,

    Thanks so much for your response. That may be a good short term workaround. I am a little confused, would I need to change part of that to my situation?

    This is definitely something I am willing to fund development of. If you are interested, let me know. If not, I have a PHP developer I can hire to give it a go.

    Thanks!
    Blake

  5. John Blackbourn
    WordPress Core Developer
    Plugin Author

    Posted 2 years ago #

    Hi Blake,

    The 'options' parameter in Facetious was designed for this situation, where you need to customise the list of options displayed. It's not just a short term workaround.

    It looks like the argument you want for get_terms() is 'parent=>0', not 'depth=>1'. So the code below should work for you as-is:

    <?php
    $category_options = array();
    
    foreach ( get_terms( 'category', array( 'parent'=> 0) ) as $category )
    	$category_options[$category->slug] = $category->name;
    
    do_action( 'facetious', array(
        'submit' => 'Search',
        'fields' => array(
            's',
            'category' => array(
    		'label'   => 'Filter:',
    		'all'     => 'Search All Sites',
    		'options' => $category_options
    		)
    	)
    ) );?>
  6. Blake Imeson
    Member
    Posted 2 years ago #

    Thanks John, that worked beautifully.

    This is a really great plugin, thanks for making it available to the community!

  7. Blake Imeson
    Member
    Posted 1 year ago #

    Hi John,

    Is this a filter to modify the normal Facetious sidebar widget? If not, is there a way?

    Thanks,
    Blake

  8. John Blackbourn
    WordPress Core Developer
    Plugin Author

    Posted 1 year ago #

    There isn't a filter on the Facetious widget, no. What are you trying to alter? We might look at adding in some filters if necessary.

  9. Blake Imeson
    Member
    Posted 1 year ago #

    We ended up just adding this to our functions file to create a new widget that had the different settings than the other normal one.

    class FacetiousExtend extends WP_Widget {
    
    	function FacetiousExtent() {
    		// Instantiate the parent object
    		parent::__construct( true, 'SearchSites' );
    	}
    
    	function widget( $args, $instance ) {
    		echo '<div id="facetious_widget-custom" class="widget widget_categories"><h3>Search</h3>';
    		$category_options = array();
    
    	foreach ( get_terms( 'category', array( 'parent'=> 0) ) as $category )
    		$category_options[$category->slug] = $category->name;
    
    	do_action( 'facetious', array(
    		'submit' => 'Search',
    		'fields' => array(
    			's',
    			'category' => array(
    			'label'   => ' ',
    			'all'     => 'Search All Blogs',
    			'options' => $category_options
    			)
    		)
    		)
    	);
    	echo '</div>';
    	}
    
    	function update( $new_instance, $old_instance ) {
    		// Save widget options
    	}
    
    	function form( $instance ) {
    		// Output admin widget options form
    	}
    }
    
    function modify_facetious_search() {
    	register_widget( 'FacetiousExtend' );
    }
    
    add_action( 'widgets_init', 'modify_facetious_search' );

    It isn't perfect and the widget doesn't show the title on the backend but it worked.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Facetious
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic

Tags

No tags yet.