Skip to content

Commit

Permalink
Fix Related Posts URL for see all posts (#2442)
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
Osong-Michael authored Nov 15, 2024
1 parent 0686d3a commit ccb3b52
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,13 +302,25 @@ function render_related_posts_block(array $attributes): string
// Encode the query attributes to JSON for the block template
$query_json = wp_json_encode($attributes['query_attributes'], JSON_UNESCAPED_SLASHES);

// Dynamically render link to News & Stories page
$news_stories_url = '';
$news_stories_page = (int) get_option('page_for_posts');

if ($news_stories_page) {
$news_stories_url = get_permalink($news_stories_page);
}

$see_all_link_group = !empty($news_stories_url) ?
'<!-- wp:navigation-link {"label":"' . __('See all posts', 'planet4-blocks') . '","url":"' . $news_stories_url . '","className":"see-all-link"} /-->'
: '';

// Define the HTML output for the block
$output = '<!-- wp:query ' . $query_json . ' -->
<div class="wp-block-query posts-list p4-query-loop is-custom-layout-list"><!-- wp:group {"layout":{"type":"flex","justifyContent":"space-between"}} -->
<div class="wp-block-group related-posts-block"><!-- wp:heading {"lock":{"move":true}} -->
<h2 class="wp-block-heading">' . __('Related Posts', 'planet4-blocks') . '</h2>
<!-- /wp:heading -->
<!-- wp:navigation-link {"label":"' . __('See all posts', 'planet4-blocks') . '","url":"http://www.planet4.test/news-stories/","className":"see-all-link"} /-->
' . $see_all_link_group . '
</div>
<!-- /wp:group -->
<!-- wp:post-template {"lock":{"move":true,"remove":true}} -->
Expand All @@ -328,7 +340,8 @@ function render_related_posts_block(array $attributes): string
<!-- /wp:group --></div>
<!-- /wp:columns -->
<!-- /wp:post-template -->
<!-- wp:navigation-link {"label":"' . __('See all posts', 'planet4-blocks') . '","url":"http://www.planet4.test/news-stories/","className":"see-all-link"} /--></div>
' . $see_all_link_group . '
</div>
<!-- /wp:query -->';

return do_blocks($output);
Expand Down

0 comments on commit ccb3b52

Please sign in to comment.