免插件实现无觅相关文章样式[修正] » 荒野无灯weblog

Keep It Simple, Stupid.

荒野无灯weblog

免插件实现无觅相关文章样式[修正]

先前写过一个文字版的。
这次弄个图片版的吧。
代码取自我的Hacklog package,在此分享出来。

别忘记在主题的 default-post-thumbnails 目录下放一默认图片 default.jpg

修正在没有标签相关文章时不获取随机文章的Bug.(2012-04-05T00:23:12+00:00)
修正有时在首页会导致sidebar 无法显示的bug.(2012-04-05T00:23:12+00:00)
弃用ob_start() 方式获取内容。(2012-04-05T00:23:12+00:00)


其它:
你可以在一目录下放一些命名规范的图片,修改ihacklog_pkg_get_default_post_thumbnail() 函数使之随机显示默认图片。


add_action( 'after_setup_theme',    'ihacklog_pkg_add_featured_image_support', 11 );
add_action('wp_head','ihacklog_pkg_related_posts_print_css',999);
add_filter('the_content','ihacklog_pkg_related_posts_with_thumbnail',10);

/**
 * @see http://wordpress.stackexchange.com/questions/23839/using-add-theme-support-inside-a-plugin
 * @see http://codex.wordpress.org/Function_Reference/add_theme_support
 * @see http://codex.wordpress.org/Plugin_API/Action_Reference
 */
function ihacklog_pkg_add_featured_image_support()
{
    $supportedTypes = get_theme_support( 'post-thumbnails' );

    if( $supportedTypes === false )
     {
        add_theme_support( 'post-thumbnails', array( 'post' ) );    
     }            
     set_post_thumbnail_size( 200 , 150 , true );
}

function ihacklog_pkg_get_default_post_thumbnail()
{
    $src = sprintf('%1$s/images/default-post-thumbnails/default.jpg', get_stylesheet_directory_uri());
    return $src;
}

/**
 * @see http://codex.wordpress.org/Function_Reference/query_posts
 * @see http://codex.wordpress.org/Class_Reference/WP_Query#Parameters
 * @author 荒野无灯
 * @description get related posts 
 */
function ihacklog_pkg_related_posts_with_thumbnail($content)
{
    global $post;
    if( $post->post_type != 'post'  || !is_singular() )
    {
        return $content;
    }
    $related_posts = '';
    $tag_ids = array();
    $tags = wp_get_post_tags($post->ID);
    if( $tags )
    {
        $related_posts_caption = get_the_tag_list('More posts tagged with: ',' • ','');
        foreach($tags as $tag)
        {
            $tag_ids[]= $tag->term_id;
        }
        $query = new WP_Query(array( 'tag__in' => $tag_ids, 
            'post__not_in' =>array($post->ID),
            'orderby'=>'rand',
            'post_status' =>'publish',
            'post_type' =>'post' ,
            'showposts'=>5
        ) );

    }
    if( !$query->have_posts() )
    {
        $related_posts_caption = '随机日志';
        $categoryies = get_the_category();
        foreach( (array)$categoryies as $category)
        {
            $cat_ids[] = $category->term_id;
        }
        //if no related post, fetch random posts.
        $query = new WP_Query(array( 'category__in' => $cat_ids, 
            'post__not_in' =>array($post->ID),
            'orderby'=>'rand',
            'post_status' =>'publish',
            'post_type' =>'post' ,
            'showposts'=>5
        ) );
    }
    if( $query->have_posts() )
    {
        if(is_feed())
        {
            ihacklog_pkg_related_posts_print_css();
        }

        $related_posts .= '';
    }
    //Ok,my query has been done.now restores the $post global to the current post in the main query
    wp_reset_postdata();
    return $content . $related_posts;
}

function ihacklog_pkg_related_posts_print_css()
{
    echo <<
/** related article **/
.related-posts {
        /*width: 568px; */
        height: 10px; 
        margin: 20px 0px 80px;
        font-family: "Microsoft YaHei","WenQuanYi ZenHei";
}

.related-posts a {
        color: #555; text-decoration: none; 
}

.related-posts-caption {
    margin-bottom: 10px; font-weight: bold; font-size: 14px;
    border-bottom: 4px double #E3E1DB;
}
.related-posts ul {
    list-style: none;
    margin-left: 10px;      
    margin: 0;
    padding: 0;
    width: 600px;
}
.related-posts ul li {
    display: block;
    background-image: none;
    float: left; 
    padding: 5px; 
}
.related-posts ul li:hover {
  background-color: #F4F4F4;
    border-radius: 5px 5px 5px 5px;
    box-shadow: 2px 2px 3px #888888;
}
.related-posts ul li img {
    border-radius: 5px 5px 5px 5px;
        width: 96px; height: 90px; padding: 2px; border: 1px solid #CCCCCC; 
}
.related-posts ul li .related-posts-tittle {
        margin-left: 2px; 
        width: 96px; 
        height: 7em;
        overflow:hidden;
        font-size: 12px;
        font-weight:normal;
        text-decoration: none; 
}

EOT;
}

效果图:

– EOF –

Tagged in : tips,相关文章

All Comments (0)
Gravatar image
No Comments