Add fictioneer_blog shortcode

This commit is contained in:
Tetrakern 2023-03-10 15:35:55 +01:00
parent 3c9146aa3a
commit ebbe4d72e9
4 changed files with 141 additions and 10 deletions

View File

@ -23,6 +23,7 @@ This documentation is about the Fictioneer theme. If you need help with WordPres
* [Additional CSS Classes](#additional-css-classes)
* [HTML Block / litRPG Box](#html-block)
* [Shortcodes](#shortcodes)
* [Blog](#blog)
* [Bookmarks](#bookmarks)
* [Contact Form](#contact-form)
* [Cookie Buttons](#cookie-buttons)
@ -378,6 +379,31 @@ The custom HTML block is the best way to add special elements to the content, su
[Shortcodes](https://wordpress.org/support/article/shortcode-block/) are bracket-enclosed keywords placed within the content that WordPress automatically interprets into code, adding features or objects without the need for programming. This should be done inside a _shortcode_ block. Since most elements created by shortcodes have no margins, the _spacer_ block can be a good addition before and/or after.
### Blog
Renders paginated blog posts akin to the default blog page, but with options. Makes use of the main query pagination variable, so only use this once per page. Optional parameters are **per_page**, **author**, **exclude_cat_ids**, **exclude_tag_ids**, **categories**, **tags**, **rel**, and **class**.
* **per_page:** Number of posts per page. Defaults to theme settings.
* **author:** Only show chapters of a specific author. Make sure to write the name right.
* **exclude_cat_ids:** Comma-separated list of category IDs, if you want to exclude some.
* **exclude_tag_ids:** Comma-separated list of tag IDs, if you want to exclude some.
* **categories:** Comma-separated list of category names (case-insensitive), if you want to pick from a curated pool.
* **tags:** Comma-separated list of tag names (case-insensitive), if you want to pick from a curated pool.
* **rel:** Relationship between different taxonomies, either `AND` or `OR`. Default `AND`.
* **class:** Additional CSS classes, separated by whitespace.
```
[fictioneer_blog]
```
```
[fictioneer_blog class="foo bar baz" per_page="5" exclude_cat_ids="1,23,24" categories="news"]
```
```
[fictioneer_blog categories="uncategorized"]
```
### Bookmarks
Renders a two-column grid of small bookmark cards, ordered by date of creation. The bookmarks are stored in the browser and appended to the document via JavaScript. You can combine this with the `show-if-bookmarks hidden` additional CSS classes, displaying a headline or other element only if bookmarks are present. Optional parameters are **count** and **show_empty**.

View File

@ -511,12 +511,12 @@ Filters the query arguments in the `fictioneer_blog` shortcode.
**$query_args:**
* $post_type (string) `post`
* $post_status (string) `'publish'`
* $author_name (string|null) Limit posts to a specific author.
* $tag__not_in (\[string]|null) Array of tag IDs to exclude.
* $category__not_in (\[string]|null) Array of category IDs to exclude.
* $paged (int) Current page.
* $posts_per_page (int) Posts per page, defaults to theme option.
* $tax_query (array|null) Query for taxonomies.
* $author_name (string|null) `$args['author']`
* $tag__not_in (\[string]|null) Array extracted from `$args['exclude_tag_ids']`.
* $category__not_in (\[string]|null) Array extracted from `$args['exclude_cat_ids']`.
* $paged (int) Current main query page.
* $posts_per_page (int) `$args['per_page']` or theme default.
* $tax_query (array|null) Query arguments for taxonomies.
**$args:**
* $per_page (string|null) Optional. The number of posts per page.
@ -525,9 +525,6 @@ Filters the query arguments in the `fictioneer_blog` shortcode.
* $exclude_cat_ids (string|null) Optional. Comma-separated list of category IDs.
* $categories (string|null) Optional. Comma-separated list of category names.
* $tags (string|null) Optional. Comma-separated list of tag names.
* $fandoms (string|null) Optional. Comma-separated list of fandom names.
* $genres (string|null) Optional. Comma-separated list of genre names.
* $characters (string|null) Optional. Comma-separated list of character names.
* $rel (string|null) Optional. Relationship between taxonomies (`AND` or `OR`).
* $class (string|null) Optional. Additional CSS classes.

View File

@ -1091,4 +1091,109 @@ function fictioneer_shortcode_search( $attr ) {
}
add_shortcode( 'fictioneer_search', 'fictioneer_shortcode_search' );
// =============================================================================
// BLOG SHORTCODE
// =============================================================================
/**
* Shortcode to show blog with pagination
*
* @since 5.2.0
*
* @param string|null $attr['per_page'] Optional. Number of posts per page.
* @param string|null $attr['author'] Optional. Limit items to a specific author.
* @param string|null $attr['exclude_tag_ids'] Optional. Exclude posts with these tags.
* @param string|null $attr['exclude_cat_ids'] Optional. Exclude posts with these categories.
* @param string|null $attr['categories'] Optional. Limit items to specific category names.
* @param string|null $attr['tags'] Optional. Limit items to specific tag names.
* @param string|null $attr['rel'] Optional. Relationship between taxonomies. Default 'AND'.
* @param string|null $attr['class'] Optional. Additional CSS classes, separated by whitespace.
*
* @return string The rendered shortcode HTML.
*/
function fictioneer_shortcode_blog( $attr ) {
// Setup
$page = intval( get_query_var( 'page', 1 ) );
$author = $attr['author'] ?? false;
$taxonomies = fictioneer_get_shortcode_taxonomies( $attr );
$exclude_tag_ids = fictioneer_explode_list( $attr['exclude_tag_ids'] ?? '' );
$exclude_cat_ids = fictioneer_explode_list( $attr['exclude_cat_ids'] ?? '' );
$rel = 'AND';
$classes = '';
// Arguments
$query_args = array(
'post_type' => 'post',
'post_status' => 'publish',
'paged' => $page,
'posts_per_page' => $attr['per_page'] ?? get_option( 'posts_per_page' )
);
// Author?
if ( ! empty( $author ) ) $query_args['author_name'] = $author;
// Relation?
if ( ! empty( $attr['rel'] ) ) {
$rel = strtolower( $attr['rel'] ) == 'or' ? 'OR' : $rel;
}
// Taxonomies?
if ( ! empty( $taxonomies ) ) {
$taxonomies['relation'] = $rel;
$taxonomies['taxonomies'] = $taxonomies;
$query_args['tax_query'] = fictioneer_get_shortcode_tax_query( $taxonomies );
}
// Excluded tags?
if ( ! empty( $exclude_tag_ids ) ) $query_args['tag__not_in'] = $exclude_tag_ids;
// Excluded categories?
if ( ! empty( $exclude_cat_ids ) ) $query_args['category__not_in'] = $exclude_cat_ids;
// Extra classes
if ( ! empty( $attr['class'] ) ) $classes = esc_attr( wp_strip_all_tags( $attr['class'] ) );
// Apply filters
$query_args = apply_filters( 'fictioneer_filter_shortcode_blog_query_args', $query_args, $attr );
// Query
$blog_query = new WP_Query( $query_args );
// Buffer
ob_start();
if ( $blog_query->have_posts() ) {
// Start HTML ---> ?>
<section class="blog <?php echo $classes; ?>" id="blog"><?php
while ( $blog_query->have_posts() ) {
$blog_query->the_post();
get_template_part( 'partials/_post', null, ['nested' => true] );
$pag_args = array(
'current' => max( 1, $page ),
'total' => $blog_query->max_num_pages,
'prev_text' => fcntr( 'previous' ),
'next_text' => fcntr( 'next' ),
'add_fragment' => '#blog'
);
}
wp_reset_postdata();
?></section>
<nav class="pagination _padding-top"><?php echo paginate_links( $pag_args ); ?></nav>
<?php // <--- End HTML
} else {
// Start HTML ---> ?>
<article class="post _empty">
<span><?php _e( 'No (more) posts found.', 'fictioneer' ) ?></span>
</article>
<?php // <--- End HTML
}
// Return buffer
return ob_get_clean();
}
add_shortcode( 'fictioneer_blog', 'fictioneer_shortcode_blog' );
?>

View File

@ -9,6 +9,8 @@
* @subpackage Fictioneer
* @since 2.0
* @see partials/_loop.php
*
* @internal $args['nested'] Whether the post is nested inside another query. Default null.
*/
?>
@ -18,6 +20,7 @@
$title = fictioneer_get_safe_title( get_the_ID() );
$content = apply_filters( 'the_content', get_the_content( fcntr( 'read_more' ) ) );
$label = esc_attr( sprintf( _x( 'Continue reading %s', 'Read more link aria label', 'fictioneer' ), $title ) );
$nested = $args['nested'] ?? false;
// Password?
if ( post_password_required() ) {
@ -33,7 +36,7 @@ if (
?>
<article id="post-<?php the_ID(); ?>" class="post padding-left padding-right">
<article id="post-<?php the_ID(); ?>" class="post <?php if ( ! $nested ) echo 'padding-left padding-right'; ?>">
<header class="post__header">
<h2 class="post__title"><a href="<?php the_permalink(); ?>"><?php echo $title; ?></a></h2>