Skip to content

Commit

Permalink
Merge pull request #12 from ItinerisLtd/use-same-function-for-getting…
Browse files Browse the repository at this point in the history
…-pageid-for-breadcrumbs

Use same function for getting pageid for breadcrumbs
  • Loading branch information
danlapteacru authored Dec 16, 2022
2 parents 60fa695 + 7532a2f commit 8fe5ba9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
29 changes: 26 additions & 3 deletions src/Types/AbstractType.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,39 @@ public static function pageSelector(array $args)
);
}

/**
* Get page ID for current model.
*
* @return int
*/
public function getPageId(): int
{
if (null === $this->customPageId) {
$this->customPageId = (int) get_option("page_for_{$this->fieldName}", 0);
$this->customPageId = $this->getPageIdByPostType($this->fieldName);
}

return $this->customPageId ?? 0;
}

/**
* Get page ID by post type.
*
* @param string $postType
* @return int
*/
public function getPageIdByPostType(string $postType): int
{
$option = "page_for_{$postType}";
if ('post' === $postType) {
$option = 'page_for_posts';
}

$pageId = (int) get_option($option, 0);

return apply_filters(
'itineris/page-as-post-type-archive/get_page_id',
($this->customPageId ?? 0),
$this->fieldName
$pageId,
$postType
);
}

Expand Down
7 changes: 1 addition & 6 deletions src/Types/CustomPostType.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,7 @@ public function breadcrumbLinks(array $links): array

$post_type = get_post_type();

if ('post' === $post_type) {
$archive_page_id = (int) get_option('page_for_posts', 0);
} else {
$archive_page_id = (int) get_option("page_for_{$post_type}", 0);
}

$archive_page_id = $this->getPageIdByPostType($post_type);
if (0 === $archive_page_id) {
return $links;
}
Expand Down

0 comments on commit 8fe5ba9

Please sign in to comment.