fictioneer/partials/_latest-posts.php

119 lines
4.0 KiB
PHP
Raw Normal View History

2023-01-21 01:31:34 +01:00
<?php
/**
* Partial: Latest Post
*
* This template part renders the HTML for the [fictioneer_latest_post] shortcode.
*
* @package WordPress
* @subpackage Fictioneer
* @since 4.0
*
2023-08-19 22:22:56 +02:00
* @internal $args['author'] The author provided by the shortcode. Default false.
* @internal $args['count'] The number of posts provided by the shortcode. Default 1.
* @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['taxonomies'] Array of taxonomy arrays. Default empty.
* @internal $args['relation'] Relationship between taxonomies.
* @internal $args['classes'] Array 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
2023-03-08 01:07:50 +01:00
$query_args = array(
2023-01-21 01:31:34 +01:00
'post_type' => 'post',
2023-03-08 01:07:50 +01:00
'post_status' => 'publish',
'post__in' => $args['post_ids'], // May be empty!
2023-01-21 01:31:34 +01:00
'has_password' => false,
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => $args['count'],
'ignore_sticky_posts' => 1,
'no_found_rows' => true, // Improve performance
'update_post_term_cache' => false // Improve performance
2023-01-21 01:31:34 +01:00
);
2023-03-08 11:36:26 +01:00
// Parameter for author?
if ( isset( $args['author'] ) && $args['author'] ) $query_args['author_name'] = $args['author'];
2023-03-08 01:07:50 +01:00
// Taxonomies?
2023-03-08 11:36:26 +01:00
if ( ! empty( $args['taxonomies'] ) ) {
2023-03-08 23:24:41 +01:00
$query_args['tax_query'] = fictioneer_get_shortcode_tax_query( $args );
2023-03-08 01:07:50 +01:00
}
2023-03-10 17:23:29 +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'];
}
2023-03-08 01:07:50 +01:00
// Apply filters
2023-03-10 15:20:48 +01:00
$query_args = apply_filters( 'fictioneer_filter_shortcode_latest_posts_query_args', $query_args, $args );
2023-03-08 01:07:50 +01:00
2023-01-21 01:31:34 +01:00
// Query post
$latest_entries = fictioneer_shortcode_query( $query_args );
2023-01-21 01:31:34 +01:00
?>
2023-01-27 01:23:02 +01:00
<section class="latest-posts <?php echo implode( ' ', $args['classes'] ); ?>">
2023-01-21 01:31:34 +01:00
<?php if ( $latest_entries->have_posts() ) : ?>
<?php while ( $latest_entries->have_posts() ) : $latest_entries->the_post(); ?>
<?php
// Setup
$title = fictioneer_get_safe_title( get_the_ID() );
$label = esc_attr( sprintf( _x( 'Continue reading %s', 'Read more link aria label', 'fictioneer' ), $title ) );
2023-01-21 01:31:34 +01:00
if (
! get_option( 'fictioneer_show_full_post_content' ) &&
! strpos( $post->post_content, '<!--more-->' )
) {
$content = '<p>' . fictioneer_get_excerpt() . '</p><a class="more-link" href="' . get_permalink() . '" title="' . $label . '" aria-label="' . $label . '">' . fcntr( 'read_more' ) . '</a>';
} else {
$content = apply_filters( 'the_content', get_the_content( fcntr( 'read_more' ) ) );
2023-01-21 01:31:34 +01:00
}
?>
<article id="post-<?php the_ID(); ?>" class="post">
<header class="post__header">
<h2 class="post__title"><a href="<?php the_permalink(); ?>"><?php echo $title; ?></a></h2>
<div class="post__meta layout-links"><?php echo fictioneer_get_post_meta_items( ['no_cat' => true] ); ?></div>
</header>
<section class="post__main content-section">
<div class="post__excerpt"><?php echo $content; ?></div>
</section>
<?php if ( has_action( 'fictioneer_latest_posts_footers_left' ) || has_action( 'fictioneer_latest_posts_footers_right' ) ) : ?>
<footer class="post__footer">
<div class="post__footer-left">
<?php do_action( 'fictioneer_latest_posts_footers_left', get_the_ID() ); ?>
</div>
<div class="post__footer-right">
<?php do_action( 'fictioneer_latest_posts_footers_right', get_the_ID() ); ?>
</div>
</footer>
<?php endif; ?>
</article>
<?php endwhile; ?>
<?php else: ?>
<div class="no-results"><?php _e( 'No posts have been published yet.', 'fictioneer' ) ?></div>
<?php endif; wp_reset_postdata(); ?>
</section>