Skip to content

Commit

Permalink
CDPT-2216 Reset global $post and WP_Query after getting recent. (#748)
Browse files Browse the repository at this point in the history
* Reset global $post and WP_Query after getting recent.

* CDPT-2216 Amends based on feedback.

* Update view.php
  • Loading branch information
EarthlingDavey authored Oct 28, 2024
1 parent a6c8094 commit faadfcb
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 51 deletions.
2 changes: 1 addition & 1 deletion deploy/config/init/fpm-start.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh

if wp core is-installed 2>/dev/null; then
if wp core is-installed 2>/dev/null; then
# WP is installed.
wp sync-user-roles sync
else
Expand Down
8 changes: 6 additions & 2 deletions public/app/themes/clarity/inc/api/get-news-rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,12 @@ function get_news_api($set_cpt = '')
echo '<h2 class="o-title o-title--section" id="title-section">News</h2>';
endif;

foreach ($posts as $key => $post) {
include locate_template('src/components/c-article-item/view-news-feed.php');
foreach ($posts as $this_post) {
// Load the $post
setup_postdata($this_post);
get_template_part('src/components/c-article-item/view-news-feed');
// Reset the post data
wp_reset_postdata();
}
} else {
echo '<!-- No posts available to return -->';
Expand Down
8 changes: 5 additions & 3 deletions public/app/themes/clarity/page_blog.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@


<div id="content">
<?php foreach ($query->posts as $key => $post) {
include locate_template('src/components/c-article-item/view-blog-feed.php');
} ?>
<?php
while ($query->have_posts()): $query->the_post();
get_template_part('src/components/c-article-item/view-blog-feed');
endwhile;
?>
</div>

<?php get_template_part('src/components/c-pagination/view-infinite', null, ['total_pages' => $query->max_num_pages, 'page' => 1]); ?>
Expand Down
8 changes: 5 additions & 3 deletions public/app/themes/clarity/page_news.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@
<h2 class="o-title o-title--section" id="title-section">Latest</h2>

<div id="content">
<?php foreach ($query->posts as $key => $post) {
include locate_template('src/components/c-article-item/view-news-feed.php');
} ?>
<?php
while ($query->have_posts()): $query->the_post();
get_template_part('src/components/c-article-item/view-news-feed');
endwhile;
?>
</div>

<?php get_template_part('src/components/c-pagination/view-infinite', null, ['total_pages' => $query->max_num_pages, 'page' => 1]); ?>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@

use MOJ\Intranet\Authors;

// Parse template args.
$show_excerpt = isset($args['show_excerpt']) ? $args['show_excerpt'] : true;

$id = $post->ID;

$thumbnail = get_the_post_thumbnail_url($id, 'user-thumb');
$id = $post->ID;
$thumbnail = get_the_post_thumbnail_url($id, 'user-thumb');
$thumbnail_alt = get_post_meta(get_post_thumbnail_id($id), '_wp_attachment_image_alt', true);

// TODO: Why is this here? It's not used.
Expand Down Expand Up @@ -58,8 +59,7 @@
</span>
</div>

<?php // On single blog pages where this is listed we don't have room for the excerpt ?>
<?php if (! is_singular('post')) : ?>
<?php if ($show_excerpt) : ?>

<div class="c-article-excerpt">
<p><?= get_the_excerpt($id) ?></p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
*
* @package Clarity
*/
$id = $post->ID;
$thumbnail = get_the_post_thumbnail_url($id, 'user-thumb');
$thumbnail_alt = get_post_meta(get_post_thumbnail_id($id), '_wp_attachment_image_alt', true);
$link = get_the_permalink($post->ID);

// Parse template args.
$show_excerpt = isset($args['show_excerpt']) ? $args['show_excerpt'] : true;

$id = $post->ID;
$thumbnail = get_the_post_thumbnail_url($id, 'user-thumb');
$thumbnail_alt = get_post_meta(get_post_thumbnail_id($id), '_wp_attachment_image_alt', true);
$link = get_the_permalink($post->ID);

// This component sometimes requires `$set_cpt` depending where this component gets called.
if (! isset($set_cpt)) {
Expand Down Expand Up @@ -36,7 +40,7 @@
</span>
</div>

<?php if (!is_singular('regional_news') && !is_singular('news')) : ?>
<?php if ($show_excerpt) : ?>
<div class="c-article-excerpt">
<p><?= get_the_excerpt($id); ?></p>
</div>
Expand Down
19 changes: 10 additions & 9 deletions public/app/themes/clarity/src/components/c-article/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,30 @@
$query_args = new SearchQueryArgs((new Agency())->getCurrentAgency()['wp_tag_id'], 'post', 1, 5, true);

// Run the query.
$query = new WP_Query($query_args->get());
$recent_query = new WP_Query($query_args->get());

?>
<!-- c-article starts here -->
<article class="c-article">

<h1 class="o-title o-title--page"><?= get_the_title() ?></h1>

<div class="l-primary">
<?php
get_template_part('src/components/c-article-byline/view');
get_template_part('src/components/c-rich-text-block/view');
?>

</div>

<aside class="l-secondary">
<?php
echo '<h2 class="o-title">Recent blog posts</h2>';
foreach ($query->posts as $key => $post) {
include locate_template('src/components/c-article-item/view-blog-feed.php');
}
?>
<h2 class="o-title">Recent blog posts</h2>
<?php
while ($recent_query->have_posts()): $recent_query->the_post();
get_template_part('src/components/c-article-item/view-blog-feed', null, ['show_excerpt' => false]);
endwhile;
wp_reset_postdata();
?>
</aside>

</article>
Expand Down
28 changes: 11 additions & 17 deletions public/app/themes/clarity/src/components/c-last-updated/view.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
<?php
$post_id = get_the_id();
$post_type = get_post_type($post_id);
$terms = get_the_terms($post_id, 'agency');
$post_id = get_the_id();
$post_type = get_post_type($post_id);
$terms = get_the_terms($post_id, 'agency');
$term_names = is_array($terms) ? array_map(fn($term) => $term->name, $terms) : null;
?>
<!-- c-last-updated starts here -->
<section class="c-last-updated">

<?php
// Remove last reviewed from blog posts as it is not a content item that needs to be reviewed
if (! is_singular('post')) :
?>
<?php
// Remove last reviewed from blog posts as it is not a content item that needs to be reviewed
if (! is_singular('post')) : ?>
<p><span class="c-share-post__meta__date">Last reviewed: <?= the_modified_date('j F Y') ?></span></p>
<?php endif; ?>
<?php endif; ?>

<p><span class="c-share-post__meta__date">Content tagged as:
<?php
if (is_array($terms)) {
foreach ($terms as $term) {
echo ' ' . $term->name . ', ';
}
}
?>
</span></p>
<p>
<span class="c-share-post__meta__date">Content tagged as: <?= join(', ', $term_names) ?></span>
</p>
</section>
<!-- c-last-updated ends here -->
12 changes: 6 additions & 6 deletions public/app/themes/clarity/src/components/c-news-article/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
$query_args = new SearchQueryArgs((new Agency())->getCurrentAgency()['wp_tag_id'], 'news', 1, 6, true);

// Run the query.
$query = new WP_Query($query_args->get());
$recent_query = new WP_Query($query_args->get());

?>

Expand All @@ -34,12 +34,12 @@
</section>

<aside class="l-secondary">

<h1 class="o-title">Recent news</h1>
<?php
echo '<h1 class="o-title">Recent news</h1>';
foreach ($query->posts as $key => $post) {
include locate_template('src/components/c-article-item/view-news-feed.php');
}
while ($recent_query->have_posts()): $recent_query->the_post();
get_template_part('src/components/c-article-item/view-news-feed', null, ['show_excerpt' => false]);
endwhile;
wp_reset_postdata();
?>

</aside>
Expand Down

0 comments on commit faadfcb

Please sign in to comment.