Wordpress,重定向到特定类别的随机帖子

Wordpress,重定向到特定类别的随机帖子,wordpress,redirect,post,random,categories,Wordpress,Redirect,Post,Random,Categories,使用这段代码,我正在从所选类别检索帖子,如何将类别名称设置为字符串,当我这样做时,它不起作用 类别名称=$CategoryNameisBla 我只是用cat id而不是cat name解决了这个问题 add_action('template_redirect','random_template'); function random_template() { if (get_query_var('random') == 1) { $posts = get

使用这段代码,我正在从所选类别检索帖子,如何将类别名称设置为字符串,当我这样做时,它不起作用

类别名称=$CategoryNameisBla

我只是用cat id而不是cat name解决了这个问题

add_action('template_redirect','random_template');
function random_template() {
       if (get_query_var('random') == 1) {
               $posts = get_posts('category_name="Kategori 3"&post_type=post&orderby=rand&numberposts=1');

        foreach($posts as $post) {
                       $link = get_permalink($post);
               }
               wp_redirect($link,307);
               exit;
       }
}