WordPress.org

Make WordPress Core

Ticket #11550: category-static-pages.php

File category-static-pages.php, 1.9 KB (added by westonruter, 4 years ago)

Theme customizations to get categories mapped to static pages and posts to appear under them.

Line 
1<?php
2
3/**
4 * When viewing a single post witnin a category that is assigned to a static
5 * page, make sure that the pages get the proper class names.
6 */
7function acme_filter_list_pages_for_category_static_pages($list){
8        //Add classes for when news
9        if(is_single() && in_category(ACME_NEWS_CATEGORY_ID)){
10                $list = preg_replace('/(?=page-item-' . ACME_ABOUTUS_PAGE_ID . '\b)/', 'current_page_ancestor ', $list);
11                $list = preg_replace('/(?=page-item-' . ACME_NEWS_PAGE_ID . '\b)/', 'current_page_ancestor current_page_parent ', $list);
12        }
13        return $list;
14}
15add_filter('wp_list_pages', 'acme_filter_list_pages_for_category_static_pages');
16
17
18/**
19 * Make sure posts in a category assigned to a static page appear under it in the
20 * URI structure. Requires a permalink structure prefixed by /%category%/...
21 */
22function acme_rewrite_post_links_for_static_page_categories($permalink, $post, $leavename){
23        if(in_category(ACME_NEWS_CATEGORY_ID, $post)){
24                return preg_replace("{.+?/(?=\d\d\d\d)}", get_permalink(ACME_NEWS_PAGE_ID), $permalink);
25        }
26        return $permalink;
27}
28add_filter('post_link', 'acme_rewrite_post_links_for_static_page_categories', 10, 3);
29
30
31/**
32 * Make it so that when linking to a category, they are linked to the category's
33 * associated static page instead.
34 */
35function acme_rewrite_static_page_category_links($catlink, $category_id){
36        if($category_id == ACME_NEWS_CATEGORY_ID)
37                return get_permalink(ACME_NEWS_PAGE_ID);
38        return $catlink;
39}
40add_filter('category_link', 'acme_rewrite_static_page_category_links', 10, 2);
41
42
43/**
44 * When attemptint to view category that is assigned to a static page, redirect
45 * to the page.
46 */
47function acme_redirect_to_static_category_page(){
48        global $wp_query;
49        if(is_category(ACME_NEWS_CATEGORY_ID)){
50                wp_redirect(get_permalink(ACME_NEWS_PAGE_ID));
51        }
52}
53add_action('template_redirect', 'acme_redirect_to_static_category_page', 10, 3);
54
55
56//Not included: rewriting archive pages