【问题标题】:Get monthly posts by year in Wordpress在 Wordpress 中按年获取每月帖子
【发布时间】:2015-04-02 15:46:28
【问题描述】:

我正在寻找 2014 年的帖子月份列表。如:

2014 - 一月 - 二月 ....

当我在那个月时,我还需要那里的“活动”类的功能。

我已经尝试了一百万个插件,但没有运气。

【问题讨论】:

  • 小部件档案不是您要找的吗?你有它的外观>小部件

标签: wordpress archive posts


【解决方案1】:
function my_custom_post_type_archive_where($where,$args){  
    $post_type  = isset($args['post_type'])  ? $args['post_type']  : 'post';  
    $where = "WHERE post_type = '$post_type' AND post_status = 'publish'";
    return $where;  
}

然后调用它:

add_filter( 'getarchives_where','my_custom_post_type_archive_where',10,2);

当您想按自定义帖子类型显示存档时,只需传递 post_type 参数:

$args = array(
    'post_type'    => 'your_post_type',
    'type'         => 'monthly',
    'echo'         => 0
);
echo '<ul>'.wp_get_archives($args).'</ul>';

【讨论】:

  • 谢谢你,那么关于functions.php 中的内容和category.php 侧边栏中的内容,我应该把它放在哪里?它适用于普通帖子,而不是自定义帖子类型 :)
  • 如果你使用post,那么你必须调用: $args = array( 'type' => 'monthly', 'echo' => 0 ); echo '
      '.wp_get_archives($args).'
    ';在 category.php 或任何你想显示的地方
【解决方案2】:

我最终使用分类法来实现这一点,我更希望它自动显示这一点,但它似乎是目前最好的折衷方案。不过,我会试试 San Jay 的选择。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-22
    相关资源
    最近更新 更多