fictioneer/partials/_showcase.php

151 lines
5.4 KiB
PHP
Raw Normal View History

2023-01-21 01:31:34 +01:00
<?php
/**
* Partial: Showcase
*
* This template part renders a showcase of posts and can be called with the
* [fictioneer_showcase for="post_type" count order orderby author post_ids]
* shortcode. Posts are displayed as grid with a maximum of three columns,
* collapsing to one on mobile.
*
* @package WordPress
* @subpackage Fictioneer
* @since 4.0
*
* @internal $args['post_type'] Post type if the showcase.
2023-09-16 20:40:44 +02:00
* @internal $args['count'] Maximum number of items. Default 8.
* @internal $args['order'] Order direction. Default 'DESC'.
* @internal $args['orderby'] Order argument. Default 'date'.
* @internal $args['author'] Author provided by the shortcode.
* @internal $args['post_ids'] Array of post IDs. Default empty.
* @internal $args['excluded_cats'] Array of category IDs to exclude. Default empty.
* @internal $args['excluded_tags'] Array of tag IDs to exclude. Default empty.
* @internal $args['ignore_protected'] Whether to ignore protected posts. Default false.
* @internal $args['taxonomies'] Array of taxonomy arrays. Default empty.
* @internal $args['relation'] Relationship between taxonomies.
2023-09-15 22:42:34 +02:00
* @internal $args['classes'] String of additional CSS classes. Default empty.
2023-01-21 01:31:34 +01:00
*/
?>
<?php
// No direct access!
defined( 'ABSPATH' ) OR exit;
2023-01-21 01:31:34 +01:00
// Prepare query
$query_args = array (
2023-09-16 13:38:49 +02:00
'fictioneer_query_name' => 'showcase',
'post_type' => $args['post_type'],
2023-01-21 01:31:34 +01:00
'post_status' => 'publish',
'post__in' => $args['post_ids'], // May be empty!
2023-09-16 13:38:49 +02:00
'order' => $args['order'],
'orderby' => $args['orderby'],
'posts_per_page' => $args['count'],
2023-03-09 11:40:41 +01:00
'update_post_term_cache' => false,
'no_found_rows' => true
2023-01-21 01:31:34 +01:00
);
2023-09-16 13:38:49 +02:00
// Author?
if ( isset( $args['author'] ) && $args['author'] ) {
$query_args['author_name'] = $args['author'];
}
2023-01-21 01:31:34 +01:00
// Author IDs?
if ( ! empty( $args['author_ids'] ) ) {
$query_args['author__in'] = $args['author_ids'];
}
2023-03-09 11:40:41 +01:00
// Taxonomies?
if ( ! empty( $args['taxonomies'] ) ) {
$query_args['tax_query'] = fictioneer_get_shortcode_tax_query( $args );
}
2023-03-10 17:29:00 +01:00
// Excluded tags?
if ( ! empty( $args['excluded_tags'] ) ) {
$query_args['tag__not_in'] = $args['excluded_tags'];
}
// Excluded categories?
if ( ! empty( $args['excluded_cats'] ) ) {
$query_args['category__not_in'] = $args['excluded_cats'];
}
// Excluded authors?
if ( ! empty( $args['excluded_authors'] ) ) {
$query_args['author__not_in'] = $args['excluded_authors'];
}
// Ignore protected?
if ( $args['ignore_protected'] ) {
add_filter( 'posts_where', 'fictioneer_exclude_protected_posts' );
}
2023-03-09 11:40:41 +01:00
// Apply filters
2023-03-10 15:20:48 +01:00
$query_args = apply_filters( 'fictioneer_filter_shortcode_showcase_query_args', $query_args, $args );
2023-03-09 11:40:41 +01:00
2023-01-21 01:31:34 +01:00
// Query collections
$query = fictioneer_shortcode_query( $query_args );
2023-01-21 01:31:34 +01:00
// Remove temporary filters
remove_filter( 'posts_where', 'fictioneer_exclude_protected_posts' );
2023-01-21 01:31:34 +01:00
?>
<?php if ( $query->have_posts() ) : ?>
2023-09-15 22:42:34 +02:00
<section class="showcase <?php echo $args['classes']; ?>">
2023-01-21 01:31:34 +01:00
<ul class="showcase__list">
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<li class="showcase__list-item">
2023-08-30 12:35:13 +02:00
<a class="showcase__list-item-link polygon" href="<?php the_permalink(); ?>">
<figure class="showcase__list-item-figure">
2023-01-21 01:31:34 +01:00
<?php
// Setup
$list_title = '';
$story_id = null;
$landscape_image_id = fictioneer_get_field( 'fictioneer_landscape_image', get_the_ID() );
// Get list title and story ID (if any)
switch ( $args['post_type'] ) {
2023-01-21 01:31:34 +01:00
case 'fcn_collection':
$list_title = trim( fictioneer_get_field( 'fictioneer_collection_list_title' ) );
break;
case 'fcn_chapter':
$list_title = trim( fictioneer_get_field( 'fictioneer_chapter_list_title' ) );
$story_id = fictioneer_get_field( 'fictioneer_chapter_story', get_the_ID() );
2023-08-31 23:52:37 +02:00
2023-01-21 01:31:34 +01:00
if ( empty( $landscape_image_id ) ) {
$landscape_image_id = fictioneer_get_field( 'fictioneer_landscape_image', $story_id );
}
break;
}
// Prepare title
2023-08-31 23:52:37 +02:00
$title = empty( $list_title ) ? fictioneer_get_safe_title( $post->ID ) : $list_title;
2023-01-21 01:31:34 +01:00
// Prepare image arguments
$image_args = array(
'alt' => sprintf( __( '%s Cover', 'fictioneer' ), $title ),
'class' => 'no-auto-lightbox showcase__image'
);
// Output image or placeholder
if ( ! empty( $landscape_image_id ) ) {
echo wp_get_attachment_image( $landscape_image_id, 'medium', false, $image_args );
} elseif ( has_post_thumbnail() ) {
the_post_thumbnail( 'medium', $image_args );
} elseif ( $story_id && has_post_thumbnail( $story_id ) ) {
echo get_the_post_thumbnail( $story_id, 'medium', $image_args );
} else {
echo '<div class="showcase__image _no-cover"></div>';
}
?>
<?php if ( ! $args['no_cap'] ) : ?>
2023-08-30 12:35:13 +02:00
<figcaption class="showcase__list-item-figcaption"><?php echo $title; ?></figcaption>
2023-01-21 01:31:34 +01:00
<?php endif; ?>
</figure>
</a>
</li>
<?php endwhile; ?>
</ul>
</section>
<?php endif; wp_reset_postdata(); ?>