WordPress各模板制作流程
常用函数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20…
常用函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
//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(); ?> //文章或者静态页面标题
<?php wp_title( ‘|’, true, ‘right’ ); ?> <?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 comments_popup_link(); ?> //显示到文章留言的链接
<?php edit_post_link(); ?> //文章编辑链接
<?php next_post_link(‘%link’); ?> //下一篇文章链接
<?php previous_post_link(‘%link’); ?> //上一篇文章链接
<?php posts_nav_link(); ?> //上一篇和下一篇文章链接
<?php get_calendar(); ?> //显示文章日历
|
wordpress调用缩略图/特色图url
1
2
3
4
5
6
7
|
<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 } ?>
|
主题选项调取
1
2
|
<?php echo stripslashes(get_option(‘code_in_head’)); ?> //头部
<?php echo stripslashes(get_option(‘code_in_foot’)); ?> //尾部
|
模板文件:functions/themeoption/themeoption.php
Options设置
1
2
|
//使用缓存data_cache.php文件
<?php include(“functions/options/data_cache.php”); ?>
|
目前设置的Options
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
//网站设置
$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
$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_fax=get_option(‘mytheme_bottom_fax’);//公司传真
$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_site_sidebar_img=get_option(‘mytheme_site_sidebar_img’);//侧边栏图片
$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’);//底部背景图片
$mytheme_chat_img=get_option(‘mytheme_chat_img’);//商务通侧边图片
|
调用
1
2
3
4
5
6
|
//直接调用
<?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 } ?>
|
文章调用主循环
1
2
3
|
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
|
自定义菜单
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<?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’ => 2,
‘walker’ => new wp_bootstrap_navwalker() ,
‘fallback_cb’ => ‘tannistha_menu_fallback’
));
?>
|
简单调取
1
2
3
4
5
6
7
8
9
10
|
<?php
wp_nav_menu( array(
‘theme_location’ => ‘footer-navigation’,
‘depth’ => 2,
‘container’ => false,
‘menu_class’ => ‘list-unstyled’,
‘fallback_cb’ => ‘wp_page_menu’,
//添加或更改walker参数
‘walker’ =>”));
?>
|
页面TDK调取
seo选项
1
2
3
4
5
6
7
8
|
<?php
if(is_single()){
$id=get_the_ID(); //获取文章id
$description=get_post_meta($id, “描述”,true);
$keyworeds=get_post_meta($id, “关键字”,true);
$posttags = get_the_tags(); if ($posttags) { foreach($posttags as $tag) { $tagess.=$tag->name.‘,’;}}
}
?>
|
调取标题
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
<?php
$singletitle_p =get_post_meta($post->ID, “title_p”,true);
$singletitle =get_post_meta($post->ID, “title”,true);
global $wp_query;
$cat_obj = $wp_query->get_queried_object_id();
$term_id = $cat_obj;
$cat_title=get_option(‘cat_title_’.$term_id);
$cat_keword = get_option(‘cat_keword_’.$term_id);
$cat_description = get_option(‘cat_description_’.$term_id);
$cat_title_p=get_option(‘cat_title_p_’.$term_id);
if (function_exists(‘is_tag’) && is_tag()) {
single_tag_title($language_th2.” | “); echo ‘ | ‘.get_bloginfo(‘name’); }
elseif (is_archive()) {
if( $cat_title){echo $cat_title;}else{wp_title(”); echo ‘ | ‘.get_bloginfo(‘name’);}
}
elseif (is_search()) {
echo $language_th2.‘ | ‘.wp_specialchars($s).‘ | ‘.get_bloginfo(‘name’); }
elseif (!(is_404()) && (is_single()) || (is_page())) {
if( $singletitle){echo $singletitle;echo ‘ | ‘.get_bloginfo(‘name’);}else{ wp_title(”); echo ‘ | ‘.get_bloginfo(‘name’); }
}
elseif (is_404()) {
echo $language_th3.‘ | ‘.get_bloginfo(‘name’); }
else if (is_home()) {
if(get_option(‘mytheme_title’)){echo get_option(‘mytheme_title’);}else{ bloginfo(‘name’); echo ‘ | ‘; bloginfo(‘description’); }
}else{wp_title(”); echo ‘ | ‘.get_bloginfo(‘name’);}
if ($paged>1) {
echo ‘_page’. $paged;echo ‘ | ‘; bloginfo(‘description’); }
?>
|
调取keywords和description
1
2
|
<meta name=“keywords” content=“<?php if(is_single()&&!$keyworeds){echo $tagess;}elseif(theme_keyworeds()){theme_keyworeds();}else{echo $cat_keword;} ?>“ />
<meta name=“description” content=“<?php if(is_single()&&!$description){echo ”;}elseif(theme_description()){theme_description();}else{echo $cat_description;} ?>“ />
|
加载自定义模板标签
1
2
|
//single.php页面根据发布类型调取
<?php get_template_part( ‘content’, get_post_format() );?>
|
调取当前分类ID
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
//普通分类及页面
<?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应用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
<?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_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_box_value13 = get_post_meta($post->ID,“other1”, true);
$meta_box_value14 = get_post_meta($post->ID,“other2”, true);
$meta_box_value15 = get_post_meta($post->ID,“other3”, true);
$meta_box_value16 = get_post_meta($post->ID,“other4”, true);
$meta_box_value_t_13 = get_post_meta($post->ID,“othert1”, true);
$meta_box_value_t_14 = get_post_meta($post->ID,“othert2”, true);
$meta_box_value_t_15 = get_post_meta($post->ID,“othert3”, true);
$meta_box_value_t_16 = get_post_meta($post->ID,“othert4”, true);
?>
//调用
<?php if($meta_box_value5){?><?php echo $meta_box_value5; ?><?php } ?>
|
自定义post-type调取文章
指定栏目调取
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
<?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 查询
} ?>
|
指定自定义分类调取
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
<?php
$args = array( ‘post_type’ => ‘products’, ‘posts_per_page’ => 8, ‘paged’ => get_query_var(‘paged’) );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->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; ?>
//首页滚动banner
<?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”>
<h2><?php the_title();?></h2>
<?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; ?>
|
指定自定义类别调取
1
2
3
4
5
6
7
8
|
<?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; ?>
|
常用调取文章方法
category.php页面使用,使用query_posts(); WordPress的主查询函数。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<?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(); ?>
|
调取分类及分类下的文章
调取自定义分类及分类下的文章
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
<ul class=“mtree”>
<?php
$taxonomys = get_categories( array( ‘taxonomy’ => ‘products_category’ ) );
if ( !empty( $taxonomys ) ):
foreach ( $taxonomys as $taxonomy ):
?>
<li><b></b><a href=“<?php echo get_category_link($taxonomy->term_id); ?>“><?php echo $taxonomy->name; ?></a>
<ul>
<?php
$args = array(
‘post_type’ => ‘products’, //自定义文章类型名称
‘posts_per_page’ => 8, //输出的文章数量,这个可以是缺省值,不用设置
‘paged’ => get_query_var( ‘paged’ ),
‘tax_query’ => array(
array(
‘taxonomy’ => ‘products_category’, //自定义分类法名称
‘terms’ => $taxonomy->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();
?>
<li><a href=“<?php the_permalink() ?>“><i class=“fa fa-caret-right”></i>
<?php the_title(); ?>
</a></li>
<?php
endwhile;
wp_reset_query(); //重置 query 查询
}
?>
</ul>
</li>
<?php endforeach; endif; ?>
|
调取常规分类及分类下的文章
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<ul class=“mtree”>
<?php
$categories = get_categories( array( ‘orderby’ => ‘name’, ‘parent’ => 0 ) );
if ( !empty( $categories ) ):
foreach ( $categories as $categorie ):
?>
<li><b></b><a href=“<?php echo get_category_link($categorie->term_id); ?>“><?php echo $categorie->name; ?></a>
<ul>
<?php
$args = array( ‘orderby’ => ‘rand’, ‘showposts’ => 8, ‘cat’ => $categorie->term_id );
$query_posts = new WP_Query();
$query_posts->query( $args );
while ( $query_posts->have_posts() ): $query_posts->the_post();
?>
<li><a href=“<?php the_permalink(); ?>“><i class=“fa fa-caret-right”></i>
<?php the_title(); ?>
</a></li>
<?php endwhile;?>
</ul>
</li>
<?php endforeach; endif; ?>
</ul>
|
禁止收录不必要的页面
404页面、搜索、tag页面
1
2
3
4
5
|
<?php
if ((is_404()) || (is_search()) || (is_tag())) {
echo ‘<meta name=”robots” content=”noindex, noarchive”>’;
}
?>
|
插件制作选择禁止收录按钮
1
|
<?php if( !is_home() && get_field(‘rel_noindex’) == true ): ?><meta name=“robots” content=“noindex, noarchive”><?php endif; ?>
|
添加多语言版本
1
2
3
4
5
6
|
<?php if( !is_home() && get_field(‘rel_noindex’) == true ): ?><meta name=“robots” content=“noindex, noarchive”><?php endif; ?>
<?php $current_url = add_query_arg(array()); if($current_url){ ?>
<link rel=“alternate” href=“https://fr.wpcwallpanel.com<?php echo $current_url;?>“ hreflang=“fr-fr” />
<link rel=“alternate” href=“https://wpcwallpanel.com<?php echo $current_url;?>“ hreflang=“en-us” />
<link rel=“alternate” href=“https://wpcwallpanel.com<?php echo $current_url;?>“ hreflang=“x-default” />
<?php } ?>
|
侧边栏Sidebar.php的判断
1
2
3
4
5
6
7
|
<?php if(‘products’ == get_post_type() ) {?>
<?php } elseif(false == get_post_format()) {?>
<?php } else {?>
<?php }?>
|
类别:WordPress开发、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
评论功能已经关闭!