Issues displaying blog post author name and comment counts

Support MB Views Issues displaying blog post author name and comment counts

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #39066
    JamesJames
    Participant

    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.

    #39108
    PeterPeter
    Moderator

    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.

    #39115
    JamesJames
    Participant

    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.

    #39128
    PeterPeter
    Moderator

    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

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.