当前位置: 首页 >> 推荐 >> 文章正文

wordpress cms常用代码必备语句

336x280

Nextgen Gallery引用代码
1、插入一个幻灯片: [ slideshow id=x w=width h=height ]

效果预览: http://nextgen.boelinger.com/slideshow

2、插入一个相册: [ album id=x template=extend ] 或 [ album id=x template=compact ]

#extend 是比较大的展示扩展框

#compact 是比较紧的展示扩展框

效果预览: http://nextgen.boelinger.com/album/

3、 插入一个图集: [ nggallery id=x ] or [ nggallery id=1 template=sample1 ]

显示图集有可以有不同的边框效果,方法是加上template=sample1,后面参数设置有sample1, sample2, sample3, sample4, sample5, sample6

效果预览: http://nextgen.boelinger.com/gallery-page

4、插入一张图片: [ singlepic id=x w=width h=height mode=web20/watermark float=left/right ]

#mode 是可选项,可选参数有 web20(倒影效果),watermark(水印效果)

#float 是可选项,可选参数有 left(左对齐),right(右对齐)

效果预览: http://nextgen.boelinger.com/singlepic/

5、插入图片浏览器: [ imagebrowser id=x ]

效果预览: http://nextgen.boelinger.com/image-browser

6、显示相册或图集里有相同标签的照片 [ nggtags album=tags ] or [ nggtags gallery=tags ]

#tags 是指你给照片所设置的标签

效果预览:http://nextgen-gallery.com/albumtags/

效果预览:http://nextgen-gallery.com/gallery-tags/

其本展示调用有这些,但还有一些物殊展示:动感幻灯片展示和视频类展示。如果要实现这两个展示要先分别安装两个插件: NextGEN Monoslideshow 和Revolt

动感幻灯片效果预览:http://nextgen-gallery.com/slideshow/nextgen-monoslideshow/

视频效果预览:http://nextgen-gallery.com/wordtube-20/plugin-support/

摄氏度 – 2011-07-22

使用分类模板是一个方法,事实上除此之后,还有更简单一些的,比如说就在 category.php 这个模板里使用函数进行分类的判断然后显示内容:

<?php if(is_category('product')) { ?>
    <img src="<?php bloginfo('template_directory'); ?>/images/b_product.jpg" width="905" height="239">
<?php } ?>
<?php if(is_category('news')) { ?>
    <img src="<?php bloginfo('template_directory'); ?>/images/b_news.jpg" width="905" height="239">
<?php } ?>

以上代码的意见是,当这个分类名称是 product 时,则显示图片 b_product.jpg,同样的道理,当分类名称为 news 时则显示图片 b_news.jpg

这个放进内容页也同样适用,也可以使用这样的代码

<?php if(in_category('product')) { ?>
    <img src="<?php bloginfo('template_directory'); ?>/images/b_product.jpg" width="905" height="239">
<?php } ?>
<?php if(in_category('news')) { ?>
    <img src="<?php bloginfo('template_directory'); ?>/images/b_news.jpg" width="905" height="239">
<?php } ?>

知更鸟 – 2011-07-21

复制一份主题的archive.php改名为:category-88.php
后面的数字是分类id,改为想显示特殊图片的分类ID,之后该分类就会调用这个模版,DIY这个模版中DIY,让它与其它页面不同,多个分类就多复制几个
正文内面就复杂一些了,具体方法:
复制三份single.php文件,重命名,例如:single_a.php、single_b.php、single_all.php
用下面的代码,替换原single.php所有内容

<?php
if ( in_category('a') ) {
include(TEMPLATEPATH . '/single_a.php');
}
elseif ( in_category('b') ) {
include(TEMPLATEPATH . '/single_b.php');
}
else {
include(TEMPLATEPATH . '/single_all.php');
}
?>

其中( in_category(’a') )字母a,是分类别名,貌似添加ID也行
最后分别个性化复制的模版

实现在自己博客的不同页面显示不一样的边栏,或者其他模块的话,下面这个段代码语句显得相当有用:

<?php if (is_home()) {?><!– 只在首页显示 –>

<此处放置只需要在首页显示的代码>

<?php }?>

类似地我们还可以写出

<?php if (is_single()) {?><!– 只在单个内容页显示 –>

<此处放置只需要在首页显示的代码>

<?php }?>

<?php if (is_page()) {?><!– 只在页面如(关于)显示 –>

<此处放置只需要在首页显示的代码>

<?php }?>

不要小看这句简单的判断语句,其实用起来非常的好用,例如对于大部分博客来说我们的友情链接都只需要在首页显示就OK了。我们就可以在边栏文件sidebar.php中合适的位置加入用这样的代码

<?php if (is_home()) {?><!– 只在首页显示 –>

<h3>博客链接</h3>

<ul>

<?php get_links(-1, ‘<li>’, ‘</li>’, ‘ – ‘); ?>

</ul>

<?php }?>

需要注意的是,WordPress后台的weight添加代码是不支持该语句的,所以必须还是要编辑模板文件中(通常是sidebar.php)的代码。

在WordPress中自动获取文章中的图片 – sofish


<?php

$soContent = $post->post_content;
$soImages = '~<img [^\>]*\ />~';
preg_match_all( $soImages, $soContent, $thePics );
$allPics = count($thePics[0]);
switch ( $allPics > 0 ) {
case $allPics = 1:
echo $thePics[0][0]; // 显示文章中的第一张图片
break; // 当图片数量有1个时,不再执行
default:
echo "这里应该显示图片,而不是sofish"; // 这里加入没图片时显示的默认图片
};
?>
或者
<?php
$soContent = $post->post_content;
$soImages = '~<img [^\>]*\ />~';
preg_match_all( $soImages, $soContent, $thePics );
$allPics = count($thePics[0]);
if( $allPics > 0 ){
echo $thePics[0][0];
}
else {
echo "这里应该显示图片,而不是sofish";
}
?>

<?php
$soContent = $post->post_content;
$soImages = '~<img [^\>]*\ />~';
preg_match_all( $soImages, $soContent, $thePics );
$allPics = count($thePics[0]);
switch ( $allPics > 0 ) {
case $allPics = 1:
echo $thePics[0][0]; // 显示文章中的第一张图片
break; // 当图片数量有1个时,不再执行
default:
echo "这里应该显示图片,而不是sofish"; // 这里加入没图片时显示的默认图片
};
?>>

或者


<?php
$soContent = $post->post_content;
$soImages = '~<img [^\>]*\ />~';
preg_match_all( $soImages, $soContent, $thePics );
$allPics = count($thePics[0]);
if( $allPics > 0 ){
echo $thePics[0][0];
}
else {
echo "这里应该显示图片,而不是sofish";
}
?>

发表评论