Improve latest_posts shortcode

This commit is contained in:
Tetrakern 2023-03-08 11:36:26 +01:00
parent a054e034f5
commit d309213742
4 changed files with 73 additions and 67 deletions

View File

@ -485,13 +485,14 @@ Renders a two-column grid of small cards, showing the latest four chapters order
### Latest Posts ### Latest Posts
Renders the last blog post or a list of blog posts, ignoring sticky posts, ordered by publishing date, descending. Optional parameters are **count**, **author**, **posts**, **categories**, **tags**, and **class**. Renders the last blog post or a list of blog posts, ignoring sticky posts, ordered by publishing date, descending. Optional parameters are **count**, **author**, **posts**, **categories**, **tags**, **rel**, and **class**.
* **count:** Limit posts to any positive number, although you should keep it reasonable. Default `1`. * **count:** Limit posts to any positive number, although you should keep it reasonable. Default `1`.
* **author:** Only show posts of a specific author. Make sure to write the name right. * **author:** Only show posts of a specific author. Make sure to write the name right.
* **posts:** Comma-separated list of post IDs, if you want to pick from a curated pool. * **posts:** Comma-separated list of post IDs, if you want to pick from a curated pool.
* **categories:** Comma-separated list of category names (case-insensitive), if you want to pick from a curated pool. * **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. * **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. * **class:** Additional CSS classes, separated by whitespace.
``` ```
@ -499,7 +500,7 @@ Renders the last blog post or a list of blog posts, ignoring sticky posts, order
``` ```
``` ```
[fictioneer_latest_posts count="16" tags="world building, characters"] [fictioneer_latest_posts count="16" tags="world building, characters" categories="blog, tutorials" rel="or"]
``` ```
``` ```

View File

@ -370,7 +370,7 @@ Filters the boolean return value of the `fictioneer_is_editor( $user_id )` funct
--- ---
### `apply_filters( 'fictioneer_filter_latest_posts_query_args', $query_args, $args )` ### `apply_filters( 'fictioneer_filter_latest_posts_query_args', $query_args, $args )`
Filters the arguments to query the posts in the `fictioneer_latest_posts` shortcode. Filters the query arguments in the `fictioneer_latest_posts` shortcode. The optional taxonomy arrays can include categories and tags.
**$query_args:** **$query_args:**
* $post_type (string) `'post'` * $post_type (string) `'post'`
@ -383,14 +383,16 @@ Filters the arguments to query the posts in the `fictioneer_latest_posts` shortc
* $posts_per_page (int) `$args['count']` * $posts_per_page (int) `$args['count']`
* $ignore_sticky_posts (boolean) `true` * $ignore_sticky_posts (boolean) `true`
* $no_found_rows (boolean) `true` * $no_found_rows (boolean) `true`
* $author_name (string|null) `$args['author']`
**$args:** **$args:**
* $author (boolean|string) The author provided by the shortcode. Default `false`. * $author (boolean|string) The author provided by the shortcode. Default `false`.
* $count (int) The number of posts provided by the shortcode. Default `1`. * $count (int) The number of posts provided by the shortcode. Default `1`.
* $post_ids (\[string]) Optional. Array of post IDs. * $post_ids (\[string]) Array of post IDs. Default empty.
* $tags (\[string]) Optional. Array of tag names. * $taxonomies (array) Array of taxonomy arrays (names). Default empty.
* $categories (\[string]) Optional. Array of category names. * $rel (string) Relationship between taxonomies. Default `'AND'`.
* $class (string) Optional. Additional CSS classes. * $classes (\[string]) Array of additional CSS classes. Default empty.
* $classes (\[string]) Array of additional CSS classes. Default empty.
--- ---

View File

@ -269,49 +269,48 @@ function fictioneer_shortcode_latest_stories( $attr ) {
$order = $attr['order'] ?? 'desc'; $order = $attr['order'] ?? 'desc';
$orderby = $attr['orderby'] ?? 'date'; $orderby = $attr['orderby'] ?? 'date';
$post_ids = []; $post_ids = [];
$taxonomies = [];
$classes = []; $classes = [];
// Post IDs
if ( ! empty( $attr['stories'] ) ) { if ( ! empty( $attr['stories'] ) ) {
$post_ids = str_replace( ' ', '', $attr['stories'] ); $post_ids = fictioneer_explode_list( $attr['stories'] );
$post_ids = explode( ',', $post_ids );
$post_ids = is_array( $post_ids ) ? $post_ids : [];
$count = count( $post_ids ); $count = count( $post_ids );
} }
// Tags
if ( ! empty( $attr['tags'] ) ) {
$taxonomies['tags'] = fictioneer_explode_list( $attr['tags'] );
}
// Categories
if ( ! empty( $attr['categories'] ) ) {
$taxonomies['categories'] = fictioneer_explode_list( $attr['categories'] );
}
// Extra classes // Extra classes
if ( ! empty( $attr['class'] ) ) $classes[] = esc_attr( wp_strip_all_tags( $attr['class'] ) ); if ( ! empty( $attr['class'] ) ) $classes[] = esc_attr( wp_strip_all_tags( $attr['class'] ) );
// Args
$args = array(
'count' => $count,
'author' => $author,
'order' => $order,
'orderby' => $orderby,
'post_ids' => $post_ids,
'taxonomies' => $taxonomies,
'classes' => $classes
);
// Buffer // Buffer
ob_start(); ob_start();
switch ( $type ) { switch ( $type ) {
case 'compact': case 'compact':
get_template_part( get_template_part( 'partials/_latest-stories-compact', null, $args );
'partials/_latest-stories-compact',
null,
array(
'count' => $count,
'author' => $author,
'order' => $order,
'orderby' => $orderby,
'post_ids' => $post_ids,
'classes' => $classes
)
);
break; break;
default: default:
get_template_part( get_template_part( 'partials/_latest-stories', null, $args );
'partials/_latest-stories',
null,
array(
'count' => $count,
'author' => $author,
'order' => $order,
'orderby' => $orderby,
'post_ids' => $post_ids,
'classes' => $classes
)
);
} }
// Return buffer // Return buffer
@ -497,8 +496,8 @@ function fictioneer_shortcode_latest_posts( $attr ) {
$author = $attr['author'] ?? false; $author = $attr['author'] ?? false;
$count = max( 1, intval( $attr['count'] ?? 1 ) ); $count = max( 1, intval( $attr['count'] ?? 1 ) );
$post_ids = []; $post_ids = [];
$tags = []; $taxonomies = [];
$categories = []; $rel = 'AND';
$classes = []; $classes = [];
// Post IDs // Post IDs
@ -511,12 +510,17 @@ function fictioneer_shortcode_latest_posts( $attr ) {
// Tags // Tags
if ( ! empty( $attr['tags'] ) ) { if ( ! empty( $attr['tags'] ) ) {
$tags = fictioneer_explode_list( $attr['tags'] ); $taxonomies['tags'] = fictioneer_explode_list( $attr['tags'] );
} }
// Categories // Categories
if ( ! empty( $attr['categories'] ) ) { if ( ! empty( $attr['categories'] ) ) {
$categories = fictioneer_explode_list( $attr['categories'] ); $taxonomies['categories'] = fictioneer_explode_list( $attr['categories'] );
}
// Relation
if ( ! empty( $attr['rel'] ) ) {
$rel = strtolower( $attr['rel'] ) == 'or' ? 'OR' : $rel;
} }
// Extra classes // Extra classes
@ -532,8 +536,8 @@ function fictioneer_shortcode_latest_posts( $attr ) {
'count' => $count, 'count' => $count,
'author' => $author, 'author' => $author,
'post_ids' => $post_ids, 'post_ids' => $post_ids,
'tags' => $tags, 'taxonomies' => $taxonomies,
'categories' => $categories, 'relation' => $rel,
'classes' => $classes 'classes' => $classes
) )
); );

View File

@ -11,9 +11,8 @@
* @internal $args['author'] The author provided by the shortcode. Default false. * @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['count'] The number of posts provided by the shortcode. Default 1.
* @internal $args['post_ids'] Array of post IDs. Default empty. * @internal $args['post_ids'] Array of post IDs. Default empty.
* @internal $args['tags'] Array tag names. Default empty. * @internal $args['taxonomies'] Array of taxonomy arrays. Default empty.
* @internal $args['categories'] Array of category names. Default empty. * @internal $args['classes'] Array of additional CSS classes. Default empty.
* @internal $args['class'] Additional classes. Default empty.
*/ */
?> ?>
@ -33,37 +32,37 @@ $query_args = array(
'no_found_rows' => true 'no_found_rows' => true
); );
// Parameter for author?
if ( isset( $args['author'] ) && $args['author'] ) $query_args['author_name'] = $args['author'];
// Taxonomies? // Taxonomies?
if ( ! empty( $args['tags'] ) || ! empty( $args['categories'] ) ) { if ( ! empty( $args['taxonomies'] ) ) {
$query_args['tax_query'] = []; $query_args['tax_query'] = [];
// Relationship? // Relationship?
if ( ! empty( $args['tags'] ) && ! empty( $args['categories'] ) ) { if ( count( $args['taxonomies'] ) > 1 ) {
$query_args['tax_query']['relation'] = 'OR'; $query_args['tax_query']['relation'] = $args['relation'];
} }
}
// Tags? // Tags?
if ( ! empty( $args['tags'] ) ) { if ( ! empty( $args['taxonomies']['tags'] ) ) {
$query_args['tax_query'][] = array( $query_args['tax_query'][] = array(
'taxonomy' => 'post_tag', 'taxonomy' => 'post_tag',
'field' => 'name', 'field' => 'name',
'terms' => $args['tags'], 'terms' => $args['taxonomies']['tags']
); );
} }
// Categories? // Categories?
if ( ! empty( $args['categories'] ) ) { if ( ! empty( $args['taxonomies']['categories'] ) ) {
$query_args['tax_query'][] = array( $query_args['tax_query'][] = array(
'taxonomy' => 'category', 'taxonomy' => 'category',
'field' => 'name', 'field' => 'name',
'terms' => $args['categories'], 'terms' => $args['taxonomies']['categories']
); );
}
} }
// Parameter for author?
if ( isset( $args['author'] ) && $args['author'] ) $query_args['author_name'] = $args['author'];
// Apply filters // Apply filters
$query_args = apply_filters( 'fictioneer_filter_latest_posts_query_args', $query_args, $args ); $query_args = apply_filters( 'fictioneer_filter_latest_posts_query_args', $query_args, $args );