自定义帖子类型+分类术语年度/月度归档

时间:2014-04-21 22:40:29

标签: sql wordpress archive custom-post-type

-

我一直在研究如何为具有分类和各种条款的自定义帖子创建日期存档...

我从walltea获得了这个Stackoverflow answer取得了巨大的成功,但我需要一些帮助来修改它,同时考虑到分类法及其术语......

这可能吗?

- 编辑 -

好的,我已经设法在LOADS的时间之后做了这个,如果你知道如果有更好的解决方案那么请分享,因为我找到的方式有点麻烦:(再次原创代码curtosy of walltea的Stackoverflow answer

1 在您的functions.php中放置以下代码:(更改“$ cptPart”的自定义帖子类型)

/**
* Custom post type date archives
*/

/**
* Custom post type specific rewrite rules
* @return wp_rewrite             Rewrite rules handled by Wordpress
*/

add_action('generate_rewrite_rules', 'cpt_rewrite_rules');

/**
* Generate date archive rewrite rules for a given custom post type
* @param  string $cpt        slug of the custom post type
* @return rules              returns a set of rewrite rules for Wordpress to handle
*/
function cpt_rewrite_rules($wp_rewrite) {
$rules = array();

$cptPart = "(news_events|live_reports|book_review|readers_digest|publications|jobs)";
$taxonomyPart = "([a-zA-Z_-]+)";
$termPart = "([a-zA-Z_-]+)";
$patterns = array(
array(
'rule' => "$cptPart/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/$taxonomyPart/$termPart",
'vars' => array('post_type', 'year', 'monthnum', 'day', 'taxonomy', 'term')
),
array(
'rule' => "$cptPart/([0-9]{4})/([0-9]{1,2})/$taxonomyPart/$termPart",
'vars' => array('post_type', 'year', 'monthnum', 'taxonomy', 'term')
),
array(
'rule' => "$cptPart/([0-9]{4})/$taxonomyPart/$termPart",
'vars' => array('post_type', 'year', 'taxonomy', 'term')
),
array(
'rule' => "$cptPart/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/$taxonomyPart",
'vars' => array('post_type', 'year', 'monthnum', 'day', 'taxonomy')
),
array(
'rule' => "$cptPart/([0-9]{4})/([0-9]{1,2})/$taxonomyPart",
'vars' => array('post_type', 'year', 'monthnum', 'taxonomy')
),
array(
'rule' => "$cptPart/([0-9]{4})/$taxonomyPart",
'vars' => array('post_type', 'year', 'taxonomy')
),
array(
'rule' => "$cptPart/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})",
'vars' => array('post_type', 'year', 'monthnum', 'day')
),
array(
'rule' => "$cptPart/([0-9]{4})/([0-9]{1,2})",
'vars' => array('post_type', 'year', 'monthnum')
),
array(
'rule' => "$cptPart/([0-9]{4})",
'vars' => array('post_type', 'year')
),
);

foreach ($patterns as $pattern) {
$i = 1;
$params = array();
foreach ($pattern['vars'] as $var) {
$params[] = $var . '=' . $wp_rewrite->preg_index($i);
$i++;
}
$query = 'index.php?' . implode('&', $params);

$rules[$pattern['rule']."/?$"] = $query;
$rules[$pattern['rule']."/feed/(feed|rdf|rss|rss2|atom)/?$"] = $query."&feed=".$wp_rewrite->preg_index($i);
$rules[$pattern['rule']."/(feed|rdf|rss|rss2|atom)/?$"] = $query."&feed=".$wp_rewrite->preg_index($i);
$rules[$pattern['rule']."/page/([0-9]{1,})/?$"] = $query."&paged=".$wp_rewrite->preg_index($i);
}

$wp_rewrite->rules = $rules + $wp_rewrite->rules;
return $wp_rewrite;
}

/**
* Get a montlhy archive list for a custom post type
* @param  string  $cpt  Slug of the custom post type
* @param  boolean $echo Whether to echo the output
* @return array         Return the output as an array to be parsed on the template level
*/
function get_cpt_archives($cpt, $echo = false, $taxomony = null, $term = null)
{
global $wpdb;

$conditions = "";
if ($taxomony) {
$conditions .= " AND wtt.taxonomy LIKE %s";
if ($term) {
$conditions .= " AND wt.slug LIKE %s";
}
}
$sql = $wpdb->prepare("
SELECT * 
FROM wp_posts p
LEFT JOIN wp_term_relationships wtr ON wtr.object_id = p.ID
LEFT JOIN wp_term_taxonomy wtt ON wtr.term_taxonomy_id = wtt.term_taxonomy_id
LEFT JOIN wp_terms wt ON wtt.term_id = wt.term_id
WHERE post_type = %s
AND post_status = 'publish'
$conditions
GROUP BY YEAR(p.post_date),
MONTH(p.post_date)
ORDER BY p.post_date DESC
", $cpt, $taxomony, $term);

$results = $wpdb->get_results($sql);

if ( $results )
{
$archive = array();
foreach ($results as $r)
{
$year = date('Y', strtotime( $r->post_date ) );
$month = date('F', strtotime( $r->post_date ) );
$month_num = date('m', strtotime( $r->post_date ) );
$link = get_bloginfo('siteurl') . '/' . $cpt . '/' . $year . '/' . $month_num;
if ($taxomony) {
$link .= "/" . $r->taxonomy;
if ($term) {
$link .= "/" . $r->slug;
}
}
$this_archive = array( 'month' => $month, 'year' => $year, 'link' => $link );
array_push( $archive, $this_archive );
}

if( !$echo )
return $archive;
foreach( $archive as $a )
{
echo '<li><a href="' . $a['link'] . '">' . $a['month'] . ' ' . $a['year'] . '</a></li>';
}
}
return false;
}

-

2。在您的自定义存档文件中(例如我的一个是archive-publications.php)将此代码放在循环上方:

    <?php 
function lr_posts_join($join) {
global $taxonomy_global_join;
global $term_global_join;
if ($taxonomy_global_join) $join .= " $taxonomy_global_join";
if ($term_global_join) $join .= " $term_global_join";
return $join;
}
function lr_posts_where($where) {
global $taxonomy_global_where;
global $term_global_where;
if ($taxonomy_global_where) $where .= " $taxonomy_global_where";
if ($term_global_where) $where .= " $term_global_where";
return $where;
}
add_filter('posts_join', 'lr_posts_join');
add_filter('posts_where', 'lr_posts_where');

$extraArgs = "";
if (get_query_var('taxonomy')) {
$taxonomy_global_join = "
LEFT JOIN $wpdb->term_relationships wtr ON wtr.object_id = $wpdb->posts.ID
LEFT JOIN $wpdb->term_taxonomy wtt ON wtr.term_taxonomy_id = wtt.term_taxonomy_id ";
$taxonomy_global_where = " AND wtt.taxonomy LIKE '" . esc_sql(like_escape(get_query_var('taxonomy'))) . "'";
$extraArgs .= "&taxonomy=" . get_query_var('taxonomy');

if (get_query_var('term')) {
$term_global_join = "
LEFT JOIN $wpdb->terms wt ON wtt.term_id = wt.term_id ";
$term_global_where = " AND wt.slug LIKE '" . esc_sql(like_escape(get_query_var('term'))) . "'";
$extraArgs .= "&term=" . get_query_var('term');
}
}

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$posts = query_posts("post_type=publications&paged=$paged$extraArgs");
$taxonomy_global_join = $taxonomy_global_where = '';
$term_global_join = $term_global_where = '';

while (have_posts()) : the_post(); ?>

    -- loop code here --

<?php endwhile; ?>

3。上述代码让我们的存档页面了解我们对分类法和术语的过滤。保存文件并在管理区域中重新保存永久链接。接下来让我们用这只小狗!我在自定义侧边栏中使用了这个(sidebar-publications.php):

<ul>
    <?php echo get_cpt_archives( 'publications', true, 'publication_category', 'articles' ) ?>
</ul>

我们的函数获取自定义帖子(出版物)并设置'true',因此我们需要正常的列表布局。接下来它调用我们想要的分类法...我只有一个用于这个自定义帖子(publication_category),最后是我想要的那个分类法(文章)。这应该吐出这样的链接:www.website/custom_post/year/month/taxonomy/term

我希望这段代码能为您提供良好的服务!我的客户非常高兴;)

非常好, 克里斯

0 个答案:

没有答案