fictioneer/singular-index.php

158 lines
4.0 KiB
PHP
Raw Normal View History

2024-04-19 02:21:48 +02:00
<?php
/**
* Template Name: Index
*
* @package WordPress
* @subpackage Fictioneer
* @since 5.14.0
*/
// Setup
$post_id = get_the_ID();
// Header
get_header();
// Query all stories
$args = array(
'post_type' => 'fcn_story',
'post_status' => ['publish'],
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'name',
'update_post_term_cache' => false, // Improve performance
'no_found_rows' => true // Improve performance
);
$stories = new WP_Query( $args );
// Sort stories
$sorted_stories = [];
if ( $stories->have_posts() ) {
// Loop through posts...
foreach ( $stories->posts as $story ) {
2024-05-17 17:17:14 +02:00
$story_id = $story->ID;
2024-04-19 02:21:48 +02:00
// Skip hidden
2024-05-17 17:17:14 +02:00
if ( get_post_meta( $story_id, 'fictioneer_story_hidden', true ) ) {
2024-04-19 02:21:48 +02:00
continue;
}
// Relevant data
2024-05-17 17:17:14 +02:00
$title = trim( fictioneer_get_safe_title( $story_id, 'story_index' ) );
2024-04-22 23:57:31 +02:00
$first_char = strtolower( mb_substr( $title, 0, 1, 'UTF-8' ) );
2024-04-19 02:21:48 +02:00
// Normalize for numbers and other non-alphabetical characters
if ( is_numeric( $first_char ) ) {
$first_char = '#'; // Group under '#'
}
// Add index if necessary
if ( ! isset( $sorted_stories[ $first_char ] ) ) {
$sorted_stories[ $first_char ] = [];
}
$sorted_stories[ $first_char ][] = array(
2024-05-17 17:17:14 +02:00
'id' => $story_id,
2024-04-19 02:21:48 +02:00
'title' => $title,
2024-05-17 17:17:14 +02:00
'link' => get_post_meta( $story_id, 'fictioneer_story_redirect_link', true ) ?: get_permalink( $story_id )
2024-04-19 02:21:48 +02:00
);
}
// Sort by index
ksort( $sorted_stories );
}
2024-04-23 01:09:04 +02:00
// Last key
end( $sorted_stories );
$last_key = key( $sorted_stories );
reset( $sorted_stories );
2024-04-19 02:21:48 +02:00
?>
<main id="main" class="main singular index">
<div class="observer main-observer"></div>
<?php do_action( 'fictioneer_main' ); ?>
<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 padding-left padding-right padding-top padding-bottom">
2024-04-23 00:42:57 +02:00
<header class="singular__header hidden">
2024-04-19 02:21:48 +02:00
<h1 class="singular__title"><?php echo $title; ?></h1>
</header>
2024-04-22 23:57:31 +02:00
<section class="singular__content content-section"><?php the_content(); ?></section>
2024-04-19 02:21:48 +02:00
2024-04-23 00:42:57 +02:00
<section class="index-letters">
<?php foreach ( $sorted_stories as $index => $stories ) : ?>
<a href="<?php echo esc_attr( "#letter-{$index}" ); ?>" class="index-letters__letter"><?php echo strtoupper( $index ); ?></a>
2024-04-23 01:09:04 +02:00
<?php if ( $last_key !== $index ) : ?>
<span class="index-letters__separator">&bull;</span>
<?php endif; ?>
2024-04-23 00:42:57 +02:00
<?php endforeach; ?>
</section>
2024-04-22 23:57:31 +02:00
<?php foreach ( $sorted_stories as $index => $stories ) : ?>
<section class="glossary">
2024-04-19 02:21:48 +02:00
2024-04-23 00:42:57 +02:00
<h2 id="<?php echo esc_attr( "letter-{$index}" ); ?>"><?php echo strtoupper( $index ); ?></h2>
2024-04-19 02:21:48 +02:00
2024-04-22 23:57:31 +02:00
<div class="glossary__columns">
2024-04-19 02:21:48 +02:00
2024-04-22 23:57:31 +02:00
<?php foreach ( $stories as $story ) : ?>
<div class="glossary__entry">
<div class="glossary__entry-head">
<a href="<?php echo $story['link']; ?>" class="glossary__entry-name _full"><?php
echo $story['title'];
?></a>
</div>
</div>
<?php endforeach; ?>
2024-04-19 02:21:48 +02:00
2024-04-22 23:57:31 +02:00
</div>
2024-04-19 02:21:48 +02:00
2024-04-22 23:57:31 +02:00
</section>
<?php endforeach; ?>
2024-04-19 02:21:48 +02:00
<footer class="singular__footer"><?php do_action( 'fictioneer_singular_footer' ); ?></footer>
</article>
<?php endwhile; ?>
</div>
</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 );
?>