From b45c0498bad050edf4a488887ac8af5bc29bf84f Mon Sep 17 00:00:00 2001 From: Alex Kirk Date: Fri, 20 Dec 2024 09:14:12 +0100 Subject: [PATCH] Make tagged posts viewable on the frontend --- includes/class-frontend.php | 26 ++++++++++++++++++++----- templates/frontend/main-feed-header.php | 9 +++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/includes/class-frontend.php b/includes/class-frontend.php index 17fc2be8..759477db 100644 --- a/includes/class-frontend.php +++ b/includes/class-frontend.php @@ -39,6 +39,13 @@ class Frontend { */ public $author = false; + /** + * Whether an tag is being displayed + * + * @var object|false + */ + public $tag = false; + /** * Whether a post-format is being displayed * @@ -1410,8 +1417,14 @@ public function friend_posts_query( $query ) { break; case 'tag': - $post_tag = array_shift( $pagename_parts ); - $tax_query = $this->friends->wp_query_get_post_tag_tax_query( $tax_query, $post_tag ); + if ( empty( $pagename_parts ) && $page_id ) { + // Support numeric tags. + $this->tag = strval( $page_id ); + $page_id = false; + } else { + $this->tag = array_shift( $pagename_parts ); + } + $tax_query = $this->friends->wp_query_get_post_tag_tax_query( $tax_query, $this->tag ); break; default: // Maybe an author. @@ -1476,9 +1489,12 @@ public function friend_posts_query( $query ) { $query->set( 'page_id', $page_id ); if ( ! $this->author ) { $post = get_post( $page_id ); - $author = User::get_post_author( $post ); - if ( false !== $author ) { - $this->author = $author; + + if ( $post ) { + $author = User::get_post_author( $post ); + if ( false !== $author ) { + $this->author = $author; + } } } $query->is_single = true; diff --git a/templates/frontend/main-feed-header.php b/templates/frontend/main-feed-header.php index 47d72fa9..56b540a3 100644 --- a/templates/frontend/main-feed-header.php +++ b/templates/frontend/main-feed-header.php @@ -63,6 +63,15 @@ $_title ) ); +} elseif ( $args['friends']->frontend->tag ) { + echo esc_html( + sprintf( + // translators: %1$s is a hash tag, %2$s is a type of feed, e.g. "Main Feed". + _x( '#%1$s on %2$s', '#tag on feed', 'friends' ), + $args['friends']->frontend->tag, + $_title + ) + ); } else { echo esc_html( $_title ); }