如何链接到wordpress中循环外的作者

时间:2014-01-31 16:32:43

标签: php wordpress

我正在使用UberMenu并且他们非常乐于助人,但我们无法弄清楚这一点。

他们有一个短代码,用于显示带有图像和标题的菜单中的最近帖子。

我想在链接到作者帖子的标题下添加帖子的作者。

以下是当前代码:

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(']]>', ']]&gt;', $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');

如果您将鼠标悬停在体育菜单标签上并在主页上鼠标悬停作者姓名,您将看到它正确链接,但如果您点击任何其他页面,则会从网址中删除作者。

网站:http://mystreetlife.com/home

0 个答案:

没有答案