0

Hi everyone I am a sort of medium weight wp designer/developer and having trouble with a site I am working on. The demo page is here:

http://flock.simonpointer.com/blog/

I have inherited the following loop code in the template file, and cannot for the life of me figure out why I seem to be getting 2 outputs for the author info in the featured content box. I seem to get what appears to be a first and second post. On the second one the author photo and link details are not loading but they are on the first one and the post thumbnail is being displayed for the second one but not the first. Any help appreciated:

            <div class="col-2-5"><div class="feature-card blog-featured-card">
            <?php
                //$args = array( 'posts_per_page' => 1);
                $loop = new WP_Query(
                    array(
                        'post_type' => 'post',
                        'posts_per_page' => 1
                    ) );

                while ( $loop->have_posts() ) : $loop->the_post();

                    if(isset($post->author)){
                        $team_id = $post->author;//get the post id of the team indiviual detail page
                        $author_name = get_the_title( $team_id ); //get the page title from the id
                        $author_image = wp_get_attachment_url(get_post_meta($team_id, 'image', true));
                        $image = get_field('image', $team_id);
                        $author_link = get_permalink($team_id);

                        $page_elements = array(
                            'author_name' => $author_name,
                            'author_image' => $image['sizes']['thumbnail'],
                            'author_link' => $author_link,
                            );


                        $author_html = '<div class="blog-author">


                                <a href="'.$page_elements['author_link'].'" title="">
                                    <img src="'.$page_elements['author_image'].'" alt="'.$page_elements['author_name'].'" />
                                </a>

                                <span>By <a href="'.$page_elements['author_link'].'">'.$page_elements['author_name'].'</a></span>
                        </div>';
                    }


                    echo '<div class="blog-feature-go">';
                    echo '<p><a href="'.get_permalink().'" title="'.get_the_title().'">'.get_the_post_thumbnail().'</a></p>'; 
                    echo '<h2 class="test-headin"><a href="'.get_permalink().'" title="'.get_the_title().'">'.get_the_title().'</a></h2>';
                    echo $author_html;
                    echo '<div><a class="text-arrow-cta cta-button read-more-button" href="'.get_permalink().'" title="'.get_the_title().'">Read More</a></div>';
                    echo '</div>';

                endwhile;

            ?>
0

By inspecting your code, it appears those are two separate featured posts, rather than a single one with the author being duplicated.

The first one being: Is Marketing Operations The New Industry Buzzword? And the second one being: Why The CMO Is The CEO’s Super-Weapon

This might be trivial - but have you double checked that you have the same author under the second post? Basically your code gets the author by display name. That then goes into get_the_title... for the life of me I cannot see why you're doing that, rather than simply fetching the author object and outputting the image and other data via that.

See get_user_by()

https://developer.wordpress.org/reference/functions/get_user_by/

EDIT

Removed comment on how to output code.

3
  • echo is fine. no need to $output, this isn't a function. – rudtek Nov 30 '17 at 17:49
  • Thanks guys for the help. To be honest it’s not my code - I never made the original and just needed to try to figure it out. I think the author is the sam for both posts so your logic of listing author twice seems correct, and yes I think I’ll have a go at removing the if author part and just work with a std loop to pull the title author and featured image in. – simonpointer Dec 1 '17 at 11:26
  • I'd offer to write it up for you, but it would require access to your code (which isn't the smartest thing to send to some random bloke over the internet) lol Best of luck :) – Daniel Fonda Dec 1 '17 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.