创建一个数组,其中包含年份存档的所有链接(自定义 post 类型)

Create an array with all the links of the years' archive (of a custom post type)

我有一个名为“论文”的自定义 post 类型,我需要创建一个数组,其中包含每年存在的所有存档链接。类似于:

array (
[0]=> 'http://www.example.com/2021/?post_type=papers'
[1]=> 'http://www.example.com/2019/?post_type=papers'// cause there's no post in 2020
[2]=> 'http://www.example.com/2017/?post_type=papers'
)

有人告诉我使用 get_archives() 但这只是给了我一个格式化的列表,而不是我可以用来提供另一个函数的数组。还是我做错了什么?

试试这个:

  $links = wp_get_archives(array('echo'=>'0','format'=>'<link>'));
  $regex = '/\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i';
  preg_match_all($regex, $links, $matches);
  $array_links = $matches[0];
  print_r($array_links);

请试试这个 -

function get_archive_post_ids(){
    if( is_archive() ){
        global $wp_query;
        $post_ids = wp_list_pluck( $wp_query->posts, 'ID' );
    }
}
add_action( 'wp_enqueue_scripts', 'get_archive_post_ids' );