0

假设我想在 wordpress 网站的主索引上显示某个作者的帖子,我该怎么做?以下是二十三个主题的循环:

<?php
$curauth = (isset($_GET['liamhodnett'])) ? get_user_by('liamhodnett', $author) : get_userdata(intval($author));
?>

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <li>
        <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>">
        <?php the_title(); ?></a>,
        <?php the_time('d M Y'); ?> in <?php the_category('&');?>
    </li>

<?php endwhile; else: ?>
    <p><?php _e('No posts by this author.'); ?></p>

<?php endif; ?>
4

1 回答 1

1

基本上,你需要做的就是设置 $curauth

<?php
   $curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
?>

作者页面的示例: http: //codex.wordpress.org/Author_Templates

<?php get_header(); ?>

<div id="content" class="narrowcolumn">

<!-- This sets the $curauth variable -->

    <?php
    $curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
    ?>

    <h2>About: <?php echo $curauth->nickname; ?></h2>
    <dl>
        <dt>Website</dt>
        <dd><a href="<?php echo $curauth->user_url; ?>"><?php echo $curauth->user_url; ?></a></dd>
        <dt>Profile</dt>
        <dd><?php echo $curauth->user_description; ?></dd>
    </dl>

    <h2>Posts by <?php echo $curauth->nickname; ?>:</h2>

    <ul>
<!-- The Loop -->

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <li>
            <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>">
            <?php the_title(); ?></a>,
            <?php the_time('d M Y'); ?> in <?php the_category('&');?>
        </li>

    <?php endwhile; else: ?>
        <p><?php _e('No posts by this author.'); ?></p>

    <?php endif; ?>

<!-- End Loop -->

    </ul>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

编辑

我会解释$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));什么

isset($_GET['author_name']) ?检查 URL 中是否存在带有用户名的参数,例如: www.myexamplewebsite.com/author/danieltulp 它是 if/else 语句的简写

如果 URL 有用户名,代码将尝试获取它get_user_by('slug', $author_name)

如果不是,它将尝试使用get_userdata Wordpress函数获取它get_userdata(intval($author))

当然,你不是在 URL 中引用用户,所以你只需要像这样设置 currentauth:

$curauth = (is_home()) ? "liamhodnett" : (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));

编辑 2 如果一切都失败了(没有测试上面的代码,所以很可能),使用get_posts()进行自己的数据库调用:

$args = array(
    'author'        =>  1 // this should be your user ID, or use 'author_name' => 'liamhodnett' 
    );

// get my posts 'ASC'
$myposts = get_posts( $args );

然后将 $mypost 数组用于您的循环:

foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php the_content(); ?>
<?php endforeach; 
wp_reset_postdata();?>
于 2013-10-29T15:02:57.153 回答