wordpress常用代替插件的代码
1:WordPress搭建内部链接,不需要插件,随机文章函数
<?php $rand_posts = get_posts('numberposts=10&orderby=rand');foreach($rand_posts as $post) : ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach;?>
这里的数字10是随机显示10条随机的文章
2:Wordpress面包屑导航代码
1.在wordpress博客当前主题的php文件(没有就创建一个)中添加以下代码:
function get_breadcrumbs()
{
global $wp_query;
if ( !is_home() )
{
// Start the UL
echo '<ul class="breadcrumbs">';
// Add the Home link
echo '<li><a href="'. get_settings('home') .'">'. get_bloginfo('name') .'</a></li>';
if ( is_category() )
{
$catTitle = single_cat_title( "", false );
$cat = get_cat_ID( $catTitle );
echo "<li> > ". get_category_parents( $cat, TRUE, " > " ) ."</li>";
}
elseif ( is_archive() && !is_category() )
{
echo "<li> > Archives</li>";
}
elseif ( is_search() )
{
echo "<li> > Search Results</li>";
}
elseif ( is_404() )
{
echo "<li> > 404 Not Found</li>";
}
elseif ( is_single() )
{
$category = get_the_category();
$category_id = get_cat_ID( $category[0]->cat_name );
echo '<li> > '. get_category_parents( $category_id, TRUE, " > " );
echo the_title('', '', FALSE) ."</li>";
}
elseif ( is_page() )
{
$post = $wp_query->get_queried_object();
if ( $post->post_parent == 0 )
{
echo "<li> > ".the_title('', '', FALSE)."</li>";
}
else
{
$title = the_title('', '', FALSE);
$ancestors = array_reverse( get_post_ancestors( $post->ID ) );
array_push($ancestors, $post->ID);
foreach ( $ancestors as $ancestor )
{
if( $ancestor != end($ancestors) )
{
echo '<li> > <a href="'. get_permalink($ancestor) .'">'. strip_tags( apply_filters( 'single_post_title', get_the_title( $ancestor ) ) ) .'</a></li>';
}
else
{
echo '<li> > '. strip_tags( apply_filters( 'single_post_title', get_the_title( $ancestor ) ) ) .'</li>';
}
}
}
}
// End the UL
echo "</ul>";
}
}
2.在显示面包屑导航的位置添加以下调用代码
<? php
if (function_exists('get_breadcrumbs'))
{
get_breadcrumbs();
}
? >
3.在主题的css样式文件中添加以下样式代码
ul.breadcrumbs {list-style: none; font-size:12px;}
ul.breadcrumbs li {float: left; margin-right:5px;}
3:WordPress自动显示缩略图
1:找到使用的主题模板的functions.php文件在<?php和?>之间添加如下代码:
function thumb_img($soContent){
$soImages = '~<img [^\>]*\ />~';
preg_match_all( $soImages, $soContent, $thePics );
$allPics = count($thePics[0]);
if( $allPics > 0 ){
echo "<span id='thumb'>";
echo $thePics[0][0];
echo '</span>';
}
else {
echo "<span id='thumb'>";
echo "<img src='";
echo bloginfo('template_url');
echo "/images/thumb.gif'></span>";
}
}
2:找到index.php文件即首页文件,找到the_content();或相似的代码在它之前添加如下代码:
thumb_img($post->post_content);
3:添加缩略图样式CSS代码
#thumb{margin:5px 15px 5px 5px;width:145px;height:120px;border:3px solid #eee;float:left;overflow:hidden;}
#thumb img{max-height:186px;max-width:186px}
4:实现wordpress文章页调用同分类上/下一篇文章
默认直接调用的代码
<?php previous_post_link('上一篇: %link') ?>
<?php next_post_link('下一篇: %link') ?>
当文章处于首篇或末篇时,会显示空白,但可以通过增加判断还填补空白:
<?php if (get_previous_post()) { previous_post_link('上一篇: %link');} else {echo "已是最后文章";} ?>
<?php if (get_next_post()) { next_post_link('下一篇: %link');} else {echo "已是最新文章";} ?>
在get_previous_post()函数中指定一下文章所属分类ID便能使代码完全有效:
<?php
$categories = get_the_category();
$categoryIDS = array();
foreach ($categories as $category) {
array_push($categoryIDS, $category->term_id);
}
$categoryIDS = implode(",", $categoryIDS);
?>
<?php if (get_previous_post($categoryIDS)) { previous_post_link('上一篇: %link','%title',true);} else { echo "已是最后文章";} ?>
<?php if (get_next_post($categoryIDS)) { next_post_link('上一篇: %link','%title',true);} else { echo "已是最新文章";} ?>
5:wordpress截取字段的代码:
<?php $post_query = new WP_Query('showposts=2');
while ($post_query->have_posts()) : $post_query->the_post();
$do_not_duplicate = $post->ID; ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<p><?php echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 200,"..."); ?><a href="<?php the_permalink(); ?>">Read More</a></p>
<?php endwhile;?>
6:更新文章用到的sql命令
操作1本地mysql更新状态
UPDATE `wp_posts` SET `post_status` = 'pending' where id> 1481
操作2更新时间 分钟,秒钟不变
UPDATE `wp_posts` SET `post_date` = REPLACE(`post_date`, '2013-01-09', '2013-01-13'), `post_date_gmt` = REPLACE(`post_date_gmt`, '2013-01-09', '2013-01-13') WHERE id > 1481
操作3导出数据 以时间为条件内容不为空
SELECT `post_author`, `post_date`, `post_date_gmt`, `post_content`, `post_title`, `post_excerpt`, `post_status`, `comment_status`, `ping_status`, `post_password`, `post_name`, `to_ping`, `pinged`, `post_modified`, `post_modified_gmt`, `post_content_filtered`, `post_parent`, `guid`, `menu_order`, `post_type`, `post_mime_type`, `comment_count` FROM `wp_posts` WHERE 1 = instr(`post_date`,'2013-01-13') and `post_content`<>""
操作4执行 发布当天的文章
UPDATE `wp_posts` SET `post_status` = 'publish' where 1 = instr(`post_date`,'2013-01-13')
7:更改wordpress数据表前缀
例:将wp表前缀更改为es
更改11个表,执下下列命令,多的其它插件表请注意
操作1
RENAME table `wp_commentmeta` TO `es_commentmeta`;
RENAME table `wp_comments` TO `es_comments`;
RENAME table `wp_links` TO `es_links`;
RENAME table `wp_options` TO `es_options`;
RENAME table `wp_postmeta` TO `es_postmeta`;
RENAME table `wp_posts` TO `es_posts`;
RENAME table `wp_terms` TO `es_terms`;
RENAME table `wp_term_relationships` TO `es_term_relationships`;
RENAME table `wp_term_taxonomy` TO `es_term_taxonomy`;
RENAME table `wp_usermeta` TO `es_usermeta`;
RENAME table `wp_users` TO `es_users`;
操作2执行下列命令
UPDATE `es_options` SET `option_name` = replace (`option_name`,'wp_user_roles','es_user_roles');
UPDATE `es_usermeta` SET `meta_key` = replace (`meta_key`,'wp_capabilities','es_capabilities');
UPDATE `es_usermeta` SET `meta_key` = replace (`meta_key`,'wp_user_level','es_user_level');
UPDATE `es_usermeta` SET `meta_key` = replace (`meta_key`,'wp_autosave_draft_ids','es_autosave_draft_ids');
操作3需要在wp-config.php中找到
$table_prefix = ‘wp_’;
将它替换为
$table_prefix = ‘es_’;
8:预设WordPress的FTP信息
当添加,删除,升级 WordPress 插件或者直接升级 WordPress 的时候,WordPress 总是提示让你输入 FTP 帐号信息,非些烦人。其实我们可以在 wp-config.php 中定义下面这些常量来让 WordPress 自动获取 FTP 信息,而无需每次手动输入:
//添加 FTP 信息,防止每次都提示输入
define('FTP_HOST', 'ftp.yoursite.com');
define('FTP_USER', 'Your_FTP_Username');
define('FTP_PASS', 'Your_FTP_password');
//如果你的 FTP 可以使用 SSL 连接,把下面设置为 True
define('FTP_SSL', true);