Extended archives and sort-order-filter
This commit is contained in:
parent
312972a9f0
commit
ca3ef92068
25
ACTIONS.md
25
ACTIONS.md
@ -123,6 +123,31 @@ Fires between the site’s `<main>` and `<footer>` blocks. This is the empty spa
|
||||
|
||||
---
|
||||
|
||||
### `do_action( 'fictioneer_archive_loop_after', $args )`
|
||||
Archive template hook. Fires right after the result loop section in any archive template.
|
||||
|
||||
**$args:**
|
||||
* $current_page (int) – Current page number of pagination or 1.
|
||||
* $order (string) – Current order query argument. Default 'desc'.
|
||||
* $orderby (string) – Current orderby query argument. Default 'date'.
|
||||
* $ago (int|string) – Current value for the date query. Default 0.
|
||||
|
||||
---
|
||||
|
||||
### `do_action( 'fictioneer_archive_loop_before', $args )`
|
||||
Archive template hook. Fires right before the result loop section in any archive template.
|
||||
|
||||
**$args:**
|
||||
* $current_page (int) – Current page number of pagination or 1.
|
||||
* $order (string) – Current order query argument. Default 'desc'.
|
||||
* $orderby (string) – Current orderby query argument. Default 'date'.
|
||||
* $ago (int|string) – Current value for the date query. Default 0.
|
||||
|
||||
**Hooked actions:**
|
||||
* `fictioneer_sort_order_filter_interface( $args )` – Interface to sort, order, and filter. Priority 10.
|
||||
|
||||
---
|
||||
|
||||
### `do_action( 'fictioneer_before_comments' )`
|
||||
Fires right before the comment section outside the `<article>` but still within the `<main>` block. Note that the wrapping block does not provide padding, which means you may use the full container width. If needed, add the `padding-left` and/or `padding-right` utility CSS classes for responsive horizontal padding.
|
||||
|
||||
|
@ -1075,4 +1075,43 @@ if ( get_option( 'fictioneer_restrict_rest_api' ) ) {
|
||||
add_filter( 'rest_authentication_errors', 'fictioneer_restrict_rest_api' );
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// ADD SORT, ORDER & FILTER TO TAXONOMY QUERIES
|
||||
// =============================================================================
|
||||
|
||||
/**
|
||||
* Modifies the query on archive pages
|
||||
*
|
||||
* @since 5.4.0
|
||||
*
|
||||
* @param WP_Query $query The current WP_Query object.
|
||||
*/
|
||||
|
||||
function fictioneer_add_sof_to_taxonomy_query( $query ) {
|
||||
// Abort if...
|
||||
if ( is_admin() || ! is_archive() || ! $query->is_main_query() ) return;
|
||||
|
||||
// Use empty array to get date query
|
||||
$date_query = fictioneer_append_date_query( [] );
|
||||
|
||||
// If date query set...
|
||||
if ( ! empty( $date_query['date_query'] ) ) {
|
||||
$query->set( 'date_query', $date_query['date_query'] );
|
||||
}
|
||||
|
||||
// Post type?
|
||||
$post_type = strtolower( sanitize_text_field( $_GET['post_type'] ?? '' ) );
|
||||
$post_type = array_intersect(
|
||||
[ $post_type ],
|
||||
['any', 'post', 'fcn_story', 'fcn_chapter', 'fcn_collection', 'fcn_recommendation']
|
||||
);
|
||||
$post_type = reset( $post_type ) ?: null;
|
||||
|
||||
// If post type queried...
|
||||
if ( ! empty( $post_type ) && $post_type !== 'any' ) {
|
||||
$query->set( 'post_type', $post_type );
|
||||
}
|
||||
}
|
||||
add_action( 'pre_get_posts', 'fictioneer_add_sof_to_taxonomy_query' );
|
||||
|
||||
?>
|
||||
|
@ -217,6 +217,25 @@ function fictioneer_sort_order_filter_interface( $args ) {
|
||||
$current_url = home_url( $base_uri );
|
||||
$current_url = preg_replace( '/\/page\/\d+\/$/', '', $current_url );
|
||||
$current_url = preg_replace( '/\/page\/\d+$/', '', $current_url );
|
||||
$post_type = null;
|
||||
|
||||
// Archive?
|
||||
if ( is_archive() ) {
|
||||
$post_type = strtolower( sanitize_text_field( $_GET['post_type'] ?? '' ) );
|
||||
$post_type = array_intersect(
|
||||
[ $post_type ],
|
||||
['any', 'post', 'fcn_story', 'fcn_chapter', 'fcn_collection', 'fcn_recommendation']
|
||||
);
|
||||
$post_type = reset( $post_type ) ?: null;
|
||||
}
|
||||
|
||||
// Post type?
|
||||
if ( ! empty( $post_type ) ) {
|
||||
$current_url = add_query_arg(
|
||||
array( 'post_type' => $post_type ),
|
||||
$current_url
|
||||
);
|
||||
}
|
||||
|
||||
// Order?
|
||||
if ( ! empty( $args['order'] ) ) {
|
||||
@ -250,6 +269,34 @@ function fictioneer_sort_order_filter_interface( $args ) {
|
||||
}
|
||||
}
|
||||
|
||||
// Post type menu options
|
||||
$post_type_menu = array(
|
||||
'any' => array(
|
||||
'label' => __( 'All Posts', 'fictioneer' ),
|
||||
'url' => add_query_arg( array( 'post_type' => 'any' ), $current_url ) . '#sof'
|
||||
),
|
||||
'post' => array(
|
||||
'label' => __( 'Blog Posts', 'fictioneer' ),
|
||||
'url' => add_query_arg( array( 'post_type' => 'post' ), $current_url ) . '#sof'
|
||||
),
|
||||
'fcn_story' => array(
|
||||
'label' => __( 'Stories', 'fictioneer' ),
|
||||
'url' => add_query_arg( array( 'post_type' => 'fcn_story' ), $current_url ) . '#sof'
|
||||
),
|
||||
'fcn_chapter' => array(
|
||||
'label' => __( 'Chapters', 'fictioneer' ),
|
||||
'url' => add_query_arg( array( 'post_type' => 'fcn_chapter' ), $current_url ) . '#sof'
|
||||
),
|
||||
'fcn_collection' => array(
|
||||
'label' => __( 'Collections', 'fictioneer' ),
|
||||
'url' => add_query_arg( array( 'post_type' => 'fcn_collection' ), $current_url ) . '#sof'
|
||||
),
|
||||
'fcn_recommendation' => array(
|
||||
'label' => __( 'Recommendations', 'fictioneer' ),
|
||||
'url' => add_query_arg( array( 'post_type' => 'fcn_recommendation' ), $current_url ) . '#sof'
|
||||
)
|
||||
);
|
||||
|
||||
// Order menu options
|
||||
$orderby_menu = array(
|
||||
'modified' => array(
|
||||
@ -261,7 +308,7 @@ function fictioneer_sort_order_filter_interface( $args ) {
|
||||
'url' => add_query_arg( array( 'orderby' => 'date' ), $current_url ) . '#sof'
|
||||
),
|
||||
'title' => array(
|
||||
'label' => __( 'Title', 'fictioneer' ),
|
||||
'label' => __( 'By Title', 'fictioneer' ),
|
||||
'url' => add_query_arg( array( 'orderby' => 'title' ), $current_url ) . '#sof'
|
||||
)
|
||||
);
|
||||
@ -316,6 +363,21 @@ function fictioneer_sort_order_filter_interface( $args ) {
|
||||
// Start HTML ---> ?>
|
||||
<div id="sof" class="sort-order-filter">
|
||||
|
||||
<?php if ( is_archive() ) : ?>
|
||||
<div class="list-button _text popup-menu-toggle toggle-last-clicked" tabindex="0" role="button"><?php
|
||||
echo $post_type_menu[ $post_type ?? 'any' ]['label'] ?? __( 'Unknown', 'fictioneer' );
|
||||
echo '<div class="popup-menu _bottom _center">';
|
||||
echo '<div class="popup-heading">' . __( 'Post Type', 'fictioneer' ) . '</div>';
|
||||
|
||||
foreach( $post_type_menu as $tuple ) {
|
||||
$url = esc_url( $tuple['url'] );
|
||||
echo "<a href='{$url}'>{$tuple['label']}</a>";
|
||||
}
|
||||
|
||||
echo '</div>';
|
||||
?></div>
|
||||
<?php endif; ?>
|
||||
|
||||
<div class="list-button _text popup-menu-toggle toggle-last-clicked" tabindex="0" role="button"><?php
|
||||
echo $orderby_menu[ $args['orderby'] ]['label'] ?? __( 'Custom', 'fictioneer' );
|
||||
echo '<div class="popup-menu _bottom _center">';
|
||||
@ -358,5 +420,6 @@ add_action( 'fictioneer_stories_after_content', 'fictioneer_sort_order_filter_in
|
||||
add_action( 'fictioneer_chapters_after_content', 'fictioneer_sort_order_filter_interface', 20 );
|
||||
add_action( 'fictioneer_collections_after_content', 'fictioneer_sort_order_filter_interface', 20 );
|
||||
add_action( 'fictioneer_recommendations_after_content', 'fictioneer_sort_order_filter_interface', 20 );
|
||||
add_action( 'fictioneer_archive_loop_before', 'fictioneer_sort_order_filter_interface', 10 );
|
||||
|
||||
?>
|
||||
|
@ -16,7 +16,31 @@
|
||||
*/
|
||||
?>
|
||||
|
||||
<?php
|
||||
|
||||
// Setup
|
||||
$page = get_query_var( 'paged', 1 ) ?: 1; // Main query
|
||||
$order = array_intersect( [strtolower( $_GET['order'] ?? 0 )], ['desc', 'asc'] );
|
||||
$order = reset( $order ) ?: 'desc';
|
||||
$orderby = array_intersect( [strtolower( $_GET['orderby'] ?? 0 )], ['modified', 'date', 'title', 'rand'] );
|
||||
$orderby = reset( $orderby ) ?: 'date';
|
||||
$ago = $_GET['ago'] ?? 0;
|
||||
$ago = is_numeric( $ago ) ? absint( $ago ) : sanitize_text_field( $ago );
|
||||
|
||||
// Prepare sort-order-filter arguments
|
||||
$hook_args = array(
|
||||
'page' => $page,
|
||||
'order' => $order,
|
||||
'orderby' => $orderby,
|
||||
'ago' => $ago
|
||||
);
|
||||
|
||||
?>
|
||||
|
||||
<?php do_action( 'fictioneer_archive_loop_before', $hook_args ); ?>
|
||||
|
||||
<?php if ( have_posts() ) : ?>
|
||||
|
||||
<section class="archive__posts">
|
||||
<ul class="card-list _no-mutation-observer" id="archive-list">
|
||||
<?php
|
||||
@ -25,7 +49,13 @@
|
||||
|
||||
// Setup
|
||||
$type = get_post_type();
|
||||
$card_args = ['show_type' => true];
|
||||
$card_args = array(
|
||||
'cache' => fictioneer_caching_active() && ! fictioneer_private_caching_active(),
|
||||
'show_type' => true,
|
||||
'order' => $order,
|
||||
'orderby' => $orderby,
|
||||
'ago' => $ago
|
||||
);
|
||||
|
||||
// Cached?
|
||||
if ( fictioneer_caching_active() && ! fictioneer_private_caching_active() ) $card_args['cache'] = true;
|
||||
@ -55,4 +85,17 @@
|
||||
?>
|
||||
</ul>
|
||||
</section>
|
||||
|
||||
<?php else : ?>
|
||||
|
||||
<section class="archive__posts">
|
||||
<ul class="card-list _no-mutation-observer" id="archive-list">
|
||||
<li class="no-results">
|
||||
<span><?php _e( 'No matching posts found.', 'fictioneer' ) ?></span>
|
||||
</li>
|
||||
</ul>
|
||||
</section>
|
||||
|
||||
<?php endif; ?>
|
||||
|
||||
<?php do_action( 'fictioneer_archive_loop_after', $hook_args ); ?>
|
||||
|
Loading…
x
Reference in New Issue
Block a user