• Resolved dorjeduck

    (@dorjeduck)


    Hi,

    thanks for the excellent plugin, it just does what it is supposed to do. πŸ™‚

    I am just looking at the source code and try to extend the plugin so I can limit the random post to a dynamically set category. I saw

    function rpl_change_ptype( $args ) {
      $args['category_name'] = 'your_category_name_goes_here';
    
      return $args;
    }
    add_filter( 'random_post_args', 'rpl_change_ptype' );

    What i want is to set the category dynamically within my template. Basically I want to call random post link with a category slug
    <?php random_post_link('Feeling lucky','your_category_name_goes_here'); ?>
    Should be not difficult to do I assume but I am new to wordpress development …

    Thanks buddies

    martin

    http://ww.wp.xz.cn/extend/plugins/random-post-link/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter dorjeduck

    (@dorjeduck)

    Okay here is what i came up with quickly ….

    firstly I just add the category as optional parameter to random_post_link, add it to the query string

    function random_post_link($text = 'Random Post',$catcat = '') {
    	printf('<a href="%s">%s</a>', get_random_post_url($catcat), $text);
    }
    
    function get_random_post_url($catcat) {
    	$cat = empty($catcat) ? '' : '=' . $catcat;
    	return trailingslashit(get_bloginfo('url')) . '?' . Random_Post_Link::query_var .  $cat;
    }

    and use this information

    $cat_name = $_GET[self::query_var];
    if (!empty($cat_name)) {
                            $args = array_merge($args, array(
    			'category_name' => $cat_name,
    		));
    }

    of course this could be improved on but for now it works for me …

    Thread Starter dorjeduck

    (@dorjeduck)

    haven’t looked into the cookie thing with this addition yet … be happy

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Limit to a dynamically set category’ is closed to new replies.