wordpress集成说说(微语、碎语、微博)类似的功能。要用到wp的“文章类型”功能,这个东西在撰写文章时右侧栏才有选择,而要出现文章类型复选栏必须要拓展出来(默认是不显示的)下面是具体实现步骤:

步骤一

先在functions.php拓展出WP的文章类型,添加以下代码:

// 文章形式拓展
add_theme_support( 'post-formats', array( 'status' ) );

就一句代码,里面的status是“状态”文章类型,如果需要拓展其他的类型可以参考这里:http://codex.wordpress.org/zh-cn:%E6%96%87%E7%AB%A0%E5%BD%A2%E5%BC%8F ,拓展之后,撰写文章就有文章类型可选了。

步骤二

写一个自定义的获取文章类型函数,也是在functions.php加入:

// 获取文章类型
function inlojv_get_post_format() {
    $format = get_post_format ();
    return $format == '' ? 'normal' : $format;
}

步骤三

剩下就简单了,用条件判断据修改主循环,比如判断文章类型为正常时输出什么,文章类型为status时输出什么,下面是INLOUJV主题的posts.php文件内容,就是首页文章列表的主循环代码,看一下就知道该怎么用了:

<?php if (is_home() || is_front_page()) { ?>
<?php if( inlo_options('is_banner') == 'Y' ) { ?>
<div class="notice">
    <div class="n_banner"><img src="<?php bloginfo('template_directory'); ?>/img/<?php echo inlo_options('banner');?>" /><h2 class="n_title tra" style="display:<?php echo inlo_options('is_notice');?>"><span><?php echo inlo_options('notice');?></span></h2></div>
</div>
<?php } ?>
<?php } ?>
<div class="post-warp">
<?php if ( have_posts() ) { ?>
<?php while ( have_posts() ) { the_post(); global $post; ?>
    <?php $post_format = inlojv_get_post_format(); ?>
    <div class="post-box">
            <?php if ( post_password_required( $post->ID ) ) { ?>
            <div class="password-post-content">
                <?php echo get_the_password_form( $post->ID ); ?>
            </div>
            <?php } else { ?>
            
            <?php if( $post_format != 'status' ) { ?>
            <div class="post-caticon <?php $category = get_the_category();  echo $category[0]->category_nicename;?>-icon"></div>
            <div class="post-header <?php $category = get_the_category();  echo $category[0]->category_nicename;?>">
                    <h3 class="post-title tra"><a href="<?php the_permalink(); ?>" class="tra"><?php the_title(); ?></a></h3>
            </div>
            <div class="post-thumb">
                <a href="<?php echo get_permalink(); ?>"><?php echo inlojv_get_thumb( $post->ID, $post->post_title, $post->post_content, true ); ?><em></em></a>
            </div>            
            <div class="post-content tra">
            <?php
                $desc = has_excerpt();
                if ( ! $desc ) {
                    // 去掉表情 和 回复可见内容
                    $post_content = preg_replace( '/(\s\:.*\:\s)|(<\!--inlo_hide_start-->([\s\S]*)<\!--inlo_hide_end-->)/i', '', get_the_content() );
                    echo inlo_substr( strip_tags( $post_content ), 450, '<span class="read-more" style="margin-left:5px;color:#aaa">...</span>' ) ;
                } else {
                    the_excerpt();
                }
            ?>
            </div>
            <div class="post-tags">                
                <div class="views" title="访问人数"><span class="fa fa-eye"></span> <a>Views/<?php echo inlo_get_view( $post->ID ); ?></a></div>
                <div class="comments" title="评论数"><span class="fa fa-comments-o"></span> <a>Comments/<?php echo inlojv_get_comment_count( $post->ID ); ?></a></div>
                <div class="pocat" title="分类目录"><span class="fa fa-sitemap"></span> <a>Category/<?php $category = get_the_category();  echo $category[0]->cat_name;?></a></div>
                <div class="clear"></div>
            </div>
            <?php } ?>
            <?php if( $post_format == 'status' ) { ?>
            <div class="post-caticon m-status"></div>
            <div class="post-header m-header">
                    <h3 class="post-title tra"><a>Status : <?php the_time('Y-m-d H:s'); ?></a></h3>
            </div>
            <div class="post-content-m tra">
            <?php
                $desc = has_excerpt();
                if ( ! $desc ) {
                    // 去掉表情 和 回复可见内容
                    $post_content = preg_replace( '/(\s\:.*\:\s)|(<\!--inlo_hide_start-->([\s\S]*)<\!--inlo_hide_end-->)/i', '', get_the_content() );
                    echo inlo_substr( strip_tags( $post_content ), 450, '<span class="read-more" style="margin-left:5px;color:#aaa">...</span>' ) ;
                } else {
                    the_excerpt();
                }
            ?>
            </div>
            <?php } ?>
            <div class="clear"></div>    
            <?php } ?>
    </div>
<?php  } ?>
<?php } ?>
</div>
<div class="clear"></div>
<?php echo inlo_pagenavi(); ?>

从循环开始后即可开始判断输出。

步骤四

文章类型输出div的样式css,这一步根据不同主题进行不同的样式化即可。
以上。