forked from Tetrakern/fictioneer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
singular-authors.php
154 lines (115 loc) · 4.11 KB
/
singular-authors.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/**
* Template Name: Author Index
*
* @package WordPress
* @subpackage Fictioneer
* @since 5.23.1
*/
// Setup
$post_id = get_the_ID();
$sorted_authors = [];
$cached = fictioneer_caching_active( 'author_index' );
// Header
get_header();
// Transient cache?
$transient = $cached ? 0 : get_transient( 'fictioneer_author_index_' . $post_id );
if ( $transient ) {
$sorted_authors = $transient;
}
// ... if not cached
if ( empty( $sorted_authors ) ) {
// Query authors with published posts
$authors = fictioneer_get_publishing_authors( array( 'fields' => array( 'ID', 'display_name', 'user_nicename' ) ) );
// Sort authors
if ( ! empty( $authors ) ) {
// Loop through authors...
foreach ( $authors as $author ) {
// Relevant data
$first_char = mb_strtolower( mb_substr( $author->display_name, 0, 1, 'UTF-8' ), 'UTF-8' );
// Normalize for numbers and other non-alphabetical characters
if ( ! preg_match( '/\p{L}/u', $first_char ) ) {
$first_char = '#'; // Group under '#'
}
// Add index if necessary
if ( ! isset( $sorted_authors[ $first_char ] ) ) {
$sorted_authors[ $first_char ] = [];
}
$sorted_authors[ $first_char ][] = array(
'id' => $author->ID,
'name' => $author->display_name,
'link' => get_author_posts_url( $author->ID, $author->user_nicename )
);
}
// Sort by index
ksort( $sorted_authors );
// Cache as Transient
if ( ! $cached ) {
set_transient( 'fictioneer_author_index_' . $post_id, $sorted_authors, 12 * HOUR_IN_SECONDS );
}
}
}
// Last key
end( $sorted_authors );
$last_key = key( $sorted_authors );
reset( $sorted_authors );
?>
<main id="main" class="main singular index">
<?php do_action( 'fictioneer_main', 'singular-index' ); ?>
<div class="main__wrapper">
<?php do_action( 'fictioneer_main_wrapper' ); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
// Setup
$title = fictioneer_get_safe_title( $post_id, 'singular' );
$this_breadcrumb = [ $title, get_the_permalink() ];
?>
<article id="singular-<?php echo $post_id; ?>" class="singular__article">
<header class="singular__header hidden">
<h1 class="singular__title"><?php echo $title; ?></h1>
</header>
<section class="singular__content content-section"><?php the_content(); ?></section>
<section class="index-letters">
<?php foreach ( $sorted_authors as $index => $authors ) : ?>
<a href="<?php echo esc_attr( "#letter-{$index}" ); ?>" class="index-letters__letter"><?php echo mb_strtoupper( $index ); ?></a>
<?php if ( $last_key !== $index ) : ?>
<span class="index-letters__separator">•</span>
<?php endif; ?>
<?php endforeach; ?>
</section>
<?php foreach ( $sorted_authors as $index => $authors ) : ?>
<section class="glossary">
<h2 id="<?php echo esc_attr( "letter-{$index}" ); ?>"><?php echo mb_strtoupper( $index ); ?></h2>
<div class="glossary__columns">
<?php foreach ( $authors as $author ) : ?>
<div class="glossary__entry">
<div class="glossary__entry-head">
<a href="<?php echo $author['link']; ?>" class="glossary__entry-name _full"><?php
echo $author['name'];
?></a>
</div>
</div>
<?php endforeach; ?>
</div>
</section>
<?php endforeach; ?>
<footer class="singular__footer"><?php do_action( 'fictioneer_singular_footer' ); ?></footer>
</article>
<?php endwhile; ?>
</div>
<?php do_action( 'fictioneer_main_end', 'singular-index' ); ?>
</main>
<?php
// Footer arguments
$footer_args = array(
'post_type' => 'page',
'post_id' => $post_id,
'breadcrumbs' => array(
[fcntr( 'frontpage' ), get_home_url()]
)
);
// Add current breadcrumb
$footer_args['breadcrumbs'][] = $this_breadcrumb;
// Get footer with breadcrumbs
get_footer( null, $footer_args );
?>