How to link the whole list item in Gutenberg’s latest posts block?


I am using a Gutenberg’s ‘Latest Posts’ block on my front page. Featured image display and grid layout are enabled in the block settings.

By default, only the post title is linked to the actual post. However, users are tempted to click on the post image or even somwhere on the excerpt in order to get to the respective article.

So, how can I apply a filter to the block output, which adds a link to the whole list item rather than just the post title? I’m looking for something like this:

function my_post_item( $post ){
  // Fetch the link
  $post_url = get_permalink( $post );
  // Wrap anchor tag around list item's content
  $list_item = '<li><a href="' . $post_url . '">';
  $list_item .= $default_latest_post_item_content;
  $list_item .= '</a></li>';

  return $list_item;
add_filter( 'gutenberg_latest_post_item', 'my_post_item' );

Any ideas?

, , Bunjip 3 years 2020-07-10T08:11:04-05:00 0 Answers 63 views 0

Leave an answer