wordpress函数技巧

1.Loop循环(成功)

<?php if(have_posts()) : ?> 
<?php while(have_posts()) : the_post(); ?> 
// the code inside the loop //插入Loop 中的代码 
<?php endwhile; ?> 
<?php else : ?> 
<?php endif; ?> 

2.在WordPress的第一篇文章中插入Google广告

<?php while ( have_posts() ): the_post(); $count++;?> 
 
<?php if ($count == 1) : ?> 
// Insert your Google AdSense code here 
<?php endif; ?> 
 
<?php endwhile; ?>

你同样可以改变 count值来让广告比如放在不同的日志后面,比如改成 count == 2, 则把Google广告显示在第二篇日志后面:

3.改变每个分类页面的日志显示数

你将需要打开 category.php文件并且找到下面的这行, 
 
在<?php if (have_posts()) : ?> 下面添加内容: 
 
<?php if (is_category('1')) {$posts = query_posts ($query_string.'&showposts=1'); } ?> 
<?php if (is_category('16')) {$posts = query_posts($query_string.'&showposts=2'); } ?> 
 
“is_category()”就是分类ID,比如可以写成”is_category(’5′)”, “showposts=4″ 里面的数值就是你要显示的分类日志数。

4.给特定分类添加不同模板

如果你想给ID为 7的分类制作一个和你目前主题不一样的外观,只需在你当前主题文件夹中创建一个名为 category-7.php 的模板文件,然后自定义我喜欢的样式,WordPress 会自动查看并显示它,请注意文件名中的"-"符号。

5.为特定分类文章创建不同外观

假设你有两个分类,分别名为"News"和"Tutorials",然后你想让"News"分类里的单篇文章的外观表层为style1.css,让"Tutorials"分类里的单篇文章外观表层为 style2.css,Lorelle提供了以下这种比较简单的解决方法: 
 
打开single.php 文件,并删除里面的所有代码,添加以下代码: 
 
<?php 
$post = $wp_query->post; 
if ( in_category('9') ) { 
include(TEMPLATEPATH . '/single2.php'); 
} else { 
include(TEMPLATEPATH . '/single1.php'); 
} 
?> 
通常情况下,PHP会自动查询这段代码,如果日志的分类ID为9,则会自动显示single2.php,如果不是则会显示为single1.php。 
注意:single1.php 和 single2.php 里的样式自定义,系统会自动判断加载不同的样式显示

6.样式化不同分类

如果你想给特定分类规定不同的样式表,你所需做的只是在 header.php 文件的标签中加入以下代码: 
 
<?php if ( is_category('15') ) { ?> 
<link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/cat-15.css" 
type="text/css" media="screen" />; 
<?php } else { ?> 
<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" 
media="screen" /> 
<?php } ?>

7.在某一个页面单独调用某一个php文件

如果你仅想在首页显示某个文件内容,可在index.php 文件中写入以下代码: 
 
<div> <?php if ( is_home() ) { include (TEMPLATEPATH.'/test.php'); } ?> </div> 
<div> <?php if ( is_category(1) ) { include (TEMPLATEPATH.'/test.php'); } ?> </div> 
页面显示特定内容:(取名字到 is_page(‘* ’)) 
<div> <?php if( is_page('contact')) { include (TEMPLATEPATH.'/test.php'); } ?> </div>//判断是不 contact页面 
/// is_posted()

8.动态标题以实现更友好的SEO

替换:header.php 内 <title><?php bloginfo('name'); wp_title(); ?></title> 
<?php 
 if (is_home()) { echo bloginfo('name'); } 
 elseif (is_404()) { bloginfo('name'); echo ' - Oops, this is a 404 page'; } 
 else if ( is_search() ) { bloginfo('name'); echo (' - Search Results');} 
 else { bloginfo('name'); echo (' - '); wp_title(''); } 
?>

9.动态显示菜单:根据分类及页面类型显示相应条目

<!--Begin an unordered list the ID is for the bookmark, the class is for the style 
 (so I can have other lists with the same styling as seen on RedScrubs.com)--> 
 <ul id="nav" class="btn"> 
<!--IF the current page is the 404 page, note the added error class--> 
 <?php if(is_404()) { ?> 
 <li class="current_page_item error"><a href="#">404 Error</a></li> 
 <?php } ?> 
 <!--IF the current page is the search results page--> 
 <?php if(is_search()) { ?> 
 <li class="current_page_item"><a href="#">Search Results</a></li> 
 <?php } ?> 
 
<!--IF the current page is a single post--> 
 <?php if(is_single()) { ?> 
 <li class="current_page_item"><a href="#">Selected Article</a></li> 
 <?php } ?> 

10.动态显示高亮显菜单

动态加载css样式: 
<ul id="nav"> 
 <li<?php if ( is_home() || is_category() || is_archive() || is_search() || is_single() || is_date() ) { echo ' class="current"'; } ?>><a href="#">Gallery</a></li> 
 <li<?php if ( is_page('about') ) { echo ' class="current"'; } ?>><a href="#">About</a></li> 
 <li<?php if ( is_page('submit') ) { echo ' class="current"'; } ?>><a href="#">Submit</a></li> 
</ul>

11.自定义顶部导航

<ul id="nav"> 
 <li <?php if ( is_home() || is_category() || is_archive() || is_search() || is_single() || is_date() ){ echo ' class="current"'; } ?>><a href="<?php bloginfo('url'); ?>">首页</a></li> 
 <li <?php if ( is_page('about') ) { echo ' class="current"'; } ?>> <a href="<?php echo '/index.php/about' ; ?>">关于</a> </li> 
 <li <?php if ( is_page('liuyan') ) { echo ' class="current"'; } ?>> <a href="<?php echo '/index.php/liuyan' ; ?>">留言板</a> </li>
</ul>

12.查询文章

Query_posts(查询文章?)能被用来显示特定文章,你所需做只是你一个模板文件的 Loop开始之前调用函数 只显示某个分类中的文章 
如果你想显示某个特定分类的一定目录的文章 
<?php query_posts('cat=15&showposts=10'); ?> //这里则会显示分类ID为15的最新10篇文章。 从首页移除特定分类  
<?php if (is_home()) { query_posts("cat= -3"); } ?> //从主页上移除分类为3的所有日志。 
检索某篇文章  
<?php query_posts('p=3'); ?> 只显示post-id为3的页面。 

13.query_posts() 函数的一些常见用法

是page 而不是post 
<?php 
query_posts('page_id=1'); // 只显示id=1的页面 
query_posts('pagename=about); // 只显示页面名字叫 about的页面 
?> 
 
获取文章 get_posts是一个简单的你可利用多循环以不同方式罗列文章的标签,例如你想罗列最新的5个条目,代码可如下: 
<div> 
 <h2> 最新文章</h2> 
 <?php 
 $previous_posts = get_posts('numberposts=5'); 
 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>
  上面的代码中变量"numberposts=?"则告诉 WordPress 在循环里索引和填充多少文章,WordPress文章格式函数setup_postdata()将会自动填充所需函数!

14.如何按分类和按月显示一个Archvies存档页面

<?php while(have_posts()) : the_post(); ?> 
<ul><?php wp_get_archives('type=monthly&show_post_count=1') ?></ul> 
<ul><?php wp_list_cats('sort_column=name&optioncount=1') ?></ul> 
 
<?php endwhile; ?> 
使用wp_list_cats 标签显示一个按分类排列的WordPress 存档列表和 wp_get_archives 显示基于日期的存档列表。

15.页面模板头部声明

<?php/* 
Template Name: Gallery 
*/ 
?>

16.为不同的分类指定不同的图像

<?php if (is_category('1') ): ?> 
<img src='<?php bloginfo('template_url'); ?>/01.bmp' alt='' /> 
<?php elseif (is_category('16') ): ?> 
<img src='<?php bloginfo('template_url'); ?>/16.bmp' alt='' /> 
<?php endif; ?> 
is_paged() : 主页/Category/Archive页是否以多页显示 :这里是指是否是分页的第 2,3,4,,,,页。 
!is_paged() 是指分页的第一页 

17.如何添加幻灯片 banner广告

在开始之前我们需要先下载 SmoothGallery 2.0 
 
1) 将所需文件放到合适的地方,解压下载到的smoothgallery 
将解压得到的css文件夹复制到wordpress 目录wp-content/themes/your_theme_name 
将scripts 文件夹复制到wp-content/themes/your_theme_name 
 
2) 在 header.php里添加: 
 
<script type="text/javascript" src="<?php 
bloginfo('template_url'); ?>/huandeng/scripts/mootools.v1.11.js"></script> 
<!--<!–JS SmoothGallery–>--> 
<script type="text/javascript" src="<?php 
bloginfo('template_url'); ?>/huandeng/scripts/jd.gallery.js"></script> 
<?php /*?><?php wp_head();?><?php */?>去掉此行,不然不能实现。不知道什么原因? 
wp_head();里面是其他插件里面的样式或者是其他调用代码,如果去掉此行的话将无法显示了,所以只能把他们所需要的代码直接写进主题的样式模板内进行统一调用。可以解决问题。 
 
3) 新建一个文件huandeng.php 添加代码: 
<!-- Initialization of SmoothGallery--> 
<script type="text/javascript"> 
 function startGallery() { 
 var myGallery = new gallery($('myGallery'), { 
 timed: false 
 }); 
 } 
 window.addEvent('domready',startGallery); 
 </script> 
<!-- Creation of the html for the gallery --> 
 <div class="content"> 
 <div id="myGallery"> 
 <div class="imageElement"> 
 <h3>Item 1 Title</h3> 
 <p>Item 1 Description</p> 
 <a href="#" title="open image" class="open"></a> 
 <img src=' <?php 
bloginfo('template_url'); ?>/huandeng/images/brugges2006/1.jpg' class="full" /> 
 <img src='<?php 
bloginfo('template_url'); ?>/huandeng/images/brugges2006/1-mini.jpg' class="thumbnail" /> 
</div> 
 <div class="imageElement"> 
 <h3>Item 2 Title</h3> 
 <p>Item 2 Description</p> 
 <a href="#" title="open image" class="open"></a> 
 <img src=" <?php 
bloginfo('template_url'); ?>/huandeng/images/brugges2006/2.jpg" class="full" /> 
 <img src="<?php 
bloginfo('template_url'); ?>/huandeng/images/brugges2006/2-mini.jpg" class="thumbnail" /> 
 </div> 
 <div class="imageElement"> 
 <h3>Item 3 Title</h3> 
 <p>Item 3 Description</p> 
 <a href="#" title="open image" class="open"></a> 
 <img src="<?php 
bloginfo('template_url'); ?>/huandeng/images/brugges2006/3.jpg" class="full" /> 
 <img src="<?php 
bloginfo('template_url'); ?>/huandeng/images/brugges2006/3-mini.jpg" class="thumbnail" /> 
 </div> 
 <div class="imageElement"> 
 <h3>Item 4 Title</h3> 
 <p>Item 4 Description</p> 
 <a href="#" title="open image" class="open"></a> 
 <img src="<?php 
bloginfo('template_url'); ?>/huandeng/images/brugges2006/4.jpg" class="full" /> 
 <img src="<?php 
bloginfo('template_url'); ?>/huandeng/images/brugges2006/4-mini.jpg" class="thumbnail" /> 
 </div> 
 <div class="imageElement"> 
 <h3>Item 5 Title</h3> 
 <p>Item 5 Description</p> 
 <a href="#" title="open image" class="open"></a> 
 <img src="/huandeng/images/brugges2006/5.jpg" class="full" /> 
 <img src="/huandeng/images/brugges2006/5-mini.jpg" class="thumbnail" /> 
 </div> 
 <div class="imageElement"> 
 <h3>Item 6 Title</h3> 
 <p>Item 6 Description</p> 
 <a href="#" title="open image" class="open"></a> 
 <img src="<?php 
bloginfo('template_url'); ?>/huandeng/images/brugges2006/6.jpg" class="full" /> 
 <img src="<?php 
bloginfo('template_url'); ?>/huandeng/images/brugges2006/6-mini.jpg" class="thumbnail" /> 
 </div>
<div class="imageElement"> 
 <h3>Item 7 Title</h3> 
 <p>Item 7 Description</p> 
 <a href="#" title="open image" class="open"></a> 
 <img src="<?php 
bloginfo('template_url'); ?>/huandeng/images/brugges2006/7.jpg" class="full" /> 
 <img src="<?php 
bloginfo('template_url'); ?>/huandeng/images/brugges2006/7-mini.jpg" class="thumbnail" /> 
 </div> 
 <div class="imageElement"> 
 <h3>Item 8 Title</h3> 
 <p>Item 8 Description</p> 
 <a href="#" title="open image" class="open"></a> 
 <img rc="<?php 
bloginfo('template_url'); ?>/huandeng/images/brugges2006/8.jpg" class="full" /> 
 <img src="<?php 
bloginfo('template_url'); ?>/huandeng/images/brugges2006/8-mini.jpg" class="thumbnail" /> 
 </div> 
 </div> 
 </div> 
</div>
4) 在需要引用的地方添加以下代码:(不知道什么原因调用 huandeng.php代码后,此区域将脱离文件流,而且没有办法让它归位,只能将其直接写入 index.php文件内部) 
 
<?php include(TEMPLATEPATH.'/huandeng.php'); ?> 
 
这里面有一个 
5) 自定义gallery的具体显示效果。 打开文件wp-content/themes/your_theme_name/css/jd.gallery.css,在这里修改gallery的宽和高。(通过修改jd.gallery.css完全对这个slideshow 根据自己的主题进行个性化。^_^) #myGallery, #myGallerySet, #flickrGallery { 590px; height: 250px; z-index:5; border: 1px solid #000; } 默认的字号对于中文太小了,可以调整 slideshow 下方信息栏的高度及文字的字号,只需要修改 .jdGallery .slideInfoZone(滑动信息栏高度、颜色等参数) .jdGallery .slideInfoZone h2(信息栏内标题样式) jdGallery .slideInfoZone p(信息栏文本样式) *上滑动栏宽度; .jdGallery .carousel { position: absolute; 100%; margin: 0px; left: 0; top: 0; height: 90px;/*上滑动栏宽度;*/ background: #333; color: #fff; text-indent: 0; overflow: hidden; } 你还可以修改 wp-content/themes/your_theme_name/scripts/jd.gallery.js 来改变 gallery 的展示效果(Smooth Gallery提供了多种不同的显示效果,你可以根据需要进行修改)

18.5个你不知道的 Wordpress函数

1. wp_mail()基本上是一个超级简单的函数,允许你通过一些简单的代码轻松
地向任何人发送电子邮件。例如: 
<?php 
$to = 'user@example.com'; 
$subject = 'Hello from my blog!'; 
$message = 'Check it out -- my blog is emailing you!' 
$mail = wp_mail($to, $subject, $message); 
if($mail) echo 'Your message has been sent!'; 
else echo 'There was a problem sending your message. Please try again.'; 
?> 
2. wp_loginout():主题中显示“登录”链接 
3. clean_url():入 URL并测试它的结构是否正确。如果链接前面缺少 http://
它可以自动添加,转换符号为正确的 HTML, 
4. wpautop() :这个函数用来转换换行符字符串为<br />标记,并把双换行符
作为一个新的段落的开始,在前一段加入</p>,并在新的段落加上<p>。 
原文地址:https://www.cnblogs.com/qiandu/p/4034836.html