Skip to content

Commit

Permalink
feat: add related posts to single post page
Browse files Browse the repository at this point in the history
  • Loading branch information
joaquimds committed Oct 3, 2023
1 parent 648c6eb commit f7ea66b
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 48 deletions.
58 changes: 26 additions & 32 deletions web/app/themes/awasqa/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,18 @@ function awasqa_carbon_get_post_meta($post_id, $name, $container_id = '')
Field::make('text', 'source_url', 'Source URL')->set_attribute('type', 'url'),
));

Container::make('post_meta', 'Related Posts')
->where('post_type', '=', 'post')
->add_fields(array(
Field::make('association', 'related_posts', 'Posts')
->set_types([
[
'type' => 'post',
'post_type' => 'post'
]
])
));

Container::make('post_meta', 'Contact')
->where('post_type', '=', 'awasqa_organisation')
->add_fields(array(
Expand Down Expand Up @@ -431,38 +443,6 @@ function get_issue_options()
<?php
});

Block::make(__('Organisations Column'))
->set_icon('networking')
->add_fields(array(
Field::make('separator', 'crb_separator', __('Organisations Column'))
))
->set_render_callback(function ($fields, $attributes, $inner_blocks) {
$post = get_post();
$authors = get_coauthors($post->ID);
if (!$authors) {
return;
}
$orgs_data = [];
foreach ($authors as $author) {
$orgs = get_author_organisations($author->ID);
foreach ($orgs as $org) {
$orgs_data[] = [

];
}
}
?>
<div class="awasqa-authors-column">
<h6><?= __('Authors') ?></h6>
<?php
foreach ($authors_data as $author_data) {
render_author_column($author_data, show_visit_link: true);
}
?>
</div>
<?php
});

Block::make(__('Account Details Form'))
->set_icon('admin-users')
->add_fields(array(
Expand Down Expand Up @@ -594,6 +574,20 @@ function get_issue_options()
$query['author'] = $author->ID;
}
}
// Display related posts on single post page
if (($query['s'] ?? '') === ':related') {
$query['s'] = '';
$post_ids = [];
$related_posts = awasqa_carbon_get_post_meta($post->ID, 'related_posts') ?? [];
foreach ($related_posts as $related_post) {
$post_ids[] = $related_post['id'];
}
if (!$post_ids) {
$post_ids = [0];
}
$query['post__in'] = $post_ids;
$query['ignore_sticky_posts'] = 1;
}
return $query;
});

Expand Down
7 changes: 7 additions & 0 deletions web/app/themes/awasqa/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,12 @@ document.querySelectorAll(".wp-block-post-date").forEach(block => {
block.innerText = (new Date(datetime)).toLocaleDateString(LOCALE, { month: "short", day: "numeric", year: "numeric" })
})

document.querySelectorAll('.awasqa-related-posts').forEach(block => {
const posts = block.querySelectorAll('.wp-block-post')
if (!posts.length) {
block.style.display = 'none'
}
})

// Display content (hidden by pre-script.js)
document.body.style.visibility = "visible"
20 changes: 13 additions & 7 deletions web/app/themes/awasqa/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ h6::after {
.awasqa-authors-column {
margin-bottom: 5.625rem;
}
.awasqa-authors-column h6 {
margin-top: 0;
}
.awasqa-authors-column .awasqa-author-column {
margin-bottom: var(--wp--style--block-gap);
}
.awasqa-authors {
display: flex;
list-style: none;
Expand Down Expand Up @@ -207,17 +213,13 @@ h6::after {
footer .wp-block-image img {
vertical-align: middle;
}
/* Prevent gap between single post background color and footer */
.single-post footer {
margin-top: 0;
}
.awasqa-heading-link {
.awasqa-heading-link a {
align-items: center;
color: var(--color-green-lighter);
display: inline-flex;
text-decoration: none;
}
.awasqa-heading-link::after {
.awasqa-heading-link a::after {
content: "";
background-image: url("/app/themes/awasqa/assets/images/arrow-right-lighter.svg");
background-position: center;
Expand All @@ -226,7 +228,7 @@ footer .wp-block-image img {
margin-left: 0.5rem;
width: 1rem;
}
.awasqa-heading-link:hover,.awasqa-heading-link:focus {
.awasqa-heading-link a:hover,.awasqa-heading-link a:focus {
text-decoration: underline;
}
.awasqa-issue-link {
Expand Down Expand Up @@ -460,3 +462,7 @@ footer .wp-block-image img {
background-color: var(--color-green-lightest);
color: var(--color-green);
}
.awasqa-related-posts {
margin-bottom: var(--wp--preset--spacing--50);
margin-top: -0.75rem;
}
1 change: 1 addition & 0 deletions web/app/themes/awasqa/styles/awasqa.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
@import "blocks/post-heading.css";
@import "blocks/post-source.css";
@import "blocks/radical-resilience.css";
@import "blocks/related-posts.css";
10 changes: 9 additions & 1 deletion web/app/themes/awasqa/styles/blocks/authors-column.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
.awasqa-authors-column {
margin-bottom: 5.625rem;
}

h6 {
margin-top: 0;
}

.awasqa-author-column {
margin-bottom: var(--wp--style--block-gap);
}
}
7 changes: 0 additions & 7 deletions web/app/themes/awasqa/styles/blocks/footer.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,3 @@ footer {
}
}
}

/* Prevent gap between single post background color and footer */
.single-post {
footer {
margin-top: 0;
}
}
2 changes: 1 addition & 1 deletion web/app/themes/awasqa/styles/blocks/heading-link.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.awasqa-heading-link {
.awasqa-heading-link a {
@apply --link-arrow;
}
4 changes: 4 additions & 0 deletions web/app/themes/awasqa/styles/blocks/related-posts.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.awasqa-related-posts {
margin-bottom: var(--wp--preset--spacing--50);
margin-top: -0.75rem;
}

0 comments on commit f7ea66b

Please sign in to comment.