Php 作者的最新文章
我正在使用一个WP插件来显示作者的最新帖子,该插件将帖子显示为一个要点列表,我需要它显示在两列中;这可以通过编辑插件的php代码来实现吗Php 作者的最新文章,php,wordpress,plugins,Php,Wordpress,Plugins,我正在使用一个WP插件来显示作者的最新帖子,该插件将帖子显示为一个要点列表,我需要它显示在两列中;这可以通过编辑插件的php代码来实现吗 function latest_posts_by_author( $array ) { global $post; $defaults = array( 'author' => '', 'show' => 5, 'excerpt' => 'false', 'post_type' => 'post
function latest_posts_by_author( $array ) {
global $post;
$defaults = array(
'author' => '',
'show' => 5,
'excerpt' => 'false',
'post_type' => 'post'
);
// Sets default author if in loop or on single page.
if( in_the_loop() || is_single() ){
$author_id=$post->post_author;
$defaults['author'] = get_the_author_meta( 'user_login', $author_id );
}
// Overrides defaults with shortcode settings and separates into individual varaibles.
extract( shortcode_atts( $defaults, $array ) );
// Checks to make sure an author has been set.
if( !empty( $author ) ){
// Checks to see if there are multiple authors set.
$comma = strpos( $author, ',' );
if( $comma === false ) {
// Gets the author data for a single author.
$author_data = get_user_by( 'login', $author );
if( !empty( $author_data ) ) {
$args = array(
'author' => $author_data->ID,
'posts_per_page' => $show,
'post__not_in' => array($post->ID),
'post_type' => $post_type
);
}
} else {
// Gets the author data for multiple authors.
$authors = explode( ',', $author );
$author_data = '';
foreach( $authors as $author_login ){
$user = get_user_by( 'login', $author_login );
$author_data .= $user->ID . ',';
}
$args = array(
'author' => $author_data,
'posts_per_page' => $show,
'post__not_in' => array($post->ID)
);
}
// Gets posts form database
$author_query = new WP_Query( $args );
// Displays posts if available
if( $author_query ) {
$html = '';
$html = apply_filters( 'latestbyauthor_list_before', $html );
$html .= '<ul class="latestbyauthor">';
while ( $author_query->have_posts() ) : $author_query->the_post();
$html .= '<li>';
$html = apply_filters( 'latestbyauthor_link_before', $html );
// Displays a link to the post, using the post title
$html .= '<a href="' . get_permalink() . '" title="' . get_the_title() . '">';
$html = apply_filters( 'latestbyauthor_title_before', $html );
$html .= apply_filters( 'latestbyauthor_title', get_the_title() );
$html = apply_filters( 'latestbyauthor_title_after', $html );
$html .= '</a>';
$html = apply_filters( 'latestbyauthor_link_after', $html );
// Displays the post excerpt if "excerpt" has been set to true
if($excerpt == 'true'){
$html .= '<p>' . apply_filters( 'latestbyauthor_excerpt', get_the_excerpt() ) . '</p>';
}
$html .= '</li>';
endwhile;
$html .= '</ul>';
$html = apply_filters( 'latestbyauthor_list_after', $html );
}
// Resets Post Data
wp_reset_postdata();
// Returns the results
return $html;
}
function最新发布由作者发布($array){
全球$员额;
$defaults=数组(
'作者'=>'',
“显示”=>5,
'摘录'=>'假',
“职位类型”=>“职位”
);
//设置循环中或单页上的默认作者。
如果(在_循环()中| |是_单循环()){
$author\u id=$post->post\u author;
$defaults['author']=获取作者元('user\u login',$author\u id);
}
//使用快捷码设置覆盖默认值,并将其分离为单个变量。
提取(shortcode_atts($defaults,$array));
//检查以确保已设置作者。
如果(!空($author)){
//检查是否设置了多个作者。
$comma=strpos($author,“,”);
如果($逗号===false){
//获取单个作者的作者数据。
$author\u data=get\u user\u by('login',$author);
如果(!空($author_data)){
$args=数组(
'author'=>$author\u data->ID,
“每页帖子”=>$show,
'post\u not\u in'=>数组($post->ID),
“post\u type”=>$post\u type
);
}
}否则{
//获取多个作者的作者数据。
$authors=分解(“,”,$author);
$author_data='';
foreach($authors as$author\u login){
$user=get_user_by('login',$author_login);
$author_data.=$user->ID.',';
}
$args=数组(
“author'=>$author\u数据,
“每页帖子”=>$show,
'post\u not\u in'=>数组($post->ID)
);
}
//获取表单数据库中的帖子
$author\u query=新的WP\u查询($args);
//显示文章(如果可用)
if($author\u query){
$html='';
$html=apply_过滤器('latestbyauthor_list_before',$html);
$html.='
';
$html=apply_过滤器('latestbyauthor_list_之后,$html);
}
//重置Post数据
wp_reset_postdata();
//返回结果
返回$html;
}
}
添加快捷码(“latestbyauthor”,“latest\u posts\u by\u author”) 是,您可以使用偏移参数 基本上做两个单独的查询,第一个显示您喜欢的数量,然后下一个使用参数中的'offset'=>x将下一个偏移5 e、 g.对于前5项:
$defaults=array(
'作者'=>'',
“显示”=>5,
'摘录'=>'假',
“职位类型”=>“职位”
);代码>
第二项:
$defaults=array(
'作者'=>'',
“显示”=>5,
'摘录'=>'假',
“post_type”=>“post”,
“偏移量”=>5
);代码>
编辑:为了使其工作,您可以复制整个函数,并使用不同的函数名和快捷码名创建一个新函数。在第二个例子中,只要用你喜欢的数量来抵消它,你就可以同时调用这两个短代码,并且你将有两个列表,每个列表中有5个帖子。谢谢,这很有效。但我仍然有一个问题,那就是它显示了相同的结果。再次感谢你的帮助,没问题。你期望得到什么样的结果,从中得到什么?