Skip to content

Commit 4b1c299

Browse files
committed
WDSBT-23 - Add related posts pattern and primary category pattern
1 parent 4cf83c9 commit 4b1c299

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

patterns/primary-category.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* Title: Primary Category
4+
* Slug: wdsbt/primary-category
5+
* Categories: posts
6+
* Block Types: custom/primary-category
7+
* Inserter: false
8+
*
9+
* @package wdsbt
10+
*/
11+
12+
$wdsbt_category = get_the_category();
13+
14+
if ( $wdsbt_category ) {
15+
// Initialize variables.
16+
$wdsbt_category_display = '';
17+
$wdsbt_category_link = '';
18+
19+
// Get primary category if available.
20+
if ( class_exists( 'WPSEO_Primary_Term' ) ) {
21+
$wdsbt_primary_term = new WPSEO_Primary_Term( 'category', get_the_id() );
22+
$wdsbt_primary_term = $wdsbt_primary_term->get_primary_term();
23+
$wdsbt_term = get_term( $wdsbt_primary_term );
24+
25+
// Check if primary term exists.
26+
if ( ! is_wp_error( $wdsbt_term ) ) {
27+
$wdsbt_category_display = $wdsbt_term->name;
28+
$wdsbt_category_link = get_category_link( $wdsbt_term->term_id );
29+
}
30+
}
31+
32+
// If primary term not found, use the first category.
33+
if ( empty( $wdsbt_category_display ) && isset( $category[0] ) ) {
34+
$wdsbt_category_display = $category[0]->name;
35+
$wdsbt_category_link = get_category_link( $category[0]->term_id );
36+
}
37+
38+
// Display category if available.
39+
if ( ! empty( $wdsbt_category_display ) ) {
40+
?>
41+
<h2 class="wp-block-heading has-large-font-size" style="padding-top: var(--wp--preset--spacing--20); padding-bottom: var(--wp--preset--spacing--20);">
42+
More <a href="<?php echo esc_url( $wdsbt_category_link ); ?>"><?php echo esc_html( $wdsbt_category_display ); ?></a>
43+
</h2>
44+
<?php
45+
}
46+
}

patterns/related-posts.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Title: Related Posts
4+
* Slug: wdsbt/related-posts
5+
* Categories: posts
6+
* Block Types: custom/related-posts
7+
* Inserter: false
8+
*
9+
* @package wdsbt
10+
*/
11+
12+
?>
13+
14+
<!-- wp:group {"queryId":8,"tagName":"aside","metadata":{"name":"Recent Posts"},"style":{"spacing":{"margin":{"top":"var:preset|spacing|40"},"padding":{"bottom":"var:preset|spacing|50"}}},"layout":{"type":"constrained"}} -->
15+
<aside class="wp-block-group" style="margin-top:var(--wp--preset--spacing--40);padding-bottom:var(--wp--preset--spacing--50)">
16+
17+
<!-- wp:query {"queryId":0,"query":{}} -->
18+
<div class="wp-block-query">
19+
20+
<!-- wp:pattern {"slug":"wdsbt/primary-category"} /-->
21+
22+
<!-- wp:post-template {"className":"related-posts-query","layout":{"type":"grid","columnCount":3}} -->
23+
<!-- wp:post-featured-image {"isLink":true} /-->
24+
<!-- wp:post-terms {"term":"category"} /-->
25+
<!-- wp:post-title {"isLink":true,"fontSize":"small"} /-->
26+
<!-- wp:read-more {"content":""} /-->
27+
<!-- /wp:post-template -->
28+
29+
</div>
30+
<!-- /wp:query -->
31+
32+
</aside>
33+
<!-- /wp:group -->

templates/single.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@
9494

9595
</div>
9696
<!-- /wp:group -->
97+
98+
<!-- wp:pattern {"slug":"wdsbt/related-posts"} /-->
9799
</div>
98100
<!-- /wp:group -->
99101
</main>

0 commit comments

Comments
 (0)