• Resolved dukearmi

    (@dukearmi)


    I came up with the following code (based on your documentation and answers to previous support threads) to display the entry meta within a div (above the entry content and sidebars). It displays most of it, but not the author name or local avatar. Any idea why? I’d appreciate your help with this.

    <?php if (is_single()) : ?>
                            <div class="entry-meta">
                                <?php
                                $author_id = get_the_author_meta('ID');
                                $user_nicename = get_the_author_meta('user_nicename');
                                $avatar_filename = sanitize_file_name($user_nicename) . '.webp';
                                $upload_dir = wp_upload_dir(null, false);
                                $avatar_path = $upload_dir['basedir'] . '/avatars/' . $avatar_filename;
                                $avatar_url = $upload_dir['baseurl'] . '/avatars/' . $avatar_filename;
    
                                // Check if the local avatar file exists, otherwise use default avatar
                                $avatar_html = file_exists($avatar_path)
                                    ? sprintf('<img src="%s" alt="Author Avatar" width="96" height="96">', esc_url($avatar_url))
                                    : get_avatar($author_id, 48);
    
                                printf(
                                    '<div class="author-wrap">
                                        <span class="author-label">Written by</span>
                                        <a href="%1$s" title="%2$s" rel="author">
                                            <span class="author-name" itemprop="name">%3$s</span>
                                        </a>
                                    </div>
                                    <div class="author vcard">%4$s</div>',
                                    esc_url(get_author_posts_url($author_id)),
                                    esc_attr(sprintf(__('View all posts by %s', 'generatepress'), get_the_author())),
                                    esc_html(get_the_author()),
                                    $avatar_html
                                );
                                ?>
                                <?php
                                $time_string = '<span class="date-label">Published on</span><time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s</time>';
                                if ( get_the_date() !== get_the_modified_date() ) {
                                    $time_string = '<span class="date-label">Updated on</span><time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified">%4$s</time>';
                                }
    
                                $time_string = sprintf( $time_string,
                                    esc_attr( get_the_date( 'c' ) ),
                                    esc_html( get_the_date() ),
                                    esc_attr( get_the_modified_date( 'c' ) ),
                                    esc_html( get_the_modified_date() )
                                );
    
                                echo sprintf( '<span class="posted-on">%s</span> ', $time_string );
                                ?>
                            </div>
                        <?php endif; ?>

Viewing 9 replies - 1 through 9 (of 9 total)
  • ying

    (@yingscarlett)

    Hi there,

    The code should work. I just tested it.

    Are you using plugin to add author name and avatar?

    Thread Starter dukearmi

    (@dukearmi)

    I’m using WPCode to add it in a a custom PHP function. For some reason it’s working now. I may have opened too many windows with different CSS in each. Thank you!

    • This reply was modified 10 months, 2 weeks ago by dukearmi.
    • This reply was modified 10 months, 2 weeks ago by dukearmi.
    ying

    (@yingscarlett)

    No Problem ??

    Thread Starter dukearmi

    (@dukearmi)

    Hey, is it possible to make my code apply to the entry meta of posts in a category or author archive page? I’ve done but I had to duplicate the code without the “divs”

    • This reply was modified 10 months, 2 weeks ago by dukearmi.
    ying

    (@yingscarlett)

    Try removing the condition:

    if (is_single())
    Thread Starter dukearmi

    (@dukearmi)

    It restructed the entry meta within the custom page hero for the archive itself. I’d like it to work for the entry meta of each post in the category or author page. It seems like I have to duplicate near-identical code to do this.

    It seems like I have to duplicate near-identical code to do this.

    Sounds good.

    Please note that lots of your questions in the free theme support forum here are achievable with our GP Premium plugin so we cannot provide full solutions for everything requested.

    Thanks for your understanding.

    Thread Starter dukearmi

    (@dukearmi)

    Thank you, Leo.

    Please note that lots of your questions in the free theme support forum here are achievable with our GP Premium plugin so we cannot provide full solutions for everything requested.

    That’s why it will be the first thing I buy when my website starts making money ?? I love this theme!

    Sounds good.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Custom Entry Meta Error’ is closed to new replies.