Skip to content

Instantly share code, notes, and snippets.

@xingstarx
Created April 27, 2019 03:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xingstarx/45dfbe556ae2e3ee935674170ebf5814 to your computer and use it in GitHub Desktop.
Save xingstarx/45dfbe556ae2e3ee935674170ebf5814 to your computer and use it in GitHub Desktop.
wordpress 常用函数
//语言和编码
<?php language_attributes(); ?> //语言属性
//根据不同情况来读取 title
<?php
global $page, $pages;
wp_title( '|', true, 'right'); //在标题的左方或右方
bloginfo( 'name' ); //博客的名称
$site_description = get_bloginfo( 'description', 'Display' ); //博客的描述
if( $site_description && ( is_home() || is_front_page() ) ) //如果是首页,则输出博客名称和描述
echo " | $side_description";
if( $paged >= 2 || $page >=2 ) //如果是文章页面,则输出页码和博客名称
echo ' | ' .sprintf( __( 'Page %s', 'alice' ), max( $paged, $page ) );
?>
//博客信息
<?php bloginfo( 'name' ); ?> //博客名称
<?php bloginfo( 'description' ); ?> //描述 Just another wordpress blog
<?php bloginfo( 'admin_email' ); ?> //博主邮箱
<?php bloginfo( 'stylesheet_directory' ); ?> //子主题路径
<?php bloginfo( 'stylesheet_url' ); ?> //子主题样式表链接
<?php bloginfo( 'template_directory' ); ?> //父主题路径
<?php bloginfo( 'template_url' ); ?> //父主题路径
<?php bloginfo( 'atom_url' ); ?> //feed路径 home/feed/atom
<?php bloginfo( 'rss2_url' ); ?> //feed路径 home/feed
<?php bloginfo( 'rss_url' ); ?> //rss路径 home/feed/rss
<?php bloginfo( 'pingback_url' ); ?> //xmlrpc 文件路径
<?php bloginfo( 'charset' ); ?> //字符编码 utf-8
<?php bloginfo( 'html_type' ); ?> //text/html
<?php bloginfo( 'language' ); ?> //en-US
//获取路径
<?php esc_url( home_url( '/' ) ); ?> //主目录路径
//布局相关
<?php get_header(); ?> //获取头部
<?php get_footer(); ?> //获取底部
<?php wp_head(); ?> //获取 wordpress 头部,以便以后扩展
<?php wp_footer(); ?> //获取 wordpress 底部。
//获取设置
<?php echo get_setting( 'home' ); ?> //获取主页路径
//获取分类链接列表
<?php the_category( ',' ); ?>
//主循环
<?php if( have_posts() ) : while( have_posts() ) : the_post(); ?>
<?php endwhile; else: ?>
<?php endif; ?>
//文章相关
<?php the_title(); ?> //文章标题
<?php the_permalink(); ?> //文章链接
<?php the_title_attribute(); ?> //文章标题属性
<?php the_ID(); ?> //文章ID
<?php edit_post_link( __( 'Edit &#187;', 'theme' ) ); ?> //编辑文章链接
//分页
<?php next_posts_link( __( '&laquo;Older posts','theme' ) ); ?>
<?php previous_posts_link( __( 'Newer posts&laquo;' ) ); ?>
//输出日志时间
<?php the_time( 'Y' . "-" . 'n' . "-" . 'd' ); ?>
<?php the_time( 'Y-n-d' ); ?>
//输出作者链接
<?php the_author(); ?>
<?php the_author_posts_link( array( 'class', 'author' ) ); ?> //链接中带有 class 为 author 的类
//输出带格式的摘要
<?php echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 280,'...','UTF-8'); ?>
//判断是哪一个分类
<?php if ( in_category( 'document' ) || in_category( 'sulotion' ) ): ?>
<?php echo '...'; ?>
<?php endif; ?>
//留言
<?php comments_popup_link( 'No comments', '1 comment', '% comments' ); ?> //留言状态
<?php comments_template(); ?> //留言模板
//获取正文
<?php the_content(); ?>
<?php get_template_part( 'content', 'single' ); ?>
//前一篇文章
<?php if( !previous_post_link() ): ?><?php endif; ?>
//后一篇文章
<?php if( !next_post_link() ): ?><?php endif; ?>
//获取头像
<?php echo get_avatar( $id or email, $size, $default, $alt ); ?>
<?php echo get_avatar( $curauth->user_email, 48 ); ?>
//作者相关
<?php echo $curauth->nickname; ?> //获取昵称
<?php echo $curauth->user_url; ?> //获取网站路径
<?php echo $curauth->user_description; ?> //获取作者描述
//按某一个分类输出指定数目的文章
<?php query_posts( 'category_name=sulotion&showposts=5' ); ?>
@xingstarx
Copy link
Author

is_home() 判断是否为首页,并且显示的不是一个静态页面
is_front_page()判断是否为首页,包括首页显示的是一个静态页面
is_search()是否为搜索页
is_404()是否为404页面
is_category()是否为分类目录归档
is_author()是否为作者归档页面
is_day()是否为按天归档页面
is_month()是否为按月归档页面
is_year()是否为按年归档页面
is_tag()是否为标签归档页面
is_single()是否为文章页面
is_page()是否为页面单页
is_date()是否按日期归档页面,相当于包括is_day()、is_month()、is_year()
is_archive()是否为归档页面,相当于包括is_category()、is_author()、is_month()、is_day()、is_year()、is_tag()
is_singular()相当于is_single()||is_page()||is_attachment()
———–
2012.1.12 补充 is_sticky() 置顶文章判断。
2012.6.25 补充 is_singular 用于判断单页

@xingstarx
Copy link
Author

xingstarx commented May 1, 2019

该函数返回指定文章中含有指定关键字自定义字段的值
<?php $meta_values = get_post_meta($post_id, $key, $single); ?>

 if (have_posts()) ://如果有文章
 while (have_posts()) : the_post(); //开启主循环
   $xzmeta  = get_post_meta(get_the_ID(),'xzmeta',true);
 endwhile; 
 endif;

@xingstarx
Copy link
Author

single_cat_title() 用来显示当前category页面对应的categoryName

@xingstarx
Copy link
Author

<?php the_excerpt(); ?> 获取文章摘要

@xingstarx
Copy link
Author

<?php the_post_thumbnail('thumbnail', array('class' => 'thumb')); ?> 显示文章的特色图片

@xingstarx
Copy link
Author

//Default WordPress
the_post_thumbnail( 'thumbnail' ); // Thumbnail (150 x 150 hard cropped)
the_post_thumbnail( 'medium' ); // Medium resolution (300 x 300 max height 300px)
the_post_thumbnail( 'medium_large' ); // Medium Large (added in WP 4.4) resolution (768 x 0 infinite height)
the_post_thumbnail( 'large' ); // Large resolution (1024 x 1024 max height 1024px)
the_post_thumbnail( 'full' ); // Full resolution (original size uploaded)

@xingstarx
Copy link
Author

if ( function_exists('add_image_size')) {
    add_image_size('customized-post-thumb', 233, 218);
    add_image_size('dac-card-thumb', 253, 237);
}

添加自定义的特色图片缩略图

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment