自定义帖子类型每年/每月归档固定链接

时间:2013-01-30 作者:dclawson

我在Wordpress网站上有一个自定义的帖子类型“news”。我正在使用高级自定义字段插件为每篇文章添加元数据。

我想创建一组新闻项目作为存档:

[2013]
    [January] => 5
[2012]
    [January] => 20
    [February] => 10
[2011]
    [April] => 30
我通过以下方式实现了这一目标:

    global $wpdb;
    $news = $wpdb->get_results(
        "SELECT wp_posts.post_date, COUNT(wp_posts.ID) as count
         FROM $wpdb->posts
         WHERE
         wp_posts.post_type = \'news\' AND
         wp_posts.post_status = \'publish\' AND
         wp_posts.post_date <= CURDATE() AND
         wp_posts.post_date >= DATE_SUB(CURDATE(), INTERVAL 3 YEAR)
         GROUP BY YEAR(wp_posts.post_date), MONTH(wp_posts.post_date)
         ORDER BY wp_posts.post_date DESC", 
         ARRAY_A);

    $archive = array();
    foreach ($news as $post):
        $year = date(\'Y\', strtotime($post[\'post_date\']));      
        $month = date(\'m\', strtotime($post[\'post_date\']));     
        $month_name = date(\'F\', strtotime($post[\'post_date\']));
        $post[\'url\'] = \'NOT SURE ABOUT URL\';
        $archive[$year][$month_name] = $post;
    endforeach;
我需要能够使用http://example.com/news/2012/http://example.com/news/2012/10/.

我试着使用插件Custom Post Type Archives 但当我走到/news/2012/ 我得到的是没有应用过滤器的标准新闻列表。

这是我的自定义邮件类型代码

register_post_type(\'news\', 
  array(    
    \'label\' => \'News\',
    \'description\' => \'Latest News\',
    \'public\' => true,
    \'show_ui\' => true,
    \'show_in_menu\' => true,
    \'capability_type\' => \'post\',
    \'hierarchical\' => true,
    \'rewrite\' => array(\'slug\' => \'\',\'with_front\' => \'1\'),
    \'query_var\' => true,
    \'has_archive\' => true,
    \'menu_position\' => 120,
    \'menu_icon\' => /assets/img/icon-custom.png
  )
);

1 个回复
SO网友:Milo

下面是一个使用add_rewrite_rule 处理自定义职位类型的年和月,其中news 是鼻涕虫。访问Settings > Permalinks 添加后,将刷新管理中的重写规则。你也可以put this in a pluginflush rewrite rules on plugin activation.

function wpa83797_news_rewrite_rules(){

    add_rewrite_rule(
        \'news/([0-9]{4})/([0-9]{1,2})/?$\',
        \'index.php?post_type=news&year=$matches[1]&monthnum=$matches[2]\',
        \'top\'
    );

    add_rewrite_rule(
        \'news/([0-9]{4})/?$\',
        \'index.php?post_type=news&year=$matches[1]\',
        \'top\'
    );

}
add_action( \'init\', \'wpa83797_news_rewrite_rules\' );

结束

相关推荐

为什么$_REQUEST[‘REDIRECT_TO’]为空?

wp登录。php使用$\\u请求[\'redirect\\u to\']作为覆盖lostpassword链接重定向的方法。然而,出于某种原因,它似乎不起作用。可能是我的服务器环境,但$\\u REQUEST[\'redirect\\u to\']值未设置,即使我在URI中显式地将其作为GET变量传递。这是wordpress的事情还是我的托管环境的问题,还是我只是一个新手?我看到另一篇帖子,其中user@rarst提到WP实现了自己的逻辑行为,将请求作为GET和post的组合来执行。但我在wp登录和wp加载

自定义帖子类型每年/每月归档固定链接 - 小码农CODE - 行之有效找到问题解决它

自定义帖子类型每年/每月归档固定链接

时间:2013-01-30 作者:dclawson

我在Wordpress网站上有一个自定义的帖子类型“news”。我正在使用高级自定义字段插件为每篇文章添加元数据。

我想创建一组新闻项目作为存档:

[2013]
    [January] => 5
[2012]
    [January] => 20
    [February] => 10
[2011]
    [April] => 30
我通过以下方式实现了这一目标:

    global $wpdb;
    $news = $wpdb->get_results(
        "SELECT wp_posts.post_date, COUNT(wp_posts.ID) as count
         FROM $wpdb->posts
         WHERE
         wp_posts.post_type = \'news\' AND
         wp_posts.post_status = \'publish\' AND
         wp_posts.post_date <= CURDATE() AND
         wp_posts.post_date >= DATE_SUB(CURDATE(), INTERVAL 3 YEAR)
         GROUP BY YEAR(wp_posts.post_date), MONTH(wp_posts.post_date)
         ORDER BY wp_posts.post_date DESC", 
         ARRAY_A);

    $archive = array();
    foreach ($news as $post):
        $year = date(\'Y\', strtotime($post[\'post_date\']));      
        $month = date(\'m\', strtotime($post[\'post_date\']));     
        $month_name = date(\'F\', strtotime($post[\'post_date\']));
        $post[\'url\'] = \'NOT SURE ABOUT URL\';
        $archive[$year][$month_name] = $post;
    endforeach;
我需要能够使用http://example.com/news/2012/http://example.com/news/2012/10/.

我试着使用插件Custom Post Type Archives 但当我走到/news/2012/ 我得到的是没有应用过滤器的标准新闻列表。

这是我的自定义邮件类型代码

register_post_type(\'news\', 
  array(    
    \'label\' => \'News\',
    \'description\' => \'Latest News\',
    \'public\' => true,
    \'show_ui\' => true,
    \'show_in_menu\' => true,
    \'capability_type\' => \'post\',
    \'hierarchical\' => true,
    \'rewrite\' => array(\'slug\' => \'\',\'with_front\' => \'1\'),
    \'query_var\' => true,
    \'has_archive\' => true,
    \'menu_position\' => 120,
    \'menu_icon\' => /assets/img/icon-custom.png
  )
);

1 个回复
SO网友:Milo

下面是一个使用add_rewrite_rule 处理自定义职位类型的年和月,其中news 是鼻涕虫。访问Settings > Permalinks 添加后,将刷新管理中的重写规则。你也可以put this in a pluginflush rewrite rules on plugin activation.

function wpa83797_news_rewrite_rules(){

    add_rewrite_rule(
        \'news/([0-9]{4})/([0-9]{1,2})/?$\',
        \'index.php?post_type=news&year=$matches[1]&monthnum=$matches[2]\',
        \'top\'
    );

    add_rewrite_rule(
        \'news/([0-9]{4})/?$\',
        \'index.php?post_type=news&year=$matches[1]\',
        \'top\'
    );

}
add_action( \'init\', \'wpa83797_news_rewrite_rules\' );

相关推荐

Redirection problems

请帮助我克服重定向问题。我需要在子域上工作,而不是在主域上工作,但这里两个域是不同的。我所做的是-将所有(website.com)文件复制到另一个子域(demo.websiteA.com),并根据wp配置更改数据库名称。php文件。但每当我尝试打开演示时。网站a。com它重定向到网站。com公司请帮帮我提前感谢