Support Forum
Hi, Metabox.
I created a custom field for related posts via post advanced select but I having a problem showing the author's name and comment counts inside single blog post.
Want to display:
https://markuphero.com/share/dM0giAmjeFkEFdckpu2x
<?php
add_filter( 'rwmb_meta_boxes', 'your_prefix_function_name' );
function your_prefix_function_name( $meta_boxes ) {
$prefix = '';
$meta_boxes[] = [
'title' => __( 'Related Post', 'your-text-domain' ),
'id' => 'related-post',
'fields' => [
[
'name' => __( 'Post', 'your-text-domain' ),
'id' => $prefix . 'post_related',
'type' => 'post',
'post_type' => ['post'],
'field_type' => 'select_advanced',
'multiple' => true,
],
],
];
return $meta_boxes;
}
<div class="post-related">
{% for post in query.posts %}
{% for item in post.post_related %}
<div class="p2-post-related__card">
<h2 class="p2-post-related__header">
<h3 class="p2-post-related__category">{{ mb.get_the_term_list( item.ID, 'category', '', ', ' ) }}</h3>
<a href="{{ item.url }}" class="p2-post-related__title">{{ item.title }}</a>
</h2>
<div class="p2-post-related__meta">
<div class="p2-post-related_author_name">By <a href="#">Julia James</a></div>
<div class="p2-post-related_details"><span>{{ item.date | date( 'F j, Y' ) }}</span> · <span>6 comments</span></div>
</div>
<div>
<img src="{{ item.thumbnail.full.url }}" width="{{ item.thumbnail.full.width }}" height="{{ item.thumbnail.full.height }}" alt="{{ item.thumbnail.full.alt }}" class="p2-post-related__thumbnail">
</div>
</div>
{% endfor %}
{% endfor %}
</div>
<!-- post-related -->
Thank you.
Hello,
On the single post page, no need to use this loop
{% for post in query.posts %}
it should be used on the archive page to display posts.
Hi, Peter.
Thanks for your reply but my problem here is I don't know how I can add the author and comments count via views.
Hello,
The key point here is the post ID or post object, from this line
{% for item in post.post_related %}
you have the post object so you can get any post info with the WordPress function. Here are some examples with PHP code, then you can convert it to Twig code on your own.
https://stackoverflow.com/questions/3188901/wordpress-get-author-info-from-post-id
https://wordpress.stackexchange.com/questions/38753/how-to-get-comments-by-post-id