# Wordpress - Theme
# ããŒãéçºã®æµã
- PHP / WordPress ã®ãšã©ãŒãä¿®æ£ããããã®éã
wp-config.php
ã«define('WP_DEBUG', true)
ãè¿œèšããŠè¡ããšè¯ãã - ãã§ãã¯ãªã¹ã (opens new window)ã§ãã§ãã¯ãè¡ã
- Unit Test (opens new window)ãè¡ã
- Validation (opens new window)ãè¡ã
- JS ã®ãšã©ãŒãä¿®æ£ãã
- è€æ°ã®ãã©ãŠã¶ã§ãã¹ããè¡ã
- äžèŠãª commentããããã°èšå®ãTODO ãæ¶ã
# åºæ¬
# ãã¡ã€ã«æ§æ
ããŒããã¡ã€ã«ã¯htdocs/wp-content/themes
ã«ãããäžèšã® 3 ã€ããæ§æããã
- StylesheetïŒ
style.css
ïŒ - Template files
- Functions file(
functions.php
)
# Stylesheet
- éåžžã® CSS ãã¡ã€ã«ã§ãã
- ããŒãã®ã¡ã¿æ å ±ãäžçªäžã«èšèŒãããWordpress ã¯ãã®æ å ±ã䜿ã£ãŠããŒããèå¥ããŠãããããååã®éè€ãªã©ã¯èš±ãããªãã
/*
Theme Name: Twenty Thirteen
Theme URI: http://wordpress.org/themes/twentythirteen
Author: the WordPress team
......
*/
# Function File
functions.php
ã¯ãã©ã°ã€ã³ã®ããã«ãµããŸããåäžã®ããŒãå
ã§äœ¿ãæ©èœã¯functions.php
ãé©ããŠããããè€æ°ã®ããŒãã§å
±éããŠäœ¿ãæ©èœã¯ Plugins ãšããŠäœæããã»ããããã
䜿çšã§ãããã¡ã³ã¯ã·ã§ã³äžèŠ§ (opens new window)
ãã®ãã¡ã€ã«ã®ç®çã¯äžèšã®éãã
- css ãã¡ã€ã«ã js ãã¡ã€ã«ã Enqueue(
wp_enqueue_scripts
)ããæã«äœ¿ã - äžèšã®æ©èœãæå¹ã«ããæã«äœ¿ã
- Sidebars
- Navigation Menus
- Post Thumbnails
- Post Formats
- Custom Headers
- Custom Backgrounds
- è€æ°ã®ãã³ãã¬ãŒããã¡ã€ã«ã§å ±éããŠäœ¿ãæ©èœãå®çŸ©ãã
- ãµã€ããªãŒããŒããªãã·ã§ã³èšå®ãããšãã®ãã«ã¹ã¿ãã€ãºã¡ãã¥ãŒã®èšå®ã«äœ¿ã
# Themplate Files
ãã³ãã¬ãŒããã¡ã€ã«ã¯ãHTML, PHP, Worspress Template Tag (opens new window)ãããªãã
極端ãªäŸã§ã¯ãäžã€ã®index.php
ã«ãã¹ãŠãèšèŒããããšãã§ããããåããŠäœæããããšã§æ§ã
ãªã«ã¹ã¿ãã€ãºãå¯èœã«ãªãã
äžèšã®ãã¡ã€ã«åã¯äºçŽæžã¿ã§ãããç¹å¥ãªåœ¹å²ãæããã
name | desc |
---|---|
style.css | ã¡ã€ã³ã® stylesheetãããŒãã®ã¡ã¿æ å ±ãããããšããŠå«ããå¿ èŠãããã |
rtl.css | RTL ç°å¢ã§æ¡çšããã CSSããã®ãã¡ã€ã«ã¯ RTLer plugin ã䜿ã£ãŠäœæã§ããã |
index.php | ã¡ã€ã³ã®ãã³ãã¬ãŒããå¿ é ãæçš¿äžèŠ§ãã«ããŽãªäžèŠ§ãã¿ã°äžèŠ§ãæ€çŽ¢çµæäžèŠ§ãªã©äžèŠ§ãšåã®ä»ããã®ãã¹ãŠã衚瀺ããããã®ãã³ãã¬ãŒããšããŠæ±ããšããã |
comments.php | ã³ã¡ã³ãã®ãã³ãã¬ãŒã |
front-page.php | ã«ãŒããã¹ã§è¡šç€ºããããŒãžã®ãã³ãã¬ãŒãã¯åžžã«ããã |
home.php | ãèšå®ïŒè¡šç€ºèšå®ïŒããŒã ããŒãžã®è¡šç€ºïŒåºå®ããŒãžã衚瀺ãã«ãªã£ãŠããå Žåã®ãæçš¿ããŒãžïŒäžèŠ§è¡šç€ºããããŒãžïŒãã®ãã³ãã¬ãŒã |
single.php | åå¥æçš¿ã®ãã³ãã¬ãŒãã |
single-{post-type}.php | ã«ã¹ã¿ã æçš¿ã¿ã€ãããšã®ãåå¥æçš¿ã®ãã³ãã¬ãŒãã |
page.php | åºå®ããŒãžçšãã³ãã¬ãŒã |
category.php | ã«ããŽãª* |
tag.php | ã¿ã°* |
taxonomy.php | ã¿ã¯ãœãã* |
author.php | Author* |
date.php | æ¥æã»æéçš* |
archive.php | ã¢ãŒã«ã€ãçšãã³ãã¬ãŒããäžèš*ããªãå Žåã«æ¡çšãããã |
search.php | æ€çŽ¢çµæçš |
attachment.php | åäžã®æ·»ä»ãã¡ã€ã«ãé²èŠ§ããçšã®ãã³ãã¬ãŒã |
image.php | åäžã®ç»åãã¡ã€ã«çšã®ãã³ãã¬ãŒãããªããã°attachment.php ãæ¡çšãããã |
404.php | 404 |
# ãã³ãã¬ãŒãã®åªå é äœ
# åºæ¬éšåã®æ§ç¯
# äžæºå
æäœéå¿ èŠãªäžèšã®ãã¡ã€ã«ãé 眮ãã
- index.php
- style.css
- screenshot.jpg (管çç»é¢ã®ãµã ãã€ã«çš)
# ããã
<!DOCTYPE html>
<html <?php language_attributes() ?>>
<head>
<meta charset="<?php bloginfo('charset') ?>">
<meta name="description" content="<?php bloginfo('description'); ?>"/>
<title>
<?php bloginfo('name'); ?><?php wp_title(); ?>
</title>
</head>
language_attributes()
- html ã¿ã°ã® lang å±æ§ãåºåãã
bloginfo('charset')
- meta ã¿ã°ã® charset å±æ§ãåºåãã
bloginfo('name');
- ãµã€ãåãåºå
bloginfo('description');
- ãµã€ãã®èª¬æãåºå
wp_title()
- çæããããããŒãžãã«å¿ããŠæé©ãªã¿ã€ãã«ãåºå
- åŒæ°ã§ãåºåãæåã®èšå®ãå¯èœ
# èªåçæãããã¯ã©ã¹ã BODY ã«ã€ã³ãžã§ã¯ã
<body <?php body_class() ?>>
body_class()
- WP ãèªåçæããã¯ã©ã¹ãã€ã³ãžã§ã¯ãããã
- ããŒãžãåºå¥ããã¯ã©ã¹åããŠãŒã¶ããã°ã€ã³äžãã©ããã瀺ãã¯ã©ã¹åããã©ã°ã€ã³ã䜿çšããã¯ã©ã¹åãªã©ãåºåããã
body_class('my-class')
ã®ããã«åŒæ°ãæå®ãããšãããšãèªåçæãããã¯ã©ã¹ã®äžçªæåŸã«æå®ããã¯ã©ã¹ïŒmy-class
ïŒãè¿œå ããŠããã
# ã³ã³ãã³ãã衚瀺ãã
<?php if (have_posts()): while (have_posts()): the_post(); ?>
<article <?php post_class() ?>>
<h1><?php the_title() ?></h1>
<?php the_content() ?>
</article>
<?php endwhile; endif; ?>
# ã«ãŒãã®äœæ
have_posts()
ãšthe_post()
ãçµã¿åãããthe_post()
ã¯ãäœååŠçããããã«ãŠã³ããããã1 ã€ãã€åãåºããããŒã¿ããã³ãã¬ãŒãã¿ã°ã§ãã¡ããšæ±ããããã«ãããããåŠçãè¡ã£ãŠãã
# èªåçæãããã¯ã©ã¹ãæçš¿ã«ã€ã³ãžã§ã¯ã
post_class()
- WP ãçæããã¯ã©ã¹ãèšäºããšã«ã€ã³ãžã§ã¯ãããã
- IDã»ã«ããŽãªãªã©ã®æ å ±ãããã©ã°ã€ã³ã䜿ãã¯ã©ã¹åãåºåãããã
post_class('my-class')
ã®ããã«åŒæ°ãæå®ãããšãèªåçæãããã¯ã©ã¹ã®äžçªæåŸã«my-class
ãè¿œå ããŠããã
# ã³ã³ãã³ãã®è¡šç€º
the_title()
ãthe_content()
ã䜿ã£ãŠã³ã³ãã³ããæåããã- ãããã¯åºæ¬çã«ã«ãŒãå ã§ãã䜿ããªãã
# CSS
ã¡ã€ã³ã® CSS ãèªã¿èŸŒãã
<link rel="stylesheet" href="<?php echo get_stylesheet_uri(); ?>">
CSS ã®å
é ã«ã¯charset
ãèšèŒããŠãã
@charset "UTF-8";
# æçš¿æ¥ã»ã«ããŽãªã®è¡šç€º
<div>
<time datetime="<?php echo get_the_date('Y-m-d'); ?>">
<?php echo get_the_date(); ?>
</time>
<span>
<?php the_category(', ') ?>
</span>
</div>
äžèšã¯ããããã«ãŒãã®äžã§äœ¿çšå¯èœã
get_the_date('圢åŒ')
- æçš¿æ¥æãåºåãã
- æå®ãã圢åŒã§åºåãã
the_time()
- æçš¿æéãåºåããã
- ãèšå®ïŒäžè¬ãã®æå»ãã©ãŒãããã§åºåãã
the_category('åºåãæå')
- ã«ããŽãªãåºåããã
- åºåãæåãçç¥ãããš
<ul>
,<li>
ãšããŠåºåãããã
# ååŸèšäºãžã®ãªã³ã¯ã衚瀺
previous_post_link(
'<< %link >>',
'[[ %title ]]',
)
// => << [[ title ]] >> ãã ããªã³ã¯ã¯[[ title ]]ã®éšåã®ã¿æå¹
previous_post_link()
ãšnext_post_link()
ã䜿ãã
第äžåŒæ°ã¯ãªã³ã¯ã®åšãã®åºåèŠçŽ ã第äºåŒæ°ã¯ã¿ã€ãã«ã®åšãã®åºåèŠçŽ ãæå®ã§ããã
# ãããããŒãžã®äœæ
# åå¥ããŒãžã®å Žåã®ã¿è¡ãåŠç
<?php if(is_single()): ?>
// çŸåšã®ããŒãžããåäžã®æçš¿ã衚瀺ããããŒãžã®å Žåã®åŠç
<?php else: ?>
// çŸåšã®ããŒãžããè€æ°ã®æçš¿ã衚瀺ããããŒãžã®å Žåã®åŠç
<?php endif; ?>
# èšäºãžã®ãªã³ã¯ã貌ã
ã«ãŒãã®äžã§ã®ã¿æå¹
<a href="<?php the_permalink(); ?>"></a>
# 1 ããŒãžã«æçš¿ãåãŸããã®å€å®
1 ããŒãžãããã®æçš¿ã®è¡šç€ºäžéæ°ã¯ç®¡çç»é¢ããèšå®ã§ããã
ç·ããŒãžæ°ãmax_num_pages
ã§ååŸã§ããã®ã§ãããã 1 以äžãã©ããã§å€å®ããã
<?php if($wp_query->max_num_pages > 1): ?>
// ããŒãžããŒã·ã§ã³ã®è¡šç€ºãªã©
<?php endif; ?>
# ããŒãžããŒã·ã§ã³ã衚瀺ãã
post
ã§ã¯ãªããposts
ãªã®ã§æ³šæãåŒæ°ãçç¥ãããšãã次ã®ããŒãžããªã©ã®æèšãèªåã§èšå®ãããã
<?php next_posts_link('å€ãèšäº') ?>
<?php previous_posts_link('æ°ããèšäº') ?>
# ããŒã ããŒãžã®èšå®
ããŒã ããŒãžã®èšå®ã¯ããèšå®ïŒè¡šç€ºèšå®ïŒããŒã ããŒãžã®è¡šç€ºãããè¡ãã2 ã€ã®ã¢ãŒããæãã
- ã«ãŒããã¹ã«ã¢ã¯ã»ã¹ããéãæçš¿ãäžèŠ§ã§è¡šç€ºããã¢ãŒãïŒãã³ãã¬ãŒãã¯
front-page.php
ïŒ - ã«ãŒããã¹ã«ã¢ã¯ã»ã¹ããéãåºå®ããŒãžã衚瀺ïŒãã³ãã¬ãŒãã¯
front-page.php
ïŒããæçš¿ã®äžèŠ§è¡šç€ºã¯å¥ã®åºå®ããŒãžïŒãã³ãã¬ãŒãã¯home.php
ïŒã§è¡ãã¢ãŒã
# ç¹å®ã®ããŒãžã®å€å®
is_singular()
äžèšã®ããããis_single()
ãæçš¿ã®åå¥ããŒãžis_page()
åºå®ããŒãž
is_home()
ããŒã ããŒãžã®è¡šç€ºèšå®ããåºå®ããŒãžãã«ãªã£ãŠããå Žåã«ããããæçš¿ããŒãžãã§ãããã©ããis_front_page()
ã«ãŒããã¹ã§è¡šç€ºãããããŒãžãã©ããis_archive()
以äžã®ããããis_category()
ã«ããŽãªããŒãžãis_month()
æå¥ããŒãžã
is_single
, is_page
, is_category
ã§ã¯ãåŒæ°ãæå®ããããšã§ãã詳现ãªçµã蟌ã¿ãè¡ããã
// 'èšæäŒæ¥ã®ãç¥ãã'ãšããã¿ã€ãã«ã®ããŒãžã«ã®ã¿
<?php if(is_single('èšæäŒæ¥ã®ãç¥ãã')): ?><?php endif; ?>
// 'Development'ãšããã«ããŽãªã«ã®ã¿
<?php if(is_category('Development')): ?><?php endif; ?>
åå¥ããŒãžã®ãã¡ãç¹å®ã®æ¡ä»¶ã®ãã®ãæãåºãã«ã¯has_***()
ã䜿ãã
has_category()
has_tag()
has_post_thumbnail()
<?php if(is_single() && has_category('Development')): ?>
# ããããšããã¿
<header>
<h1><a href="<?php echo home_url(); ?>"><?php bloginfo('name'); ?></a></h1>
<p><?php bloginfo('description'); ?></p>
</header>
echo home_url();
- ãµã€ãã®ãããããŒãž URL ãåºå
- åŒæ°ã§ãä»å ããããã¹ãæå®ããããšãå¯èœ
# wp_head()
ãšwp_footer()
- WordPress ã®ããŒãã§ã¯ã
wp_head()
ãšwp_footer()
ã¯å¿ ãä»å ãã決ãŸãã«ãªã£ãŠããã wp_head()
ã¯</head>
ã®çŽåã«å ¥ããwp_footer()
ã¯</body>
ã®çŽåã«å ¥ããïŒ</footer>
ã®çŽåã§ã¯ãªãã®ã§æ³šæïŒ
# ç»å
ç»åãã¢ããããŒããããšã3 çš®é¡ã®ç»åïŒãµã ãã€ã«ãäžãµã€ãºã倧ãµã€ãºïŒãäœæãããããã®ãµã€ãºã¯ã管çç»é¢ã§èšå®å¯èœã
ãã£ãã·ã§ã³ã¯äžèšã®ãããªåœ¢ã§ç»åã®äžã«åºåãããã
<p class="wp-caption-text">ãã£ãã·ã§ã³ã§ãã</p>
ç»åã®é 眮äœçœ®ããå³ãå·Šãäžå€®ããªã©ã«ããŠããæšæºã§ã¯èŠãç®ã¯å€ãããªãã
aligncenter
ãalignright
ãalignleft
ãšããã¯ã©ã¹ãèšå®ãããã®ã§ãããã䜿ã£ãŠãæå㧠CSS ãèšå®ããããšã
# Menu
# ãŠã£ãžã§ããã®ç»é²ãšè¡šç€º
ãŠã£ãžã§ããã¯ãµã€ãããŒã«è¡šç€ºãããã®ã§ããŸãã¯ãµã€ãããŒãèšçœ®ããã ãã®åŸã管çç»é¢ããé 眮ãããŠã£ãžã§ãããéžæããã
// function.php
<?php register_sidebar(); ?>
// index.php
<ul><?php dynamic_sidebar(); ?></ul>
è€æ°ã®ãµã€ãããŒãç»é²ããããšãã¯äžèšã®éãã
// function.php
<?php
register_sidebar();
register_sidebar();
?>
// index.php
<ul><?php dynamic_sidebar(1); ?></ul>
<ul><?php dynamic_sidebar(2); ?></ul>
# ã«ããŽãªããŒãžçšãã³ãã¬ãŒã
<?php if(is_category()): ?>
<h1>"<?php single_cat_title(); ?>"ã«é¢ããèšäº</h1>
<?php endif; ?>
single_cat_title();
- ã«ããŽãªåãåºå
# æå¥ããŒãžçšãã³ãã¬ãŒã
<?php if(is_month()): ?>
<h1><?php echo get_the_date('Y幎næ'); ?>ã«æçš¿ããèšäº</h1>
<?php endif; ?>
# RSS
// functions.php
<?php
add_theme_support('automatic-feed-links');
// index.php
<a href="<?php bloginfo('rss2_url'); ?>">RSS Feed</a>
add_theme_support(automatic-feed-links)
- head ã¿ã°å ã« RSS ãžã®ãªã³ã¯ãèªåã§åã蟌ãïŒéåžžã®ãã£ãŒãïŒã³ã¡ã³ããã£ãŒãïŒ
bloginfo('rss2_url')
- RSS ãã¡ã€ã«ãžã®ãªã³ã¯ãåºåãã
# ã³ã¡ã³ã
# ã³ã¡ã³ããã©ãŒã ã®èšçœ®
// comments.php
<?php
comment_form('format=html5');
// index.php
<?php
<?php comments_template(); ?>
comment_form()
- ã³ã¡ã³ããã©ãŒã ãèªã¿èŸŒã
- éåžžã®ãã³ãã¬ãŒãã«ãã¿æã¡ããŠãåäœãããããã®å Žåããã¹ãŠã®ããŒãžã§ã³ã¡ã³ããã©ãŒã ã衚瀺ããŠããŸã£ãŠéœåãæªãã®ã§ã
comments_template()
ãšçµã¿åãããŠäœ¿ããšããã
comments_template()
- ã³ã¡ã³ããã³ãã¬ãŒãïŒ
comments.php
ïŒãèªã¿èŸŒã - åå¥ããŒãžãåºå®ããŒãžã®å Žåã«ã®ã¿äœåãããäžèŠ§ããŒãžã§ã¯äœåããªãã
- ã³ã¡ã³ããã³ãã¬ãŒãïŒ
# ã³ã¡ã³ãã®è¡šç€º
// comments.php
<?php if(have_comments()): ?>
<h3>ã³ã¡ã³ã</h3>
<ul>
<?php wp_list_comments(); ?>
</ul>
<?php endif; ?>
have_comments()
- ããæžã蟌ãŸããã³ã¡ã³ããååšããå Žå
wp_list_comments()
- ã³ã¡ã³ããªã¹ããæåãã
- åŒæ°ã«ãããªãã·ã§ã³ãæå®ã§ãããã¢ãã¿ãŒã®ãµã€ãºãéå±€ã®æ·±ããHTML5 ã§ã®åºåãªã©ã
# ã³ã¡ã³ãæ°ã®è¡šç€º
<a href="<?php comments_link(); ?>">
<?php comments_number('ã³ã¡ã³ããªã','ã³ã¡ã³ã1件','ã³ã¡ã³ã%件') ?>
</a>
comments_number()
- ã³ã¡ã³ãã®çµ±èšæ å ±ãåºåãã
- åŒæ°ã¯é ã«ãã³ã¡ã³ã 0 件ãã³ã¡ã³ã 1 件ãã³ã¡ã³ãè€æ°ä»¶ã®ãšãã®åºåãæå®ã§ãã
comments_link()
- ã³ã¡ã³ããžã®ãªã³ã¯ãçæãããå®éã¯ã«ã¬ã³ã URL ã«
#comments
ãå ããã ãã®ãã®ã
- ã³ã¡ã³ããžã®ãªã³ã¯ãçæãããå®éã¯ã«ã¬ã³ã URL ã«
# ãã©ãã¯ããã¯
Trackback URL: <?php trackback_url() ?>
trackback_url()
- ãã©ãã¯ãã㯠URL ãåºåãã
# åºå®ããŒãžã®äœæ
# ãã³ãã¬ãŒã
åºå®ããŒãžã®ãã³ãã¬ãŒãã¯äžèšã®é ã§éžæãããã
- ã«ã¹ã¿ã ãã³ãã¬ãŒã
page.php
index.php
ã«ã¹ã¿ã ãã³ãã¬ãŒãã¯äžèšã®ããã«ããŠäœæããã管çç»é¢ã§ãåºå®ããŒãžããšã«ãã³ãã¬ãŒããæåéžæã§ããã
// my-template.php
<?php
/*
Template Name: ãã€ãã³ãã¬ãŒã1
*/
?>
# ããŒããã³ãã¬ãŒã
ãã³ãã¬ãŒãã¿ã° | èªã¿èŸŒãŸãããã¡ã€ã« | åè |
---|---|---|
get_header() | header.php | |
get_footer() | footer.php | |
get_sidebar() | sidebar.php | |
comments_template() | comments.php | is_single() ã®å Žåã¯èªã¿èŸŒãŸãªã |
get_serarch_form() | searchform.php | |
get_template_part('*') | *.php | ä»»æã®ãã¡ã€ã«ãèªã¿èŸŒã |
äŸãã°ããããããŒããã³ãã¬ãŒããšããŠåãåºãå Žåãheader.php
ãäœæããããã§ãindex.php
ã§äžèšã®ããã«ããã
// index.phpãªã©
<?php get_header(); ?>
get_template_part()
ã§ã¯ä»»æã®ãã³ãã¬ãŒãããŒããèªã¿èŸŒãããšãã§ããã
get_template_part('my-template/content');
// => my-template/content.php
get_template_part('my-template/content', 'single');
// => my-template/content-single.php
# ããã²ãŒã·ã§ã³ã¡ãã¥ãŒã®äœæ
ãã±ãŒã·ã§ã³ãç»é²ãããåãã±ãŒã·ã§ã³ã«ã¯ 1 ã€ã®ã¡ãã¥ãŒã衚瀺ããããšãã§ããã
// functions.php
register_nav_menu('navigation', 'ããã²ãŒã·ã§ã³');
register_nav_menu('navigation2', 'ããã²ãŒã·ã§ã³2'); // ãã±ãŒã·ã§ã³ã2ã€äœãå Žå
ãã³ãã¬ãŒãã«ããã±ãŒã·ã§ã³ã瀺ããã³ãã¬ãŒãã¿ã°ãèšèŒããã
<?php wp_nav_menu('theme_location=navigation'); ?>
管çç»é¢ã§ã¡ãã¥ãŒãäœæããããã®éãã¡ãã¥ãŒãã©ãã®ãã±ãŒã·ã§ã³ã«è¡šç€ºãããéžæããã
ãªããããã²ãŒã·ã§ã³ã¡ãã¥ãŒã¯ãã®ãŸãŸãŠã£ãžã§ãããšããŠã䜿ãããšãã§ããã
# ãåãåãããã©ãŒã ã®é 眮
Contact Form 7 ãšãããã©ã°ã€ã³ã䜿ãã°ãã·ã§ãŒãã³ãŒãã貌ãä»ããã ã㧠OKããã ãã¡ãŒã«ãµãŒãã®èšå®ãå¿ èŠã
# ãã®ä»
# ãã³ãã¬ãŒããã©ã«ãã®ã¢ã»ããã«ã¢ã¯ã»ã¹ãã
get_template_directory_uri()
- ããŒããã©ã«ãã® URI ãååŸãã
# ã«ã¹ã¿ã ããããŒ
ã«ã¹ã¿ã ããããšã¯ãè€æ°ã®ãããç»åã管çã§ããæ©èœã§ããã
æ©èœãæå¹ã«ãã
// functions.php
add_theme_support('custom-header');
ãããç»åãããã°ãåºåãã
<?php if(get_header_image()): ?>
<img src="<?php header_image(); ?>" />
<?php endif; ?>
ç»åã®ã¢ããããŒããèšå®ã¯ç®¡çç»é¢ã®ãå€èŠ³ïŒããããããè¡ãã
add_theme_support('custom-header')
- ã«ã¹ã¿ã ããããæå¹ã«ãã
get_header_image()
- ç»åã® URI ãè¿ã
header_image()
- ç»åã® URI ã echo åºåãã
# ã«ã¹ã¿ã èæ¯
èæ¯ç»åãæå®ã§ããæ©èœã
// functions.php
add_theme_support('custom-background');
ç»åã®ã¢ããããŒããèšå®ã¯ç®¡çç»é¢ã®ãå€èŠ³ïŒèæ¯ãããè¡ãã
# èŠçŽã®è¡šç€º
// functions.php
add_filter('excerpt_mblength', function($length) {
return 70;
});
add_filter('excerpt_more', function($more){
return '...';
});
/// index.php
<?php if(is_single()): ?>
<?php the_content() ?>
<?php else: ?>
<?php the_excerpt() ?>
<?php endif; ?>
# ã¢ã€ãã£ããç»å
// functions.php
add_theme_support('post-thumbnails');
/// index.php
<?php if(has_post_thumbnail()): ?>
<?php the_post_thumbnail('medium'); ?>
<?php endif; ?>
ãµã€ãºã¯ä»¥äžã® 4 çš®é¡
thumbnail
medium
large
full
# Untrusted Data ã®ãµãã¿ã€ãº
åçãªããŒã¿ã¯ãäžèšã®ãã¡ã³ã¯ã·ã§ã³ã§é©å®ãµãã¿ã€ãºããããšã
- HTML å±æ§ =>
esc_attr()
- HTML =>
esc_html()
- URL =>
esc_url()
# Theme Customization API
ãµã€ã管çè ã«ããŒããã«ã¹ã¿ãã€ãºãããããã® APIãTODO:詳现ã¯èŠèª¿æ»ã https://codex.wordpress.org/Theme_Customization_API
# ãã³ãã¬ãŒãå ã§ãã¡ã€ã«ãåç §ãã
ãã³ãã¬ãŒãå ãããä»ã®ãã¡ã€ã«ã® URI(url) ã絶察ãã¹ãååŸããéã¯ãããŒãã³ãŒãã£ã³ã°ã¯ããã«ãäžèšã®é¢æ°ã䜿ãããšã
echo get_theme_file_uri( 'images/logo.png' ); // => 'http://...'
echo get_theme_file_path( 'images/logo.png' ); // => '/opt/bitnami/...'
echo get_parent_theme_file_uri( 'images/logo.png' );
echo get_parent_theme_file_path( 'images/logo.png' );
ãªããcss ãã¡ã€ã«å ã¯çžå¯Ÿåç §ã«ãªãã
h1 {
background-image: url(images/my-background.jpg);
}
# Plugin API Hooks
ããŒããäœæãããšãã¯ãä»ã®ãã©ã°ã€ã³ãæ©èœãã€ã³ãžã§ã¯ãã§ããããã«ãæäœéã§ãäžèšã® Plugin API ãããŒãå ã«å«ããŠããå¿ èŠãããã
wp_enqueue_scripts
- functions.php ã®äžã«èšèŒããããã©ã°ã€ã³ããå€éšã® JS/CSS ãã¡ã€ã«ãèªã¿èŸŒãæã«äœ¿ãã
wp_head()
- header.php ã®äžã«èšèŒããããã©ã°ã€ã³ã JS ã³ãŒããã€ã³ãžã§ã¯ããããšãçã«äœ¿ãã
wp_footer()
- footer.php ã®çµäº BODY ã¿ã°ã®çŽåã«èšèŒããããã©ã°ã€ã³ããäžçªæåŸã§è¯ãåŠçãäŸãã° Analytics ã³ãŒãã®ã€ã³ãžã§ã¯ããããéãªã©ã«äœ¿ãã
wp_meta()
- sidebar.php ã«èšèŒããããã©ã°ã€ã³ãåºåãã¿ã°ã¯ã©ãŠããæ¿å ¥ããæã«äœ¿ãã
comment_form()
- comments.php ã®æåŸã®
</div>
ã®çŽåã«èšèŒããããã©ã°ã€ã³ãã³ã¡ã³ããã¬ãã¥ãŒãæ¿å ¥ããéãªã©ã«äœ¿ãã
- comments.php ã®æåŸã®
# ã¯ãšãªããŒã¹ã®ãã³ãã¬ãŒã
ã¯ãšãªã¿ã€ãã«ãã£ãŠãã³ãã¬ãŒããåãæ¿ããã«ã¯ããã³ãã¬ãŒãããšã©ã«ããŒã䜿ãæ¹æ³ãšãThe Loop ã®äžã§ Conditional Tags ã䜿ãæ¹æ³ã® 2 ã€ãããã
# ãã³ãã¬ãŒãããšã©ã«ããŒã䜿ã
åè¿°ã®ãã³ãã¬ãŒãã®åªå
é äœãå©çšããŠããã³ãã¬ãŒããåãæ¿ããæ¹æ³ãäŸãã°ãcategory-6.php
ãšãããã¡ã€ã«ãæãã°ãID ã 6 ã§ããã«ããŽãªã®ãã³ãã¬ãŒãã«ã¯ãindex.php
ã®ä»£ããã«ãã®ãã¡ã€ã«ã䜿ãããã
# Conditional Tags ã䜿ã
ãã³ãã¬ãŒãå ã§ãæ¡ä»¶åå²ã«ãã衚瀺ãããã³ãã¬ãŒããåãæ¿ããæ¹æ³ã
Conditional Tags ã®äžèŠ§ (opens new window)
<?php
if ( is_category( '9' ) ) {
get_template_part( 'single2' ); // looking for posts in category with ID of '9'
} else {
get_template_part( 'single1' ); // put this on every other category post
}
?>
# ã«ã¹ã¿ã ãã³ãã¬ãŒãã®å®çŸ©
Plugins ãfunctions.php
ãçšããŠãtemplate_include
ãšãã action hook ã§è¡šç€ºãããã³ãã¬ãŒããåãæ¿ããæ¹æ³ã
function custom_template_include($template)
{
if (is_single() && in_category('news')) {
$new_template = locate_template(array( 'single-news.php' ));
if ('' != $new_template) {
return $new_template ;
}
}
return $template;
}
add_filter('template_include', 'custom_template_include', 99);
# i18n
TODO:èŠèª¿æ» https://codex.wordpress.org/I18n_for_WordPress_Developers