WordPress相关文章代码

WordPress文章页底部相关文章代码:

<table>
<tr>
<td style="float:left;width:340px;"">
<h3>Related Posts</h3>
<?php
        $tags = wp_get_post_tags($post->ID);
        if ($tags) {
        $first_tag = $tags[0]->term_id;
        $args=array(
        'tag__in' => array($first_tag),
        'post__not_in' => array($post->ID),
        'showposts'=>1,
        'caller_get_posts'=>1
        );
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
        while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title();?></a>
        <?php
        endwhile;
            }else { ?>
        <?php 
        echo hui_randomPosts(5);
        }
        }
        wp_reset_query();
        ?>
</td>
<?php include (TEMPLATEPATH . '/bsa-250.php'); ?>
</tr>
</table>

Function函数:

function hui_randomPosts ($limit = 5, $length = 200, $after = '</br>', $show_pass_post = false, $show_excerpt_in_title = true) {
    global $wpdb;
    $sql = "SELECT ID, post_title, post_date, post_content FROM $wpdb->posts WHERE post_status = 'publish' ";
	if(!$show_pass_post) $sql .= "AND post_password ='' ";
	$sql .= "ORDER BY RAND() LIMIT $limit";
    $posts = $wpdb->get_results($sql);
	$output = '';
    foreach ($posts as $post) {
       $post_title = stripslashes($post->post_title);
	$post_date = mysql2date('Y.m.d', $post->post_date);
       $permalink = get_permalink($post->ID);
	$post_content = strip_tags($post->post_content); 
	$post_content = stripslashes($post_content); 
	$post_strip = substr($post_content,0,$length);
       $post_strip = hui_utf8_trim($post_strip);
	$post_strip = str_replace('"', '', $post_strip);
	$output .= '<a href="' . $permalink . '" rel="bookmark" title="';
	if($show_excerpt_in_title) {
		$output .= $post_strip . '...  ';
      	   } else  {
		$output .= 'Link: ' . str_replace('"', '', $post_title) . '...   ';
	}
	$output .= $post_date . '">' . $post_title . '</a>';
	if(!$show_excerpt_in_title) {
		$output .= ': ' . $post_strip . '...  ';
      	   }
	$output .= $after;
	}
	echo $output;
}

珲珲同学出品。

文章地址:http://www.rssidc.cn/286.html ,欢迎分享。


发表评论

电子邮件地址不会被公开。 必填项已用*标注