Skip to content

Commit ccb3b52

Browse files
Fix Related Posts URL for see all posts (#2442)
- Added code to dynamically render the right URL for see all posts - News and Stories page is now rendered if availabe and in the right language
1 parent 0686d3a commit ccb3b52

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

functions.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,13 +302,25 @@ function render_related_posts_block(array $attributes): string
302302
// Encode the query attributes to JSON for the block template
303303
$query_json = wp_json_encode($attributes['query_attributes'], JSON_UNESCAPED_SLASHES);
304304

305+
// Dynamically render link to News & Stories page
306+
$news_stories_url = '';
307+
$news_stories_page = (int) get_option('page_for_posts');
308+
309+
if ($news_stories_page) {
310+
$news_stories_url = get_permalink($news_stories_page);
311+
}
312+
313+
$see_all_link_group = !empty($news_stories_url) ?
314+
'<!-- wp:navigation-link {"label":"' . __('See all posts', 'planet4-blocks') . '","url":"' . $news_stories_url . '","className":"see-all-link"} /-->'
315+
: '';
316+
305317
// Define the HTML output for the block
306318
$output = '<!-- wp:query ' . $query_json . ' -->
307319
<div class="wp-block-query posts-list p4-query-loop is-custom-layout-list"><!-- wp:group {"layout":{"type":"flex","justifyContent":"space-between"}} -->
308320
<div class="wp-block-group related-posts-block"><!-- wp:heading {"lock":{"move":true}} -->
309321
<h2 class="wp-block-heading">' . __('Related Posts', 'planet4-blocks') . '</h2>
310322
<!-- /wp:heading -->
311-
<!-- wp:navigation-link {"label":"' . __('See all posts', 'planet4-blocks') . '","url":"http://www.planet4.test/news-stories/","className":"see-all-link"} /-->
323+
' . $see_all_link_group . '
312324
</div>
313325
<!-- /wp:group -->
314326
<!-- wp:post-template {"lock":{"move":true,"remove":true}} -->
@@ -328,7 +340,8 @@ function render_related_posts_block(array $attributes): string
328340
<!-- /wp:group --></div>
329341
<!-- /wp:columns -->
330342
<!-- /wp:post-template -->
331-
<!-- wp:navigation-link {"label":"' . __('See all posts', 'planet4-blocks') . '","url":"http://www.planet4.test/news-stories/","className":"see-all-link"} /--></div>
343+
' . $see_all_link_group . '
344+
</div>
332345
<!-- /wp:query -->';
333346

334347
return do_blocks($output);

0 commit comments

Comments
 (0)