• riddhitatvic

    (@riddhitatvic)


    Hello,

    I have requirement like I need to set condition to hide/show widget based on post author.

    If my blog post is of author1/author2 then widget show else everywhere hide widget.

    If any one have any idea please let me know ASAP.

    Thanks & regards,
    Riddhi.k

Viewing 2 replies - 1 through 2 (of 2 total)
  • This feature would be awesome.

    @maartenm – thoughts?

    I needed it too, and i wrote the class for it in my local. @maartenm perhaps you can include it in a next update?

    in includes/conditions i’ve adde dthe file class-wdc-post_author-condition.php

    
    <?php defined( 'ABSPATH' ) or exit; // Exit when accessed directly.
    
    /**
     * Post type condition
     */
    class WDC_Post_Author_Condition extends WDC_Condition
    {
    	/**
    	 * Constructor
    	 */
    	public function __construct()
    	{
    		parent::__construct( 'post_author', __( 'Post Author', 'wdc' ), array
    		(
    			'operators' => array( '==', '!=' ),
    			'category'  => 'post',
    			'order'     => 10,
    		));
    	}
    
    	/**
    	 * Values
    	 *
    	 * @param array $choices
    	 *
    	 * @return array
    	 */
    	public function values( $choices )
    	{
    		$authors = 	get_users();
    
    		$values = array();
    
    		foreach ( $authors as $author ) 
    		{
    			$values[ $author->ID ] = $author->user_nicename;
    		}
    
    		return $values;
    	}
    	
    	/**
    	 * Apply
    	 *
    	 * @param bool   $return
    	 * @param string $operator
    	 * @param mixed  $value
    	 *
    	 * @return bool
    	 */
    	public function apply( $return, $operator, $value )
    	{
    		global $post;
    		return wdc_do_operator( $operator, (!empty($post) && $post->post_author == $value ), true );
    	}
    }
    
    wdc_register_condition( 'WDC_Post_Author_Condition' );

    and in widget-display-conditions.php i’ve added

    	include_once WDC_ABSPATH . 'includes/conditions/class-wdc-page-condition.php';
    

    to the wdc_init() function.

    this works for me

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Can we set condition based on post author?’ is closed to new replies.