wordpress 自定义分页符

先在functions.php中设置:

function get_pagenavi( $range = 4 ) {
 global $paged,$wp_query;
 if ( !$max_page ) {
 $max_page = $wp_query->max_num_pages;
 }
 if( $max_page >1 ) { 
 if( !$paged ){
 $paged = 1;
 }
 echo "<li>"; previous_posts_link('上一页');echo"</li>";
 if ( $max_page >$range ) {
 if( $paged <$range ) {
 for( $i = 1; $i <= ($range +1); $i++ ) {
 echo "<li><a href='".get_pagenum_link($i) ."'";
 if($i==$paged) echo " class='current'";echo ">$i</a></li>";
 }
 }elseif($paged >= ($max_page -ceil(($range/2)))){
 for($i = $max_page -$range;$i <= $max_page;$i++){
 echo "<li><a href='".get_pagenum_link($i) ."'";
 if($i==$paged)echo " class='current'";echo ">$i</a></li>";
 }
 }elseif($paged >= $range &&$paged <($max_page -ceil(($range/2)))){
 for($i = ($paged -ceil($range/2));$i <= ($paged +ceil(($range/2)));$i++){
 echo "<li><a href='".get_pagenum_link($i) ."'";if($i==$paged) echo " class='current'";echo ">$i</a></li>";
 }
 }
 }else{
 for($i = 1;$i <= $max_page;$i++){
 echo "<li><a href='".get_pagenum_link($i) ."'";
 if($i==$paged)echo " class='current'";echo ">$i</a></li>";
 }
 }
 echo "<li>";next_posts_link('下一页');echo"</li>"; 
 echo '<li><span>共'.$max_page.'页</span></li>'; 
 }
}

调用:

 <div class="pagination">
 <ul>
 <?php get_pagenavi();?>
 </ul>
 </div>

 

调用特色图片为缩略图

调用特色图片为缩略图:
在functions.php中加入add_theme_support( 'post-thumbnails' );
如果想加入自定义格式加入:
add_image_size( 'one', 215, 140, true ); // one是调用参数名,后面是大小
add_image_size( 'two', 345, 205, true ); // Set thumbnailsize

add_image_size( 'big', 350, 220, true ); // Set thumbnail size

调用:尺寸可以在多媒体后台设置
<?php $full_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full'); echo $full_image_url[0]; ?><!--特色图像引用:小尺寸-->
<?php $thumbnail_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail'); echo $thumbnail_image_url[0]; ?><!--特色图像引用:中尺寸-->
<?php $medium_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'medium'); echo $medium_image_url[0]; ?><!--特色图像引用:大尺寸-->
<?php $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large'); echo $large_image_url[0]; ?><!--特色图像引用:自定义尺寸-->
<?php $array_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array(200,200)); echo $array_image_url[0]; ?>array(200,200)来定义图片大小也可以 设置为 'one' 在functions.php中设置自定义图片名。

如果没有图片,指定固定图片

<?php if ( has_post_thumbnail() ) { ?>
    <a href="<?php the_permalink(); ?>" target="_blank" title="<?php the_title(); ?>">
        //特色图片
        <img alt="<?php the_title(); ?>" src="<?php $full_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full'); echo $full_image_url[0]; ?>"/>
    </a>
<?php } else {?>
    <a href="<?php the_permalink(); ?>" target="_blank" title="<?php the_title(); ?>">
        //暂无图片
        <img title="<?php the_title(); ?>" src="<?php bloginfo('template_url'); ?>/images/morentu.png" />
    </a>
<?php } ?>

 

Contact Form 7 显示提交成功,并返回当前页面URL

1.需要先为按钮指定ID如“submit”,如果页面有多个表单按钮,那就需要定义多个ID

<a href="#" id="submit" onclick="document.getElementById('click').submit();return false;">
				<div class="yuyue-NOW" id="bottomBnt"><i>开始计算</i></div>
			</a>

 

2.DOM到按钮ID,使用javascript事件来跳转

<script type="text/javascript">
		document.getElementById( "submit" ).addEventListener( "click", function () {
			var url;
//将当前页面的URL,赋值给url变量
			url = window.location.href; 
			alert( "提交成功" );
			//跳转到当前页面URL
			location = url;
		} );
	</script>

 

Contact Form 7 Javascript 代替 html sumit做按钮

案例代码:

<head>
        <meta charset="utf-8">
        <title>注册</title>
        <link rel="stylesheet" type="text/css" href="css/reg.css" />

        //以下定义哪个id 的 form使用 javascript代替
		<script type="text/javascript">
            function submit(){
               $("#click").submit();
            }
        </script>

</head>



//定义 form的ID为click
<form action="loginControl" method="post" id="click">
     <p>
        用户名:<input type="text" name="username" id="username" value="" />
     </p>
            //定义a 为submit
     <a href="#" onclick="document.getElementById('click').submit();return false;">注册</a>
</form>

复制代码

 

wordpress调用目录页

调用目录页:

<ul class="mproductpic">
	<?php if ($posts_perpage) { ?>
	<?php $postsperpage = $posts_perpage; ?>
	<?php } else { ?>
    <?php $postsperpage =6; ?>
    <?php } ?>
    <?php
						$categoryID=$cat;
						
						$wp_query = new WP_Query('cat=' . $categoryID. 'orderby=date&order=desc&posts_per_page='.$postsperpage.'&paged='.$paged); ?>
    <?php while (have_posts()) : the_post(); ?>
		
			
			<li>
				<a href=<?php the_permalink() ?> title="<?php the_title(); ?>"><?php if(function_exists('wp_thumbnails_for_homepage')) { wp_thumbnails_for_homepage(); } ?></a>

				<h2 class="mproductpicbt">
					<?php the_title(); ?>
				</h2>
			</li>
	    <?php endwhile; ?>
			<div class="clearfix"></div>
		</ul>

 

wordpress 获取相关文章两种方法 标签tag 和 目录category

1.使用当前文章tag获取:

<ul class="aboutCon">
<?php
global $post;
$post_tags = wp_get_post_tags($post->ID);
if ($post_tags) {
  foreach ($post_tags as $tag) {
    // 获取标签列表
    $tag_list[] .= $tag->term_id;
  }
 
  // 随机获取标签列表中的一个标签
  $post_tag = $tag_list[ mt_rand(0, count($tag_list) - 1) ];
 
  // 该方法使用 query_posts() 函数来调用相关文章,以下是参数列表
  $args = array(
        'tag__in' => array($post_tag),
        'category__not_in' => array(NULL),  // 不包括的分类ID
        'post__not_in' => array($post->ID),
        'showposts' => 6,                           // 显示相关文章数量
        'caller_get_posts' => 1
    );
  query_posts($args);
 $i=1;
  if (have_posts()) {
    while (have_posts()) {
      the_post(); update_post_caches($posts); ?>
    <li><br /><?php echo $i+".";?><?php the_title(); ?>
				<br /><?php the_excerpt(); ?>
				</li>
<?php
    }
  }
  else {
    echo '<li>* 暂无相关文章</li>';
  }
  wp_reset_query(); 
}
else {
  echo '<li>* 暂无相关文章</li>';
}
?>
				
			</ul>

2.使用目录category获取

<?php
			global $post;
			$cats = wp_get_post_categories( $post->ID );
			if ( $cats ) {
				$args = array(
					'category__in' => array( $cats[ 0 ] ),
					'post__not_in' => array( $post->ID ),
					'showposts' => 6,
					'caller_get_posts' => 1
				);
				query_posts( $args );

				if ( have_posts() ) {
					while ( have_posts() ) {
						the_post();
						update_post_caches( $posts );
						?>

			<li class="tuiJianCon last ">

				<ul class="tuiJianNews">

					<li class="font-24" id="shij">
						<?php the_time('d');?><br/>
						<span>
							<?php the_time('Y-m');?>
						</span>
					</li>
					<li class="newsBt"><a href=<?php the_permalink(); ?> title="<?php the_title(); ?>"><?php the_title(); ?></a>
					</li>
					<li class="newsCon"><a href=<?php the_permalink(); ?> title="">  
	<?php the_excerpt(); ?></a>
					</li>
					<div class="clearfix"></div>
				</ul>
			</li>


			<?php
			}
			} else {
				echo '<li>* 暂无相关文章</li>';
			}
			wp_reset_query();
			}
			else {
				echo '<li>* 暂无相关文章</li>';
			}
			?>

 

wordpress 代码实现分页

分页符调用:

<?php echo paginate_links(array(
    'prev_next'          => 0,
    'before_page_number' => '',
    'mid_size'           => 2,
));?>

分页CSS:

/*分页样式*/
.posts-nav {
	font-size: 14px;
	color:#000000;
	padding: 10px 0;
	margin: 0 auto;
	line-height:23px;text-align:center;
}
.posts-nav .page-numbers {
	border-radius: 3px;
	border: 1px solid rgba(0, 0, 0, 0.15);
	display: inline-block;
	text-align: center;
	width: 30px;
	line-height: 30px;
	margin: 0 5px;
}
.posts-nav .page-numbers.current, .posts-nav .page-numbers:not(.dots):hover {
	background: #1F7508;
	border-color: #1F7508;
	color: #fff;
}
.posts-nav .page-numbers.dots {
	border-color: rgba(0,0,0,0)
}

 

 

wordpress 免插件显示浏览量

在functions.php中放入以下代码:

/*显示文章浏览次数*/
function getPostViews($postID){
$count = get_post_meta($postID,'views', true);
if($count==''){
delete_post_meta($postID,'views');
add_post_meta($postID,'views', '0');
return "0";
}
return $count.'';
}
function setPostViews($postID) {
$count = get_post_meta($postID,'views', true);
if($count==''){
$count = 0;
delete_post_meta($postID,'views');
add_post_meta($postID,'views', '0');
}else{
$count++;
update_post_meta($postID,'views', $count);
}
}

调用代码:

<?php setPostViews(get_the_ID()); echo number_format(getPostViews(get_the_ID())); ?>