Skip to content
This repository has been archived by the owner on Jun 27, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2 from helsingborg-stad/feature/Anvnd-emblem-p-po…
Browse files Browse the repository at this point in the history
…ster-utan-bild-CU-865c063k9

     * If the post has no featured image and it's being displayed on a page for a term      * then use the emblem as the featured image
  • Loading branch information
annalinneajohansson committed Mar 29, 2023
2 parents 44d438a + bd898b9 commit 8b452a9
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions source/php/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public function __construct()
add_action('template_redirect', [$this,'redirectTermToPageForTerm']);

add_action('init', [$this, 'setupCustomColumns']);

add_filter('post_thumbnail_id', [$this, 'fallbackFeaturedImageToEmblem'], 10, 2);
}

public function loadPageForTermPostTypes($field)
Expand Down Expand Up @@ -209,4 +211,28 @@ public function setupSecondaryQuery($query)

$query->set('secondaryQuery', $secondaryQuery);
}

/**
* If the post has no featured image and it's being displayed on a page for a term
* then use the emblem as the featured image
*
* @note $post is the post object the featured image belongs to
* whilst get_queried_object_id() is the id of post/page currently being displayed as the main query.
*
* @param attachmentId The attachment ID of the featured image.
* @param post The post object.
*
* @return The attachment ID of the featured image.
*/
public function fallbackFeaturedImageToEmblem($attachmentId, $post)
{
if (0 == $attachmentId && !empty($this->isPageForTerm(get_queried_object_id()))) {
$attachmentId = attachment_url_to_postid(get_theme_mod('logotype_emblem'));
}
return $attachmentId;
}
public function isPageForTerm(int $postId = 0)
{
return get_field('is_page_for_term', $postId);
}
}

0 comments on commit 8b452a9

Please sign in to comment.