无法在循环之外获取作者信息

时间:2014-06-26 作者:Desi

我在循环外有一个author框,用来抓取作者的名字,但很可能不会显示正确的作者,因为这些标记只在循环内起作用。我可以在这段代码中更改什么,使其在循环之外工作?

    <div id="author-bio">
        <div class="inner">
            <h3>About <?php echo get_the_author(); ?></h3>
            <?php echo get_avatar( get_the_author_meta( \'user_email\' ), 70 ); ?>
            <div class="author-info">
                <p><?php the_author_meta( \'description\' ); ?></p>
                <p><a class="author-link" href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( \'ID\' ) ) ); ?>" rel="author">View all posts by <?php echo get_the_author(); ?> <span class="meta-nav">&rarr;</span></a></p>
            </div>
        </div>
    </div>
单件的完整代码。php如下:

<?php get_header(); ?>

    <!--BEGIN #content -->
    <div id="content">

        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

        <!--BEGIN .hentry -->
        <div <?php post_class(); ?> id="post-<?php the_ID(); ?>">

            <!--BEGIN .post-header-->
            <div class="post-header">

                <div class="inner">

                    <span class="meta-category"><?php the_category(\', \'); ?></span>

                    <h1 class="post-title">

                        <?php the_title(); ?>

                        <?php $format = get_post_format(); ?>
                        <?php if ($format == "image" || $format == "gallery" || $format == "video") : ?>
                        <span class="icon"><img src="<?php echo get_template_directory_uri(); ?>/images/icon-<?php echo $format; ?>.png" alt="<?php echo $format; ?>" /></span>
                        <?php endif; ?>

                    </h1>

                    <span class="meta-published"><?php _e(\'Posted\', \'engine\') ?> <?php echo human_time_diff( get_the_time(\'U\'), current_time(\'timestamp\') ) . \' \'.  __(\'ago\', \'engine\'); ?></span>
                    <span class="meta-author"><?php _e(\'by\', \'engine\') ?> <?php the_author_posts_link(); ?> <a class="post-twitter" href="http://twitter.com/<?php the_author_meta(\'twitter\') ?>" target="_blank"><img src="<?php echo get_stylesheet_directory_uri(); ?>/images/twitter-icon.png" /></a></span>

                </div>

            <!--END .post-header -->
            </div>

            <!--BEGIN .featured-image -->
            <div class="featured-image <?php echo get_post_format(); ?>">

                <?php if (get_post_format() == \'video\' && get_post_meta(get_the_ID(), \'dt_video\', true) != \'\') : ?>

                    <?php
                    global $wp_embed;
                    $video_url = get_post_meta(get_the_ID(), \'dt_video\', true);
                    $video_embed = $wp_embed->run_shortcode(\'[embed width="700"]\'.$video_url.\'[/embed]\');
                    ?>
                    <div id="video-<?php the_ID(); ?>"><?php echo $video_embed; ?></div>

                <?php elseif (get_post_format() == \'gallery\') : ?>

                    <!--BEGIN #slides -->
                    <div id="single-slides" class="clearfix">

                        <?php

                        $args = array(
                            \'orderby\'        => \'menu_order\',
                            \'order\'          => \'ASC\',
                            \'post_type\'      => \'attachment\',
                            \'post_parent\'    => get_the_ID(),
                            \'post_mime_type\' => \'image\',
                            \'post_status\'    => null,
                            \'numberposts\'    => -1,
                        );

                        $attachments = get_posts($args);

                        ?>

                        <?php if ($attachments) : ?>

                        <div class="slides_container">

                        <?php foreach ($attachments as $attachment) : ?>

                            <?php
                                $format = get_post_format();
                                $src = wp_get_attachment_image_src( $attachment->ID, array( \'9999\',\'9999\' ), false, \'\' );
                                $src = $src[0];
                                $image = dt_resize($attachment->ID, $src, 700, \'\', true);
                                $caption = $attachment->post_excerpt;
                            ?>

                            <div>

                                <span class="overlay-icon overlay-<?php echo $format; ?>"><a rel="group-<?php the_ID(); ?>" title="<?php echo $attachment->post_title; ?>" class="colorbox-<?php echo $format; ?>" href="<?php echo $src; ?>"></a></span>

                                <img
                                height="<?php echo $image[\'height\']; ?>"
                                width="<?php echo $image[\'width\']; ?>"
                                alt="<?php echo apply_filters(\'the_title\', $attachment->post_title); ?>"
                                src="<?php echo $image[\'url\']; ?>"
                                />

                                <?php if($caption != \'\') : ?>
                                <span class="caption"><?php echo $caption; ?></span>
                                <?php endif; ?>

                            </div>

                        <?php endforeach; ?>

                        </div>

                        <!--BEGIN .slide-cntrols-->
                        <div id="slide-controls">

                            <a href="#" class="next">Next</a>
                            <a href="#" class="prev">Prev</a>

                        <!--END .slide-cntrols-->
                        </div>

                        <?php endif; ?>

                    <!--END #slides -->
                    </div>

                <?php elseif (has_post_thumbnail() && get_option(\'dt_blog_image\') != \'false\') : ?>

                    <?php 
                    $thumb = get_post_thumbnail_id(get_the_ID());
                    $image = dt_resize( $thumb, \'\', 700, \'\', true );
                    echo \'<img src="\'.$image[\'url\'].\'" width="\'.$image[\'width\'].\'" height="\'.$image[\'height\'].\'" alt="" />\';
                    ?>

                <?php endif; ?>

            <!--END .featured-image -->
            </div>

            <!--BEGIN .post-content -->
            <div class="post-content">
                <?php the_content(); ?>
            <!--END .post-content -->
            </div>

            <!--BEGIN .post-footer-->
            <div class="post-footer">

                <span class="meta-published"><?php echo human_time_diff( get_the_time(\'U\'), current_time(\'timestamp\') ) . \' \'.  __(\'ago\', \'engine\'); ?></span>

                <span class="meta-comments"><?php comments_number(__(\'No Comments\', \'engine\'), __(\'1 Comment\', \'engine\'), __(\'% Comments\', \'engine\')); ?></span>

            <!--END .post-footer-->
            </div>

        <!--END .hentry-->
        </div>

        <div id="next-prev-posts">
            <?php
                $prevPost = get_previous_post(); 
                if(isset($prevPost->ID))
                $prevthumbnail = get_the_post_thumbnail($prevPost->ID, array(100,100) );
                $nextPost = get_next_post(); 
                if(isset($nextPost->ID))
                $nextthumbnail = get_the_post_thumbnail($nextPost->ID, array(100,100) ); 
                $default = \'<img src="\'.get_stylesheet_directory_uri().\'/images/isa.jpg" />\';
            ?>

            <div class="inner">
                <div class="nav-next">
                    <?php
                        if ($nextPost && \'\' != get_the_post_thumbnail($nextPost->ID, array(100,100))) { 
                            next_post_link(\'%link\', $nextthumbnail, false); 
                            next_post_link(\'%link\', \'<span class="meta-nav">«</span> %title\', false); 
                        } elseif ($nextPost) {
                            next_post_link(\'%link\', $default, false); 
                            next_post_link(\'%link\', \'<span class="meta-nav">«</span> %title\', false);
                        } else {
                            print \'<p>\' . __( \'You are at the beginning!\' ) . \'</p>\';
                        }   
                    ?>
                </div>
                <div class="nav-previous">
                    <?php
                        if ($prevPost && \'\' != get_the_post_thumbnail($prevPost->ID, array(100,100))) { 
                            previous_post_link(\'%link\', \'%title <span class="meta-nav">»</span> \', false);
                    previous_post_link(\'%link\', $prevthumbnail, false); 
                        } elseif ($prevPost) {
                    previous_post_link(\'%link\', \'%title <span class="meta-nav">»</span> \', false);
                            previous_post_link(\'%link\', $default, false);      
                        } else {
                            print \'<p>\' . __( \'You are at the end!\' ) . \'</p>\';
                        }   
                    ?>
                </div>
            </div>
        </div> 

        <!--BEGIN #related--><?php $related = get_option(\'dt_related\');

        if($related == \'true\') : ?>


        <div id="related" class="clearfix">
            <div class="inner">
                <h3>Recommended</h3>

                <?php

                $related = dt_get_posts_related_by_taxonomy(get_the_ID(), \'category\', get_the_ID());

                ?>

                <ul>

                    <?php  if($related->have_posts()) : while ($related->have_posts()) : $related->the_post(); ?>

                    <!--BEGIN li -->
                    <li id="featured-post-<?php the_ID(); ?>">

                        <div class="featured-image">
                            <div class="da-hover">
                                <span class="da-wrap">
                                    <span class="title"><?php the_title(); ?></span>
                                    <?php dt_overlay_icon(); ?>
                                </span>
                            </div>
                            <a href="<?php the_permalink(); ?>"><?php dt_image(200, \'\'); ?></a>
                        <!--END .featured-image -->

                    <!--END li-->
                    </li>

                    <?php endwhile;  else: ?>
                        <p><?php _e(\'There are no related posts.\', \'engine\'); ?></p>
                    <?php endif; ?>

                </ul>

            </div>
        </div>

        <?php endif; ?><!--END #related-->

        <div id="author-bio">
            <div class="inner">
                <h3>About <?php echo get_the_author(); ?></h3>
                <?php echo get_avatar( get_the_author_meta( \'user_email\' ), 70 ); ?>
                <div class="author-info">
                    <p><?php the_author_meta( \'description\' ); ?></p>
                    <p><a class="author-link" href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( \'ID\' ) ) ); ?>" rel="author">View all posts by <?php echo get_the_author(); ?> <span class="meta-nav">&rarr;</span></a></p>
                </div>
            </div>
        </div>

        <div id="comments">
            <div class="inner">
                <div id="disqus_thread"></div>
                    <script type="text/javascript">
                    /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
                    var disqus_shortname = \'isatv\'; // required: replace example with your forum shortname

                    /* * * DON\'T EDIT BELOW THIS LINE * * */
                    (function() {
                        var dsq = document.createElement(\'script\'); dsq.type = \'text/javascript\'; dsq.async = true;
                        dsq.src = \'//\' + disqus_shortname + \'.disqus.com/embed.js\';
                        (document.getElementsByTagName(\'head\')[0] || document.getElementsByTagName(\'body\')[0]).appendChild(dsq);
                    })();
                    </script>
                    <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
            </div>
        </div>

        <?php endwhile; else : ?>

            <p><?php _e(\'No posts found\', \'engine\'); ?></p>

        <?php endif; ?>

    </div><!-- #content -->

<?php get_sidebar(); ?>

<?php get_footer(); ?>

1 个回复
最合适的回答,由SO网友:Pieter Goosen 整理而成

要使“作者”框正常工作,它必须位于循环内。但第二个循环有一个基本缺陷。您当前在一个中使用两个不同的循环,第一个是主循环,第二个是自定义循环以获取相关帖子

就其现状而言,以及您放置作者框的位置,它是从second 循环,而不是主循环,这就是为什么返回的作者是错误的

要解决此问题,需要重置第二个循环。始终重置使用创建的自定义查询(如您使用的查询)get_postsWP_Query. 要这样做,你必须打电话wp_reset_postdata 循环完成后。您的第二个循环应该如下所示

<!--BEGIN #related--><?php $related = get_option(\'dt_related\');

    if($related == \'true\') : ?>


    <div id="related" class="clearfix">
        <div class="inner">
            <h3>Recommended</h3>

            <?php

            $related = dt_get_posts_related_by_taxonomy(get_the_ID(), \'category\', get_the_ID());

            ?>

            <ul>

                <?php  if($related->have_posts()) : while ($related->have_posts()) : $related->the_post(); ?>

                <!--BEGIN li -->
                <li id="featured-post-<?php the_ID(); ?>">

                    <div class="featured-image">
                        <div class="da-hover">
                            <span class="da-wrap">
                                <span class="title"><?php the_title(); ?></span>
                                <?php dt_overlay_icon(); ?>
                            </span>
                        </div>
                        <a href="<?php the_permalink(); ?>"><?php dt_image(200, \'\'); ?></a>
                    <!--END .featured-image -->

                <!--END li-->
                </li>

                <?php endwhile;  wp_reset_postdata(); else: ?>
                    <p><?php _e(\'There are no related posts.\', \'engine\'); ?></p>
                <?php endif; ?>

            </ul>

        </div>
    </div>

    <?php endif; ?><!--END #related-->

结束

相关推荐

Hide all posts by an author

我们有一个名为“技术”的类别,希望在此页面上隐藏特定作者的帖子。例如,约翰·史密斯的帖子不会显示在科技类,或任何与此相关的类别,甚至搜索中。我该怎么做?我不想隐藏作者姓名,我想从搜索、分类、分类页面等中完全隐藏作者X的所有帖子。非常感谢。