Php 如何链接到wordpress中循环外的作者帖子
我正在使用UberMenu,他们非常有帮助,但我们还没有弄明白这一点 他们有一个快捷代码,可以在菜单中显示最近发布的文章,并带有图片和标题 我想在链接到作者文章的标题下添加该文章的作者 以下是当前代码:Php 如何链接到wordpress中循环外的作者帖子,php,wordpress,Php,Wordpress,我正在使用UberMenu,他们非常有帮助,但我们还没有弄明白这一点 他们有一个快捷代码,可以在菜单中显示最近发布的文章,并带有图片和标题 我想在链接到作者文章的标题下添加该文章的作者 以下是当前代码: function ubermenu_recent_posts($atts){ global $uberMenu; extract(shortcode_atts(array( 'num' => 3, 'img' => 'on'
function ubermenu_recent_posts($atts){
global $uberMenu;
extract(shortcode_atts(array(
'num' => 3,
'img' => 'on',
'img_width' => 85,
'img_height'=> 55,
'excerpt' => 'off',
'category' => '',
'default_img' => false,
'offset' => 0,
), $atts));
$args = array(
'numberposts' => $num,
'offset' => $offset,
'suppress_filters' => false
);
if(!empty($category)){
if(is_numeric($category)){
$args['category'] = $category;
}
else $args['category_name'] = $category;
}
$posts = get_posts($args);
$class = 'wpmega-postlist';
if($img == 'on') $class.= ' wpmega-postlist-w-img';
$html= '<ul class="'.$class.'">';
foreach($posts as $post){
$ex = $post->post_excerpt;
if( $ex == '' && function_exists( 'wp_trim_words' ) ){ //wp_trim_words is a WP3.3 function
$ex = $post->post_content;
$ex = strip_shortcodes( $ex );
$ex = str_replace(']]>', ']]>', $ex);
$excerpt_length = apply_filters('excerpt_length', 10);
$excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
$ex = wp_trim_words( $ex, $excerpt_length, $excerpt_more );
}
//$ex = apply_filters('get_the_excerpt', $post->post_excerpt);
$post_url = get_permalink($post->ID);
$author_name = get_the_author_meta('display_name', $post->post_author );
$author_id = $post->post_author;
$post_comments = comments_popup_link( __('No comments', 'wellthemes'), __( '1 comment', 'wellthemes'), __('% comments', 'wellthemes'));
$image = '';
$w = $img_width;
$h = $img_height;
if($img == 'on') $image = $uberMenu->getPostImage($post->ID, $w, $h, $default_img);
$html.= '<li>'. $image.
'<div class="wpmega-postlist-title">
<div class="wpmega-post-title"><a href="'.$post_url.'">'.$post->post_title.'</a></div>
<div class="wpmega-post-author"><a href="'.get_author_posts_url($author_id).'"><span>by '.$author_name.'</span></a>'.$post_comments.'</div>
</div>';
if($excerpt == 'on')
$html.= '<div class="wpmega-postlist-content">'.$ex.'</div>';
$html.= '<div class="clear"></div>'.
'</li>';
}
$html.= '</ul>';
return $html;
}
add_shortcode('wpmega-recent-posts', 'ubermenu_recent_posts'); //legacy
add_shortcode('ubermenu-recent-posts', 'ubermenu_recent_posts');
功能菜单\u最近发布的帖子($atts){
全球$uberMenu;
提取(短码)附件(数组)(
'num'=>3,
“img”=>“on”,
“img_宽度”=>85,
“img_高度”=>55,
'摘录'=>'关闭',
'类别'=>'',
“default_img”=>false,
“偏移量”=>0,
)美元(附件);;
$args=数组(
“numberposts”=>$num,
“偏移量”=>$offset,
“抑制_过滤器”=>false
);
如果(!空($category)){
如果(是数字($category)){
$args['category']=$category;
}
else$args['category_name']=$category;
}
$posts=get_posts($args);
$class='wpmega postlist';
如果($img=='on')$class.='wpmga-postlist-w-img';
$html=“”;
foreach($posts作为$post){
$ex=$post->post_摘录;
如果($ex=''&&function_存在('wp\u trim\u words')){//wp\u trim\u words是一个WP3.3函数
$ex=$post->post\u内容;
$ex=条带和短代码($ex);
$ex=str_replace(']]>',']]]',$ex);
$extract_length=应用过滤器('extract_length',10);
$EXECRPT_more=应用_过滤器('EXECRPT_more','.'.[…]);
$ex=wp\u trim\u words($ex,$extract\u length,$extract\u more);
}
//$ex=应用过滤器('get_the_extract',$post->post_extract);
$post\u url=get\u permalink($post->ID);
$author\u name=获取作者元($display\u name',$post->post\u author);
$author\u id=$post->post\u author;
$post_comments=comments_popup_link('No comments'、'wellthemes')、'1 comment'、'wellthemes')、'u_('comments'、'wellthemes')、'wellthemes');
$image='';
$w=$img\U宽度;
$h=$img\U高度;
如果($img=='on')$image=$uberMenu->getpostmage($post->ID,$w,$h,$default\u img);
$html.='- '.$image。
'
“.$post_评论。”
';
如果($extract=='on')
$html.=''.$ex.';
$html.=''。
“
”;
}
$html.='
';
返回$html;
}
添加快捷码('wpmga-recent-posts','ubermenu_recent_posts')//遗产
添加快捷码(“ubermenu-recent-posts”、“ubermenu\u recent\u posts”);
如果将鼠标悬停在“运动”菜单选项卡上,并将鼠标悬停在主页上的作者姓名上,您将看到它正确链接,但如果您单击任何其他页面,则会从URL中删除作者
站点:您能发布完整的代码吗?get_The_author..()函数是模板标记,它们的工作方式取决于循环的结构。我很惊讶它能在主页上运行。添加了完整的代码。我也做了一些改变<代码>$author\u id=$post->post\u author代码>和这个
,现在一切似乎都正常了。现在,如果我只能得到工作评论的数量,我将被设置。