常用函数
//WordPress、主题或插件都可以通过给wp_head函数来向网站的head标签中加入内容
<?php wp_head(); ?>
<?php wp_footer(); ?>
//头部信息函数
<?php flush(); ?> //在<body>前面,</head>后面添加PHP代码,用于提高程序运行效率
<html <?php language_attributes(); ?>> //语言函数
<meta charset="<?php bloginfo( 'charset' ); ?>"> //编码
<body <?php body_class(); ?>> //调用头部模板文件
<?php site_url(); ?> //站点根链接
<?php wp_title('',true,''); ?> //文章或者静态页面标题
<?php bloginfo('name'); ?> //站点名称
<?php bloginfo('description'); ?> //站点描述
<?php bloginfo('stylesheet_url'); ?> //样式文件链接
<?php bloginfo('pingback_url'); ?> //pingback 链接
<?php bloginfo('template_url'); ?> //模板文件所在目录链接
<?php bloginfo('version'); ?> //WordPress 版本
<?php bloginfo('atom_url'); ?> //atom 链接
<?php bloginfo('rss2_url'); ?> //rss2 链接
<?php bloginfo('url'); ?> //网站根链接
<?php bloginfo('html_type'); ?> //html 版本
<?php bloginfo('charset'); ?> //字符集 //模板函数
<?php the_title_attribute() ?> //输出文章标题属性
<?php the_time('m-d-y'); ?> //显示时间 //参考:WordPress 博客文章时间格式the_time()设置 - https://vpura.com/2019/05/16/code/714/
<?php the_tags('Tag: ', ' , ' , ''); ?> //调取文章标签,用逗号分隔 //参考:wordpress 的tags标签自定义样式 - https://vpura.com/2019/05/10/function/677/
<?php the_title(); ?> //文章标题
<?php the_permalink(); ?> //文章链接
<?php the_shortlink() ?> //输出文章短链接
<?php the_excerpt() ?> //输出文章摘要
<?php the_content() ?> //输出文章内容
<?php the_meta() ?> //输出文章自定义meta信息
<?php the_category(); ?> //文章分类
<?php the_author(); ?> //文章作者
<?php the_author_link() ?> //输出文章作者链接
<?php the_ID(); ?> //文章 ID
<?php post_views(' ', '');?> //<a href="https://bricks.shspecial.com/%e9%9d%9e%e6%8f%92%e4%bb%b6%e6%b7%bb%e5%8a%a0%e6%96%87%e7%ab%a0%e6%b5%8f%e8%a7%88%e6%ac%a1%e6%95%b0%e7%bb%9f%e8%ae%a1%e5%8a%9f%e8%83%bd.html" data-mce-href="https://bricks.shspecial.com/%e9%9d%9e%e6%8f%92%e4%bb%b6%e6%b7%bb%e5%8a%a0%e6%96%87%e7%ab%a0%e6%b5%8f%e8%a7%88%e6%ac%a1%e6%95%b0%e7%bb%9f%e8%ae%a1%e5%8a%9f%e8%83%bd.html">文章浏览次数统计</a>
<?php comments_popup_link(); ?> //显示到文章留言的链接
<?php edit_post_link(); ?> //文章编辑链接
<?php next_post_link('%link'); ?> //下一篇文章链接
<?php previous_post_link('%link'); ?> //上一篇文章链接
<?php posts_nav_link(); ?> //上一篇和下一篇文章链接
<?php get_calendar(); ?> //显示文章日历
<?php echo mb_strimwidth(strip_tags(apply_filters('the_excerpt', $post->post_content)), 0, 300,"..."); ?> //调用自定义长度文章简介
<?php single_cat_title(); ?> //栏目标题
<?php the_archive_description( '<div class="taxonomy-description">', '</div>' ); ?> //这个函数显示当前档案的描述,显示类别、标签、术语或作者描述。
wordpress调用缩略图/特色图url
<a href="<?php the_post_thumbnail_url( 'full' ); ?>"><?php the_post_thumbnail(); ?></a>
//判断
<?php if ( has_post_thumbnail() ) { ?>
<?php the_post_thumbnail(); ?>
<?php } else {?>
<img src="这里填写默认图片地址" />
<?php } ?>
主题选项调取
<?php echo stripslashes(get_option('code_in_head')); ?> //头部
<?php echo stripslashes(get_option('code_in_foot')); ?> //尾部
Options设置
//使用缓存data_cache.php文件
<?php include("functions/options/data_cache.php"); ?>
目前设置的Options
$mytheme_site_logo=get_option('mytheme_site_logo');//网站logo
$mytheme_site_white_logo=get_option('mytheme_site_white_logo');//网站纯白logo
$mytheme_site_black_logo=get_option('mytheme_site_black_logo');//网站纯黑logo
$mytheme_site_favicon=get_option('mytheme_site_favicon');//网站favicon标识
$mytheme_top_ad=get_option('mytheme_top_ad');//顶部宣传位置
$mytheme_main_contact_mail=get_option('mytheme_main_contact_mail');//联系邮箱
$mytheme_main_chat=get_option('mytheme_main_chat');//商务通按钮链接
$mytheme_socil_facebook=get_option('mytheme_socil_facebook');//Facebook地址
$mytheme_socil_twitter=get_option('mytheme_socil_twitter');//
$mytheme_socil_twitter=get_option('mytheme_socil_linkedin');//
$mytheme_socil_twitter=get_option('mytheme_socil_instagram');//
$mytheme_socil_youtube=get_option('mytheme_socil_youtube');//Youtube地址
$mytheme_socil_pin=get_option('mytheme_socil_pin');//Pin地址
$mytheme_bottom_des=get_option('mytheme_bottom_des');//简介设置
$mytheme_bottom_address=get_option('mytheme_bottom_address');//地址设置
$mytheme_bottom_phone=get_option('mytheme_bottom_phone');//公司电话
$mytheme_bottom_sj_phone=get_option('mytheme_bottom_sj_phone');//联系手机号码
$mytheme_bottom_skype=get_option('mytheme_bottom_skype');//Skype
$mytheme_bottom_whatsapp=get_option('mytheme_bottom_whatsapp');//WhatsApp
$mytheme_bottom_wechat=get_option('mytheme_bottom_wechat');//WeChat
$mytheme_footer_pic=get_option('mytheme_footer_pic');//底部图标
$mytheme_site_copyright=get_option('mytheme_site_copyright');//底部版权信息
$mytheme_footer_bg_pic=get_option('mytheme_footer_bg_pic');//底部背景图片
调用
//直接调用
<?php echo get_option('header_bzms'); ?>
<?php if(get_option("header_bzms")){ echo get_option('header_bzms'); } ?>
//引用data_cache.php后,多个判断可用||相隔
<?php echo $mytheme_socil_facebook; ?>
<?php if($mytheme_footer_tel){?><?php echo $mytheme_footer_tel; ?><?php } ?>
文章调用主循环
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?> <?php the_content(); ?> <?php endwhile; ?>
自定义菜单
<?php wp_nav_menu( array( 'theme_location' => 'primary', 'container' => 'div', 'container_class' => 'navbar-collapse collapse clearfix', 'container_id' => 'navbarSupportedContent', 'menu_class' => 'navigation clearfix', 'menu_id' => '', 'echo' => true, 'fallback_cb' => 'wp_page_menu', 'before' => '', 'after' => '', 'link_before' => '', 'link_after' => '', 'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>', 'depth' => 3, 'walker' => new wp_bootstrap_navwalker() , 'fallback_cb' => 'tannistha_menu_fallback' )); ?>
简单调取
<?php wp_nav_menu( array( 'theme_location' => 'sidebar', 'depth' => 2, 'container' => false, 'menu_class' => 'lf_sidebar nav list flex-column text-3-5 font-weight-medium mb-5-5', 'menu_id' => 'lf_sidebar', 'link_before' =>'<i class="fas fa-chevron-right text-1 position-relative top-1 pe-3-5"></i>', 'fallback_cb' => 'wp_page_menu', //添加或更改walker参数 'walker' =>'')); ?>
加载自定义模板标签
<?php get_template_part('inquiry'); ?>
//single.php页面根据发布类型调取
<?php get_template_part( 'content', get_post_format() );?>
调取当前分类ID
//普通分类及页面 <?php // 取出当前分类 id: $categories[0]->term_id $categories = get_the_category(); $term_id = $categories[0]->term_id; $cat_name = $categories[0]->name; ?> //自定义分类及页面 <?php // 取出当前分类 id: $categories[0]->term_id $categories = get_the_terms( $post->ID, 'products_category' ); $term_id = $categories[0]->term_id; $cat_name = $categories[0]->name; ?>
meta-box中tab.php应用
//滑动窗口产品介绍 $meta_box_value5 = get_post_meta( $post->ID, "tab-title1", true ); $meta_box_value6 = get_post_meta( $post->ID, "tab-title2", true ); $meta_box_value7 = get_post_meta( $post->ID, "tab-title3", true ); $meta_box_value8 = get_post_meta( $post->ID, "tab-title4", true ); $meta_box_value9 = do_shortcode( wpautop( get_post_meta( $post->ID, "tab-description1", true ) ) ); $meta_box_value10 = do_shortcode( wpautop( get_post_meta( $post->ID, "tab-description2", true ) ) ); $meta_box_value11 = do_shortcode( wpautop( get_post_meta( $post->ID, "tab-description3", true ) ) ); $meta_box_value12 = do_shortcode( wpautop( get_post_meta( $post->ID, "tab-description4", true ) ) ); //其他信息 $meta_ot_value13 = get_post_meta( $post->ID, "other13", true ); $meta_ot_value14 = get_post_meta( $post->ID, "other14", true ); $meta_ot_value15 = get_post_meta( $post->ID, "other15", true ); $meta_ot_value16 = get_post_meta( $post->ID, "other16", true );
自定义post-type调取文章
1、指定自定义分类下的具体栏目
<?php
$args = array( 'post_type' => 'homeslider', 'posts_per_page' => 7, 'tax_query' => array(array('taxonomy' => 'homeslider_category','terms' => 5)));
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li> <a href="<?php the_post_thumbnail_url( 'one' ); ?>" class="diamond <?php echo get_post_meta( $post->ID, '_class_id', true ); ?> lightbox" data-plugin-options="{'type':'image'}">
<div class="content"> <img src="<?php the_post_thumbnail_url( 'full' ); ?>" class="img-fluid plugin-random-images" alt="<?php the_title(); ?>"/> </div>
</a> </li>
<?php endwhile; wp_reset_query();?>
2、在当前自定义栏目下
<?php
$args = array(
'post_type' => 'products', //自定义文章类型名称
'posts_per_page' => 8, //输出的文章数量,这个可以是缺省值,不用设置
'paged' => get_query_var('paged'),
'tax_query' => array(
array(
'taxonomy' => 'products_category',//自定义分类法名称
'terms' => $term_id, //id 为 64 的分类。也可是多个分类 array(12,64)
),
)
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();?>
<div class="col-lg-3 col-md-4 col-sm-6">
<div class="shopping-col">
<div class="product-img"> <img src="<?php post_thumbnail_src(); ?>" alt="<?php the_title(); ?>">
<div class="product-over-box"> <a href="<?php the_permalink() ?>"><i class="fa fa-link"></i></a> </div>
</div>
<div class="product-info text-center">
<h4><a href="<?php the_permalink() ?>">
<?php the_title(); ?>
</a></h4>
</div>
</div>
</div>
<?php endwhile; wp_reset_query(); //重置 query 查询
} ?>
3、指定自定义分类
<?php
$args = array( 'post_type' => 'homeslider', 'posts_per_page' => 6 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="item <?php echo get_post_meta( $post->ID, '_class_id', true ); ?>"> <img src="<?php echo post_thumbnail_src(); ?>" alt="<?php the_title();?>" class="slide-image"/>
<div class="container">
<div class="carousel-caption text-left">
<h1><?php the_title();?></h1>
<?php the_excerpt() ?>
<p><a class="btn btn-lg btn-primary" href="<?php echo get_post_meta( $post->ID, '_class_url', true ); ?>" role="button">Read more</a></p>
</div>
</div>
</div>
<?php endwhile; ?>
4、通过meta_key调取
<?php
$args = array( 'post_type' => 'homeslider', 'posts_per_page' => 5, 'meta_key' => '_class_id','meta_value' =>'customer' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="single-product clearfix">
<div class="image"> <a href="<?php echo get_post_meta( $post->ID, '_class_url', true ); ?>"></a> <img src="<?php the_post_thumbnail_url( 'two' ); ?>" alt="<?php the_title();?>"/> </div>
<a href="<?php echo get_post_meta( $post->ID, '_class_url', true ); ?>" class="title"><?php the_title();?></a> </div>
<?php endwhile; ?>
常用调取文章方法
https://bricks.shspecial.com/%e8%b0%83%e5%8f%96%e6%96%87%e7%ab%a0%e7%9a%84%e6%96%b9%e6%b3%95.html
1、category.php页面使用,使用query_posts(); WordPress的主查询函数。
<?php
$num = rand(1, 9);
//$num = 1;
$cat_ID = get_query_var('cat');
query_posts( array( 'cat' => $cat_ID,'posts_per_page' => 10,'order' =>ASC, 'paged' => get_query_var('paged') ) );
if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<li class="clearfix">
<div class="col-md-4"><a href="<?php the_permalink() ?>"><img src="<?php the_post_thumbnail_url( 'three' ); ?>" alt="<?php the_title(); ?>"></a></div>
<div class="col-md-8">
<div class="team-details">
<h5><a href="<?php the_permalink() ?>">
<?php the_title(); ?>
</a></h5>
<div style="padding:10px 0 5px;">By GREENLINE / <a href="javascript:void(0);"> <i class="icn fa fa-eye"><span class="sr-only">icon</span></i> <span>
<?php post_views(' ', '');?>
</span> </a> / <i class="fa fa-calendar"></i>
<?php the_time('d-m-y'); ?>
</div>
</div>
<?php echo mb_strimwidth(strip_tags(apply_filters('the_excerpt', $post->post_content)), 0, 200,"..."); ?>
<div><a href="<?php the_permalink() ?>">>> Read More</a></div>
</div>
</li>
<?php endwhile; wp_reset_query(); ?>
2、任意地方调取栏目文章
<?php
$num = rand(1, 3);
//$num = 1;
query_posts( array( 'cat' => 1, 'posts_per_page' => 3 ) );
if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div class="col-sm-9 col-md-6 col-lg-4 mb-4 mb-lg-0"> <a href="<?php the_permalink(); ?>" class="text-decoration-none" data-cursor-effect-hover="plus">
<div class="card border-0">
<div class="card-img-top position-relative overlay">
<div class="position-absolute bottom-10 right-0 d-flex justify-content-end w-100 py-3 px-4 z-index-3"> <span class="text-center bg-primary text-color-light font-weight-semibold text-5-5 line-height-2 px-3 py-2"> <span class="position-relative z-index-2"> <?php the_time('d'); ?> <span class="d-block custom-font-size-1 positive-ls-2 px-1"><?php the_time('m'); ?>月</span> </span> </span> </div>
<img src="<?php the_post_thumbnail_url( 'two' ); ?>" class="img-fluid" alt="<?php the_title(); ?>" /> </div>
<div class="card-body py-4 px-0"> <span class="d-block text-color-grey font-weight-semibold positive-ls-2 text-2">桌木鸟发布</span>
<h3 class="text-transform-none font-weight-bold text-5 text-color-hover-primary mb-2"><?php the_title(); ?></h3>
<p class="mb-2"><?php echo mb_strimwidth(strip_tags(apply_filters('the_excerpt', $post->post_content)), 0, 150,"..."); ?> </p>
<span class="custom-view-more d-inline-flex font-weight-medium text-color-primary"> 了解更多 <img width="27" height="27" src="<?php bloginfo('template_url'); ?>/img/demos/construction/icons/arrow-right.svg" alt="" data-icon data-plugin-options="{'onlySVG': true, 'extraClass': 'svg-fill-color-primary ms-2'}" style="width: 27px;" /> </span> </div>
</div>
</a> </div>
<?php endwhile; wp_reset_query(); ?>
禁止收录不必要的页面
<?php
if ((is_404()) || (is_search()) || (is_tag())) {
echo '<meta name="robots" content="noindex, noarchive">';
}
?>
侧边栏Sidebar.php的判断
<?php if('products' == get_post_type() ) {?>
<?php } elseif(false == get_post_format()) {?>
<?php } else {?>
<?php }?>
Advanced Custom Fields调取
<?php if( get_field('z_address') ) { ?>
<?php the_field('z_address'); ?>
<?php } ?>

