我找到了两位代码。一个给我不超过一个月的帖子,另一个给我一个作者列表及其自定义字段、名称等。
有人能帮我把两者结合起来吗。一个运行在wordpress循环中,带有一个自定义查询,另一个是foreach(所有我不太了解的东西)。我怎样才能让这两个人一起工作。
谢谢你的帮助。
<?php
$display_admins = false;
$order_by = \'display_name\';
$role = \'\'; // \'subscriber\', \'contributor\', \'editor\', \'author\' - leave blank for \'all\'
$hide_empty = false;
if(!empty($display_admins)) {
$blogusers = get_users(\'orderby=\'.$order_by.\'&role=\'.$role);
} else {
$admins = get_users(\'role=administrator\');
$exclude = array();
foreach($admins as $ad) {
$exclude[] = $ad->ID;
}
$exclude = implode(\',\', $exclude);
$blogusers = get_users(\'exclude=\'.$exclude.\'&orderby=\'.$order_by.\'&role=\'.$role);
}
$authors = array();
foreach ($blogusers as $bloguser) {
$user = get_userdata($bloguser->ID);
if(!empty($hide_empty)) {
$numposts = count_user_posts($user->ID);
if($numposts < 1) continue;
}
$authors[] = (array) $user;
}
<?php
foreach($authors as $author) {
$display_name = $author[\'data\']->display_name;
$main_profile = get_the_author_meta(\'mainProfile\', $author[\'data\']->ID);
$hover_profile = get_the_author_meta(\'hoverProfile\', $author[\'data\']->ID);
$author_profile_url = get_author_posts_url($author[\'ID\']);
?>
<div class="da-author">
<div class="original-image">
<img src="<?php echo $main_profile; ?>" alt="<?php echo $display_name; ?>">
</div>
<div class="hover-image">
<a href="<?php echo $author_profile_url; ?>">
<img src="<?php echo $hover_profile; ?>">
</a>
</div>
</div>
<?php}?>
回路:
<?php
$args = array(
\'showposts\' => 1,
\'orderby\' => \'date\',
\'date_query\' => array(
array(
\'after\' => array(
\'year\' => date( "Y" ),
\'month\' => date( "m", strtotime( "-1 Months" ) ),
\'day\' => date( "t", strtotime( "-1 Months" ) ),
),
\'inclusive\' => true,
)
)
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<a href="<?php echo the_permalink(); ?>"><?php echo get_the_title(); ?></a>
<?php }} ?>
最合适的回答,由SO网友:s_ha_dum 整理而成
您的查询需要包含一个参数来指定作者,并且需要位于“author”循环中。事实上,我认为没有必要重复您的作者数据两次。一次就可以了。
$display_admins = false;
$order_by = \'display_name\';
$role = \'\'; // \'subscriber\', \'contributor\', \'editor\', \'author\' - leave blank for \'all\'
$hide_empty = false;
if(!empty($display_admins)) {
$blogusers = get_users(\'orderby=\'.$order_by.\'&role=\'.$role);
} else {
$admins = get_users(\'role=administrator\');
$exclude = array();
foreach($admins as $ad) {
$exclude[] = $ad->ID;
}
$exclude = implode(\',\', $exclude);
$blogusers = get_users(\'exclude=\'.$exclude.\'&orderby=\'.$order_by.\'&role=\'.$role);
}
foreach($blogusers as $author) {
$args = array(
\'author\' => $author->ID, // here is your author ID
\'showposts\' => 1,
\'orderby\' => \'date\',
\'date_query\' => array(
array(
\'after\' => array(
\'year\' => date( "Y" ),
\'month\' => date( "m", strtotime( "-1 Months" ) ),
\'day\' => date( "t", strtotime( "-1 Months" ) ),
),
\'inclusive\' => true,
)
)
);
$query = new WP_Query( $args );
if ($query->have_posts()) {
$display_name = $author->data->display_name;
$main_profile = get_the_author_meta(\'mainProfile\', $author->ID);
$hover_profile = get_the_author_meta(\'hoverProfile\', $author->ID);
$author_profile_url = get_author_posts_url($author->ID); ?>
<div class="da-author">
<div class="original-image">
<img src="<?php echo $main_profile; ?>" alt="<?php echo $display_name; ?>">
</div>
<div class="hover-image">
<a href="<?php echo $author_profile_url; ?>">
<img src="<?php echo $hover_profile; ?>">
</a>
</div>
</div><?php
// a post Loop
while ($query->have_posts()) {
$query->the_post();
the_title();
// etc.
}
}
}