2013-03-07 86 views
0

在我们当前的网站上,我们有不同的公司简介。一些公司的博客文章显示在他们的个人资料中,有些则没有。他们是一个h2标签,位于页面模板中的帖子之上,然后是发布帖子的代码。代码看起来像这样。如果他们没有博客文章,则不会显示标题。 Wordpress

<h2>Recent Blog Articles</a></h2> 
<?php echo get_related_author_posts(); ?> 

我想找到一种方法,以便如果他们没有公司的职位,h2标签不会显示出来。功能文件中的代码是

function get_related_author_posts() { 
    global $authordata, $post; 

    $authors_posts = get_posts(array('author' => $authordata->ID, 'post__not_in' => array($post->ID), 'posts_per_page' => 5)); 


    $output = ' <ul style="list-style: none;">'; 
    foreach ($authors_posts as $authors_post) { 
     $output .= '<li><a href="' . get_permalink($authors_post->ID) . '">' . apply_filters('the_title', $authors_post->post_title, $authors_post->ID) . '</a></li>'; 
    } 
    $output .= '</ul>'; 

    return $output; 

} 

我无法弄清楚任何建议都会有帮助。先谢谢你。

+0

我会问在WordPress的答案这个问题。它会更适合那里,可能会更快地回答。 http://wordpress.stackexchange.com/ – 2013-03-07 17:37:29

回答

1

如果您将h2添加到该函数中,您可以通过添加if语句来检查是否有任何作者帖子来控制整个输出。

function get_related_author_posts() { 
    global $authordata, $post; 

    $authors_posts = get_posts(array('author' => $authordata->ID, 'post__not_in' => array($post->ID), 'posts_per_page' => 5)); 

    if(! $authors_posts) { 
     return; 
    } 

    $output = '<h2>Recent Blog Articles</h2>'; 
    $output .= ' <ul style="list-style: none;">'; 
    foreach ($authors_posts as $authors_post) { 
     $output .= '<li><a href="' . get_permalink($authors_post->ID) . '">' . apply_filters('the_title', $authors_post->post_title, $authors_post->ID) . '</a></li>'; 
    } 
    $output .= '</ul>'; 

    return $output; 

} 
+0

是的。谢谢。我在那里挣扎了一段时间,这很简单。你先生真棒。 – 2013-03-07 17:47:16

+0

如果这个答案你问你应该接受这个答案。 – 2013-03-07 18:18:36

相关问题