SOHO创业

分类: 建站知识

wordpress建站必备的函数大全

wordpress作为一个经典的建站系统,只要你懂技术,可以通过wordpress搭建出,任何形式的网站。要想搭建wordpress网站,wordpress函数是必须要熟练掌握的。

下面这些就是本人整理的一些wordpress建站必备的函数

一、基本的条件判断函数

is_home():是否为主页

is_single():是否为内容页 (Post)

is_page():是否为内容页 (Page)

is_category():是否为 Category/Archive 页

is_tag():是否为标签 (Tag) 存档页

is_date():是否为指定日期存档页

is_year():是否为指定年份存档页

is_month():是否为指定月份存档页

is_day():是否为指定日存档页

is_time():是否为指定时间存档页

is_archive():是否为存档页

is_search():是否为搜索结果页

is_404():是否为 "HTTP 404: Not Found" 错误页

is_paged():主页 /Category/Archive 页是否以多页显示

二、header部分常用函数

<?php bloginfo('name'); ?>:博客名称 (Title)

<?php bloginfo('stylesheet_url'); ?>:CSS 文件路径

<?php bloginfo('pingback_url'); ?>:PingBack URL

<?php bloginfo('template_url'); ?>:模板文件路径

<?php bloginfo('version'); ?>:WordPress 版本

<?php bloginfo('atom_url'); ?>:Atom URL

<?php bloginfo('rss2_url'); ?>:RSS 2.o URL

<?php bloginfo('url'); ?>:博客 URL

<?php bloginfo('html_type'); ?>:博客网页 HTML 类型

<?php bloginfo('charset'); ?>:博客网页编码

<?php bloginfo('description'); ?>:博客描述

<?php wp_title(); ?>:特定内容页 (Post/Page) 的标题

三、wordpress模板制作过程中常用到的函数和命令

<?php get_header(); ?>:调用 Header 模板

<?php get_sidebar(); ?>:调用 Sidebar 模板

<?php get_footer(); ?>:调用 Footer 模板

<?php the_content(); ?>:显示内容 (Post/Page)

<?php if(have_posts()):?>:检查是否存在 Post/Page

<?php while(have_posts()):the_post(); ?>:如果存在Post/Page则予以显示

<?php endwhile; ?>:While 结束

<?php endif; ?>:If 结束

<?php the_time('字符串') ?>:显示时间,时间格式由"字符串"参数决定,具体参考 PHP 手册

<?php comments_popup_link(); ?>:正文中的留言链接,如果使用 comments_popup_script(); 则新窗口打开链接

<?php the_title(); ?>:内容页 (Post/Page) 标题

<?php the_permalink() ?>:内容页 (Post/Page) URL

<?php the_category(',') ?>:特定内容页 (Post/Page) 所属 Category

<?php the_author(); ?>:作者

<?php the_ID(); ?>:特定内容页 (Post/Page) ID

<?php edit_post_link(); ?>:如果用户已登录并具有权限,显示编辑链接

<?php get_links_list(); ?>:显示 Blogroll 中的链接

<?php comments_template(); ?>:调用留言/回复模板

<?php wp_list_pages(); ?>:显示 Page 列表

<?php wp_list_categories(); ?>:显示 Categories 列表

<?php next_post_link('%link '); ?>:下一篇文章链接

<?php previous_post_link('%link'); ?>:上一篇文章链接

<?php get_calendar(); ?>:日历

<?php wp_get_archives() ?>:显示内容存档

<?php posts_nav_link(); ?>:导航,显示上一篇/下一篇文章链接

<?php include(TEMPLATEPATH . '/文件名'); ?>:嵌入其他文件,可为定制的模板或其他类型文件

四、与wordpress模板相关的一些其它函数

<?php _e('Message'); ?>:输出相应信息

<?php wp_register(); ?>:显示注册链接

<?php wp_loginout(); ?>:显示登录/注销链接

<!–next page–>:将当前内容分页

<!–more–>:将当前内容截断,以不在主页/目录页显示全部内容

<?php timer_stop(1); ?>:网页加载时间(秒)

<?php echo get_num_queries(); ?>:网页加载查询量

wordpress主题二次开发时常用到的函数

wordpress是一款非常强大的建站系统,在默认的功能基础上可以根据自己的实际需求二开(二次开发)出任意你想要的功能。

下面收集整理了一些wordpress二次开发时常会用到的函数,熟练掌握了这些函数,就可以在自己制作的模板中展示任何的内容了。

废话不说,上代码。

<?php the_content(); ?> 日志内容 
<?php if(have_posts()) : ?> 确认是否有日志 
<?php while(have_posts()) : the_post(); ?> 如果有,则显示全部日志 
<?php endwhile; ?> 结束PHP函数”while” 
<?php endif; ?> 结束PHP函数”if” 
<?php get_header(); ?> header.php文件的内容 
<?php get_sidebar(); ?> sidebar.php文件的内容 
<?php get_footer(); ?> footer.php文件的内容 
<?php the_time("m-d-y") ?> 显示格式为”10-12-13″的日期 
<?php comments_popup_link(); ?> 显示一篇日志的留言链接 
<?php the_title(); ?> 显示一篇日志或页面的标题 
<?php the_permalink() ?> 显示一篇日志或页面的永久链接/URL地址 
<?php the_category(",") ?> 显示一篇日志或页面的所属分类 
<?php the_author(); ?> 显示一篇日志或页面的作者 
<?php the_ID(); ?> 显示一篇日志或页面的ID
<?php edit_post_link(); ?> 显示一篇日志或页面的编辑链接 
<?php get_links_list(); ?> 显示链接 
<?php comments_template(); ?> comments.php文件的内容 
<?php wp_list_pages(); ?> 显示一份博客的页面列表 
<?php wp_list_cats(); ?> 显示一份博客的分类列表 
<?php next_post_link("%link") ?> 下一篇日志的URL地址 
<?php previous_post_link("%link") ?> 上一篇日志的URL地址 
<?php get_calendar(); ?> 调用日历 
<?php wp_get_archives() ?> 显示一份博客的日期存档列表 
<?php posts_nav_link(); ?> 显示较新日志链接(上一页)和较旧日志链接(下一页) 
<?php bloginfo("description"); ?> 显示博客的描述信息
<?php bloginfo("name"); ?> 网站标题 
<?php wp_title(); ?> 日志或页面标题 
<?php bloginfo("stylesheet_url"); ?> WordPress主题样式表文件style.css的相对地址 
<?php bloginfo("pingback_url"); ?> WordPress博客的Pingback地址 
<?php bloginfo("template_url"); ?> WordPress主题文件的相对地址 
<?php bloginfo("version"); ?> 博客的Wordpress版本 
<?php bloginfo("url"); ?> WordPress博客的绝对地址 
<?php bloginfo("charset"); ?> 网站的字符编码格式

wordpress调用随机文章和随机推荐文章

wordpress调用随机文章的代码如下:

<?php
$query = array(
'post_type' => 'post',
'orderby' => 'rand'
);
$posts = new WP_Query( $query );
if ( $posts->have_posts() ) {
while( $posts->have_posts() ) :
$posts->the_post();
the_content();
endwhile;
}
wp_reset_query();
?>

wordpress随机调用置顶推荐的文章代码:

<?php
//获取置顶文章的ID串
$rand_id = get_option( 'sticky_posts' );
$query = array(
'post__in' => $rand_id,
'post_type' => 'post',
'orderyby' => 'rand',
'numberposts' => 2
);
$posts = new WP_Query( $query );
if ( $posts->have_posts() ) {
while( $posts->have_posts() ) :
$posts->the_post();
the_content();
endwhile;
}
wp_reset_query();
?>

wordpress为不同页面调用不同头部header文件代码

在wordpress模板制作的过程中常常会遇到,需要为不同页面调用不同头部模板的情况,下面这段代码可以完美解决这个问题。

<?php 
if ( is_home() ) : 
 get_header( 'home' ); 
elseif ( is_404() ) : 
 get_header( '404' ); 
else : 
 get_header(); 
endif; 
?> 

这里为首页和404错误页分别调用了header-home.php和header-404.php这两个不同的头部文件。只需要将要显示的代码分别放入header-home.php和header-404.php文件即可。

wordpress父分类和归档页调用子分类名称和链接

category和archives调用下级子分类的名称和链接

<?php if ( is_category() ) { $this_category=g et_category( $cat ); } ?>
<?php if ( $this_category->category_parent ) 
$this_category = wp_list_categories( 'orderby=id&show_count=0&title_li=&use_desc_for_title=1&child_of='.$this_category->category_parent."&echo=0" ); 
else $this_category = wp_list_categories( 'orderby=id&depth=1&show_count=0&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID."&echo=0" ); 
if ( $this_category ) { ?>
<ul>
<?php echo $this_category; ?>
</ul>
<?php } ?>

wordpress显示相同父分类目录下的所有子分类目录

在制作wordpress列表模板的时候,经常会用到调用同录分类目录的情况,这段代码完美解决了这一问题。

<?php
$cat = get_queried_object();
$list = wp_list_categories( [
    'taxonomy' => $cat->taxonomy,
    'child_of' => $cat->parent,
    'title_li' => '',
    'echo'     => 0,
] );
if ( $list ) {
    echo "<ul>$list</ul>";
}
?>

wordpress子分类调用父分类名称和链接

专为导航而生,在wordpress模板制作过程中常常会在做breadcrumbs导航时会用到,子分类调用父分类的名称和链接,下面这段简洁的代码,可以完美解决这个问题。

<?php echo get_category_parents( $cat, true, ' &raquo; ' ); ?>

下面这种方法也可以,不过代码不够简洁。

<?php
if ( is_category() ) {
    // Get the current category term id.
    $query_obj = get_queried_object();
    $term_id   = $query_obj->term_id;
 
    echo get_term_parents_list( $term_id, 'category' );
}
?>

第三种方法,调用分类目录名称和链接,作为导航。

<?php
if ( ( is_tax() || is_category() || is_tag() ) ) {
    $trail     = '';
    $home      = '/<a href="' . get_home_url() . '">Home</a>';
    $query_obj = get_queried_object();
    $term_id   = $query_obj->term_id;
    $taxonomy  = get_taxonomy( $query_obj->taxonomy );
 
    if ( $term_id && $taxonomy ) {
        // Add taxonomy label name to the trail.
       // $trail .=  '/' . $taxonomy->labels->menu_name;
        // Add term parents to the trail.
        $trail .= '/' . get_term_parents_list( $term_id, $taxonomy->name, array( 'inclusive' => false ) );
    }
 
    // Print trail and add current term name at the end.
    echo '<p class="breadcrumb-trail">' . $home . $trail . $query_obj->name . '</p>';
}
?>

wordpress按栏目别名调用最新内容

用wordpress制作网站模板时,可以使用以下代码,即可实现按栏目别名调用该栏目下的最新内容。

<?php $cmntCnt = 1;
$cat=get_category_by_slug('news'); 
?>
<?php $posts = get_posts( "category=$cat->term_id&numberposts=3" ); ?>
<?php if( $posts ) : ?>
<?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
<li>  
<h6 class="wow fadeInUp" data-wow-delay="0.2s"><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php echo cut_str($post->post_title,38); ?></a></h6><span class="wow fadeInUp" data-wow-delay="0.4s"><?php the_category(', '); ?></span><span class="text-muted wow fadeInUp" data-wow-delay="0.4s"> - <?php the_time(get_option('date_format')) ?></span>
<p class="text-muted d-block mt-3 wow fadeInUp" data-wow-delay="0.6s"><?php echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 100, '…'); ?></p>
</li>

<?php endforeach; ?>
<?php endif; ?>
<?php wp_reset_query();?>

WordPress the_category与single_cat_title

在wordpress网站主题开发用常会用到调用分类目录的名称,the_category与single_cat_title都可以调用出分类目录的名称。

<?php single_cat_title(); ?>
<?php the_category(); ?>

但是,不少人搞不清楚二者有什么区别,其实很简单。

如果用single_cat_title调用分类名称,在分类里没文章时可以显示分类名称。用the_category分类名称,在分别里没文章时不会显示分类名称。

就这么点区别,其它的没什么区别。

WordPress调用当前文章作者头像

制作wordpress博客主题时经常会到用,需要调用wordpress当前文章作者头像的时候,用下面的这段代码即可。

<?php if (have_posts()) : the_post(); update_post_caches($posts); ?>
//wodepress.com
<?php echo get_avatar( get_the_author_email(), '80' );?>//80代表头像的大小
<?php endif; ?>

wordpress获取父页面的2种方法

wordpress模板制作的过程中有时会用到获取父页面的ID,下面是wordpress获取父页面的2种方法。

方法一

global $post;
$id = $post -> ID;
$parent = get_post_ancestors($post -> ID);
print_r($parent);

方法二

global $post;
$parent_id = $post -> post_parent;
echo $parent_id;

wordpress调用当前页面ID

在制作wordpress模板时常会用到的,wordpress调用当前页面ID代码。

<?php global $post;
$id = $post -> ID;
echo $id; ?>

在需要的位置上这段代码,即可显示出该页面的ID。