最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to get post archives urls list without any markup?

programmeradmin2浏览0评论

I'm using wp_get_archives but I cannot prepend any text inside the links. I want code like this:

<a href="archive link">[my custom prepend] 2020<a/>

But the 'before' option will prepend the passed string before the <a> tag.

Current code is the following:

$archives = wp_get_archives(array(
  'type' => 'yearly'
  ,'echo' => false
  ,'format' => 'custom'
  ,'before' => 'News'
));

So I really would like to get a list of only years and urls, without any markup, so I can build it by myself. Thus maybe without wp_get_archives().

Maybe the best thing would be have a simple array like:

array(
   array('year' => 2020, 'url' => 'https://www.....')
)

How to?

I'm using wp_get_archives but I cannot prepend any text inside the links. I want code like this:

<a href="archive link">[my custom prepend] 2020<a/>

But the 'before' option will prepend the passed string before the <a> tag.

Current code is the following:

$archives = wp_get_archives(array(
  'type' => 'yearly'
  ,'echo' => false
  ,'format' => 'custom'
  ,'before' => 'News'
));

So I really would like to get a list of only years and urls, without any markup, so I can build it by myself. Thus maybe without wp_get_archives().

Maybe the best thing would be have a simple array like:

array(
   array('year' => 2020, 'url' => 'https://www.....')
)

How to?

Share Improve this question asked May 15, 2020 at 9:59 Luca ReghellinLuca Reghellin 1,6422 gold badges21 silver badges45 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

I was going to suggest creating a regex to parse it but if you look at this comment on the Wordpress wp_get_archives() docs it looks like someone has gotten there before us.

I maneged to do like this:

First, get simply the years:

// this is a multipurpose function I made, and it's quite self explanatory
function get_years($post_type, $order = "ASC", $exclude = array(), $future = true, $taxonomy = null, $term = null){
  global $wpdb;
  $type = ($post_type) ? $post_type : 'post';
  $status = ($future) ? " ('publish', 'future') " : " ('publish') ";
  $exclude = (!empty($exclude)) ? " AND YEAR(post_date) NOT IN (" . implode(',',$exclude) . ") " : "";

  return $wpdb->get_results($wpdb->prepare("
    SELECT YEAR(post_date) AS `y`, count(ID) as posts_count
    FROM $wpdb->posts p
      INNER JOIN $wpdb->term_relationships rel ON p.ID = rel.object_id
      INNER JOIN $wpdb->term_taxonomy tt ON tt.term_taxonomy_id = rel.term_taxonomy_id
      INNER JOIN $wpdb->terms t ON tt.term_id = t.term_id
    WHERE
      p.post_type = %s
      AND tt.taxonomy = %s
      AND t.slug = %s
      AND post_status IN " . $status . "
      $exclude
    GROUP BY y
    ORDER BY post_date " . $order,
    $post_type,
    $taxonomy,
    $term
  ),OBJECT_K);
}

Then, cicle through the years and build the permalinks:

get_year_link($year); // this is wp. 

It works for posts, otherwise one could build the likes in a more 'manual' way:

home_url(user_trailingslashit('write some path here' . $year));
发布评论

评论列表(0)

  1. 暂无评论