【问题标题】:How to show WordPress author.php when user has no posts?当用户没有帖子时如何显示 WordPress author.php?
【发布时间】:2013-09-11 15:13:52
【问题描述】:

所以我一直在构建 author.php 页面来显示用户配置文件。

这依赖于顶部的 'if ( have_posts() ) :',其中 - 仅供参考 - 我也有...

<?php /* Queue the first post, that way we know * what author we're dealing with (if that is the case). * * We reset this later so we can run the loop * properly with a call to rewind_posts(). */ the_post(); ?>

<?php /* Since we called the_post() above, we need to * rewind the loop back to the beginning that way * we can run the loop properly, in full. */ rewind_posts(); ?>

如果有问题的用户他/她的名字的任何帖子,那很好。 但是,当他们的帖子为零时,个人资料页面在应该有内容的地方出现空白。

我该如何克服这个问题?

仅供参考,完整的 author.php 是...

`

    <?php if ( have_posts() ) : ?>

        <?php
            /* Queue the first post, that way we know
             * what author we're dealing with (if that is the case).
             *
             * We reset this later so we can run the loop
             * properly with a call to rewind_posts().
             */
            the_post();
        ?>

        <?php
            /* Since we called the_post() above, we need to
             * rewind the loop back to the beginning that way
             * we can run the loop properly, in full.
             */
            rewind_posts();
        ?>

        <?php
        /* Get Extra User Fields*/
        $values_by_name = array(  // Assign defaults to all CIMY fields
            'TITLEJOB' => '',
            'TITLESECONDARY' => '',
            'COMPANY' => '',
        );
        $values = get_cimyFieldValue($author, false);
        if ($values) {
            foreach ($values as $value) {
                $values_by_name[$value['NAME']] = cimy_uef_sanitize_content($value['VALUE']);
            }
        }
        ?>

        <h1><?php the_author(); ?></h1>

        <?php
        if(!empty($values_by_name['COMPANY'])) {
            echo '<p>Company: '.$values_by_name['COMPANY'].'</p>';
        }

        if(!empty($values_by_name['TITLEJOB'])) {
            echo '<p>Title: '.$values_by_name['TITLEJOB'].'</p>';

            if(!empty($values_by_name['TITLESECONDARY'])) {
                echo '<p>Secondary: '.$values_by_name['TITLESECONDARY'].'</p>';
            }

        }
        ?>

        <?php

        ?>

        <p><?php echo get_avatar( get_the_author_meta( 'user_email' )); ?></p>
        <p>URL: <a href="<?php the_author_meta( 'user_url' ); ?>"><?php the_author_meta( 'user_url' ); ?></a></p>
        <p>Description: <?php echo nl2br(get_the_author_meta( 'description' )); ?></p>

        <h4>Posts: <?php the_author_posts(); ?></h4>
        <?php /* Start the Loop */ ?>
        <ul>
        <?php while ( have_posts() ) : the_post(); ?>
            <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
        <?php endwhile; ?>
        </ul>

        <h4>Topics</h4>
        <ul>
            <li>Topic</li>
            <li>Topic</li>
            <li>Topic</li>
        </ul>

        <h4>Companies</h4>
        <ul>
            <li>Company</li>
            <li>Company</li>
            <li>Company</li>
        </ul>

    <?php endif; ?>

`

【问题讨论】:

  • 有一个插件可以解决这个问题,但它在我的网站上不起作用。代码运行...if (!function_exists('show_authors_without_posts')) { function show_authors_without_posts($template) { global $wp_query; if( !is_author() &amp;&amp; get_query_var('author') &amp;&amp; (0 == $wp_query-&gt;posts-&gt;post) ) { // debug // echo 'Overwrite default 404 template...'; return get_author_template(); } return $template; } add_filter('404_template', 'show_authors_without_posts'); }这有什么问题需要更改才能为我工作吗?

标签: php wordpress profile author


【解决方案1】:

您不能自己查询吗?

haveno_posts{

    If Posts = 0 then return true?

}

<?php if ( have_posts() ) : ?>

应该是

<?php if ( have_posts() || haveno_posts() ) : ?>

或者:

http://wordpress.org/plugins/show-authors-without-posts/admin/

https://wordpress.stackexchange.com/questions/41078/show-author-archive-pages-for-authors-with-no-posts

【讨论】:

  • 我认为这与循环内部/外部可以访问哪些值有关(?)。该插件对我不起作用;我给开发者留言。我查看了讨论的指导,基本上是关于使用 get_userdata。我可以使用 get_userdata 访问的完整值列表是什么?因为到目前为止,我看不到我可以提取与当前方法等效的值,包括头像。
  • 完整列表在数据库本身中,我假设在_users中
【解决方案2】:

您已将整个页面内容包装在条件中:

if ( have_posts() ) :

按理说,如果用户没有帖子,它会跳到底部并且什么都不显示...

【讨论】:

  • 没有这个,零帖子作者的作者页面缺少作者姓名、头像和 URL 字段等,并且在一种情况下会抛出一个奇怪的帖子计数“9”。
  • 也就是说,删除“if”子句会显示一些字段——无论是有帖子的作者还是没有帖子的作者。现在,对于以前拥有完整个人资料页面的帖子作者,缺少信息。我错过了一些关于我可以在外部/内部访问这些信息的方式,我认为......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-02
  • 1970-01-01
  • 2012-03-16
  • 2017-09-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多