繁体   English   中英

显示与某些作者相关的Wordpress帖子

[英]Show Wordpress Posts Associated with Certain Author

假设我想在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; ?>

基本上,您需要做的就是设置$ 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)); $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_user_by('slug', $author_name)来获取用户名

如果没有,它将尝试使用get_userdata(intval($author)) 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();?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM