wordpress一些常用代码

显示最新文章 
<div id="newpost"> 
	<h2> 最新文章</h2> 
	<?php 
		$previous_posts = get_posts('numberposts=15'); 
		foreach($previous_posts as $post) : 
		setup_postdata($post); 
	?> 
	<li>
		<a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>">
		<?php 
			the_title(); 
		?>
		</a>
	</li> 
	<?php /*?><?php the_content(); ?><?php */?> 
	<?php endforeach; ?> 
</div> 
	<div id="newcomment"> 
显示最新评论 
		<h2>最新评论</h2> 
		<div id="newcom"> 
		<?php 
			globa$wpdb; 
			$sq= "SELECT DISTINCT ID, post_title, post_password, comment_ID,comment_post_ID, comment_author, comment_date_gmt, comment_approved, comment_type,comment_author_url, SUBSTRING(comment_content,1,30) AS com_excerpt FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) WHERE comment_approved = '1' AND comment_type = '' AND post_password = '' ORDER BY comment_date_gmt DESC LIMIT 10"; 
		$comments = $wpdb->get_results($sql); 
		$output = $pre_HTML; 
		$output .= "
<ul>"; 
		foreach ($comments as $comment) { 
			$output .= "
<li>".strip_tags($comment->comment_author) .":" . "<a href="" . get_permalink($comment->ID). "#comment-" . $comment->comment_ID . "" title="on " . $comment->post_title . "">" .strip_tags($comment->com_excerpt) ."</a></li>"; 
		} 
		$output .= "
</ul>"; 
		$output .= $post_HTML; 
		echo $output;
		?> 
	</div> 
</div> 
显示热评文章 
	<div id="reping"> 
		<h2> 最受关注文章</h2> 
		<?php 
			$result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 10"); 
			foreach ($result as $topten) { 
				$postid = $topten->ID; 
				$title = $topten->post_title; 
				$commentcount = $topten->comment_count; 
			if ($commentcount != 0) { ?> 
				<li><a href="<?php echo get_permalink($postid); ?>" title="<?php echo $title ?>"><?php echo $title ?></a></li> 
		<?php } } ?> 
	</div> 
调用 Gravatar(只适应 2.5以上) 
	<?php if(function_exists('get_avatar')){ echo get_avatar($comment, '50');} ?> 
调用前两篇日志,后续 3篇日志列表 
	<?php query_posts('showposts=2'); ?><?php if (have_posts()) : while (have_posts()) : 
		the_post(); ?> 
		<h2<?php the_title(); ?></h2> 
		Posted on <?php the_time('F jS, Y') ?> in <?php the_category(', '); ?> 
		<?php endwhile; endif; ?><!-- google ad script --> 
		(Google ads here) 
		<?php query_posts('showposts=3&offset=2'); ?> 
		<?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
		<h2><?php the_title(); ?></h2> 
		Posted on <?php the_time('F jS, Y') ?> in <?php the_category(', '); ?> 
	<?php endwhile; endif; ?> 
随机文章 
	<div id="rand"> 
	<h2>随机文章</h2> 
	<?php 
	$rand_posts = get_posts('numberposts=10&orderby=rand');
	foreach( $rand_posts as $post ) : ?> 
	<!--下面是你想自定义的 Loop--> 
	<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> 
	<?php endforeach; ?></div> 
安全的调用 Wordpress插件模板代码 
	<?php if ( function_exists(’plugin_template_tag_function’) ) : ?> 
	<h2>部件标题</h2> 
	<?php plugin_template_tag_function(); ?> 
	<?php endif; ?>

  

原文地址:https://www.cnblogs.com/qiandu/p/4034847.html