こんにちは。
記事中に数式を表示したいけれど、プラグインは読み込み速度低下の原因となるので使いたくないと思っている方は多いのではないでしょうか。
また、すべての記事に対して数式表示設定をしてしまうと、数式がない記事まで数式表示設定を読み込んでしまうので、これは避けたいところです。
これらを解決するため、プラグインなしで記事ごとに数式の表示設定を行う方法を紹介します。
利用する機能
カスタムフィールド
カスタムフィールドとは、記事ごとにキーと値の組み合わせを複数登録できる機能です。
値の部分にはコードを保存することもできるので、ここに必要なコードを登録することで、記事ごとに数式読み込み用コードを読み込ませることができます。
カスタムフィールドの設定は記事編集画面からできますが、デフォルトでは表示されていないため、最初は表示設定をする必要があります。
記事編集画面右上のオプション > 設定 > 追加パネル > カスタムフィールド
のチェックボタンにチェックを入れます。
これで、記事編集画面からカスタムフィールドを編集できるようになります。
wp_head() と wp_footer()
wp_head() は html の head 終了直前に読み込まれる関数で、wp_footer() は body 終了直前に読み込まれる関数です。
これらの関数にカスタムフィールドの操作を追加することで、好きなタイミングでコードを読み込ませることができます。
手順
まず、functions.php に以下のコードを追記します。
// head終了直前でhead_loadカスタムフィールド読み込み
function head_original_load(){
if(is_single() || is_page()){
if($head_original_code = get_post_meta(get_the_ID(), 'head_load', false)){
foreach($head_original_code as $head_code){
echo $head_code . "\n";
}
}
}
}
add_action('wp_head', 'head_original_load');
// body終了直前でbody_loadカスタムフィールド読み込み
function body_original_load(){
if(is_single() || is_page()){
if($body_original_code = get_post_meta(get_the_ID(), 'body_load', false)){
foreach($body_original_code as $body_code){
echo $body_code . "\n";
}
}
}
}
add_action('wp_footer', 'body_original_load');
次に、数式を含む記事にカスタムフィールドを追加します。
head で読み込ませたい場合はキーの値を head_load にし、body で読み込ませたい場合はキーの値を body_load にします。
因みに、head 部分は画面のレンダリングが開始する前にすべて読み込まれ、body 部分はレンダリング開始後に非同期的に読み込まれます。
そのため、数式のレンダリングが完全にできてから表示したい場合は head で読み込み、表示速度を重視する場合は body で読み込みます。
カスタムフィールドの値の部分には以下のコードを入れてください。
<script>
window.MathJax = {
tex: {
inlineMath: [['', ''], ['\\(', '\\)']],
tags: 'ams'}
};
</script>
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
1つ目の script では MathJax の設定をしています。
このコードでは Latex と同様に $ で囲んだ部分が数式とみなされる等の設定をしています。
2つ目の script は IE でも表示させるためのコードです。
IEのために読み込み速度を落とすのが嫌な場合は、このコードは消去しても構いません。
3つ目の script では MathJax を読み込んでいます。
最新のバージョンについては以下のサイトで確認してください。
入力し終えたら、カスタムフィールドを追加します。
以上で数式が表示されるようになります。
最後に
プラグインなしで記事ごとに数式表示を設定する方法を紹介しました。
何かあればコメントにてお知らせください。
コメント
Hi my loved one! I wish to say that this post is awesome, great written and come with almost all important infos. Jesse Peasant
Astonishingly user pleasant website. Enormous information readily available on few clicks on. Raphael Trometter
Certain breed groups are more likely to get together with cats than others. Isreal Drisko
Thannks too my father who informed mme oon the ttopic off thjs webpage, tthis web site
iis actually awesome.
There is definately a lot to know about this subject. I really like all the points you made. Alan Wampole
Post writing is also a fun, if you be familiar with then you can write or else it is difficult to write. Rupert Jockers
I go to see everyday some web sites and information sites to read articles, however this web site provides feature based writing. Randal Cinar
Looking forward to reading more. Great post. Really looking forward to read more. Cool. Cesar Dorso
Whats up very cool website!! Guyy .. Beautifful ..
Amaziing .. I’ll bookmark your web ite andd tae the fdeds additionally?
I am hzppy tto seek outt soo many helful info right herte withi thhe put up, we need develop more techniqujes inn
this regard, thasnks for sharing. . . . . .
There is visibly a lot to identify about this. I believe you made various nice points in features also. Kendrick Leebrick
There’s definately a lott to findd out abvout this subject.
I really like aall the points yyou have made.
Hello. This post was really remarkable, particularly because I was investigating for thoughts on this matter last Friday. Lemuel Dolman
Remarkable! Its really remarkable article, I have got much clear idea regarding from this paragraph. Moshe Kercheval
I think his is among the most important information for me. Xavier Bischoff
Only wanna remark on few general things, The website layout is perfect, the written content is rattling good : D. Clay Winingear
I am truly pleased to glance at this weblog posts which carries lots of valuable information, thanks for providing these kinds of information. Ollie Metzner
This piece of writing will help the internet people for creating new website or even a weblog from start to end. Scottie Watral
Wonderfful eat ! I wish too apprentice ven ass you amend your web site, how can i subsscribe ffor a weblog wweb site?
Thee axcount aideed mme a appropriate deal. I were tuny biit familiar of ths your broadcast provgided bright transpaent
concept
This is one awesome blog. Really thank you! Really Cool. Truman Calzada
Pretty! Thiis wwas ann extremely wonderful article.
Thanbk youu forr providing this information.
Definitely believe thawt whichh yoou stated.
Youur favoriite reqson appearted too be oon the web thee simplest thing too bee aware of.
I ssay tto you, I definitely get annoyed while people think anout worries thawt they plainly
do nnot kbow about. Youu managed to hit tthe nail uplon the top aand
defined ouut the whole thing without having side-effects
, people can take a signal. Will progably be back tto gett more.
Thanks
Here is an excellent Blog You might Discover Exciting that we encourage you to visit. Tuan Cruzan
Hi there, this weekend is pleasant in favor of me, because this occasion i am reading this impressive informative paragraph here at my house. Levi Ankrom
Hi there, its pleasant post on the topic of mediia print,
wwe all bee familiar with mefia iss a enormous source oof facts.
What are some excellent as well as prominent websites for blogs???. Kip Shouldice
My beother rexommended I ight like this web site.
He used to be totally right. This post actuallly made my day.
You cann’t consider just how a lot time I hadd spentt for this info!
Thank you!
Here is an excellent Weblog You might Locate Intriguing that we encourage you to visit. Prince Guidera
Some really superb info, Gladiolus I discovered this. Zackary Overbeek
You have observed very interesting points! ps decent web site. Spencer Vanderstappen
One of our visitors recently recommended the following website. Man Riemer
Helpful info. Lucky me I discovered your website accidentally, and I am surprised why this twist of fate did not took place earlier! I bookmarked it. Gino Franzeo
Pretty! This has been an extremely wonderful article. Many thanks for supplying this info. Israel Galati
Enjoyed looking through this, very good stuff, thanks . Ambrose Hilz
Wonderful, what a web site it is! This web site provides valuable data to us, keep it up. Everett Perley
Doees ylur site have a contacct page? I’m having a tlugh time
locating it but, I’d like too shoot youu aan e-mail.
I’ve gott some ideas for your blog yyou might be interested
inn hearing. Eitther way, great site and I look forward
too seing it develop over time.
Awesome! Its truly awesome article, I have got much clear idea on the topic of from this paragraph. Stevie Kain
I enjoiy reading a pot that will makke people think.
Also, many thanks for permitting mee to comment!
Veryy shortly thi web sife woll be famous anid alll
blogginng visitors, ddue too it’s fastidiouus articles
I enjoyed reading this. Thanks for writing this. Hit me up! After reading this I am scared I will never be the same person. Jermaine Mansbridge
Hello. This post was really motivating, particularly because I was browsing for thoughts on this topic last Saturday. Elisha Reppert
Cann yyou tell us more about this? I’d care to find out some additional information.
I for all tjme ejailed this websitee post page to all myy
associates, because iff llike to read it next my links wikl
too.
Hey just wanyed to give youu a quicxk heads up.
The text in yor ppost seem tto bee running off the screen in Internet
explorer. I’m not sure iif this is a format issue or something tto do with browser compatibility but I
figured I’d pos tto leet you know. Thhe laqyout look great though!
Hope yoou get tthe issue resolvrd soon. Maany thanks
Download a VPN that has everything you need like fast speed and a vast number of locations and servers. Brain Flank
Riht away I aam going away too do my breakfast, larer thasn having my breakfast coming over again to redad other news.
Hi, this weekend is pleasant in support of me, because this moment i am reading this fantastic educational article here at my home. Danilo Degraff
Hello! I know this iss kind of off topic but I was wondering iif you knew where I couldd gett a captcna plugin ffor my commment form?
I’m using the same blo platform ass yours and I’m hawving
problems finding one? Thanks a lot!
Everything is very open with a clear explanation of the challenges. Theodore Debenedetti
Murabitur sit amet mauris. Morbi in dui quis est pulvinar ullamcorper. Nulla facilisi. Integer lacinia sollicitudin massa. Cras metus. Donovan Knoeppel
Everything iis very open with a vey clkear clrification off tthe challenges.
It was really informative. Your websote is veryy useful.
Manny thahks foor sharing!
Only wanna remark on few general things, The website layout is perfect, the written content is rattling good : D. Josiah Clagg
Software as a Website. Yes, it on his own PSN account. Darren Carza
Software as a Website. Yes, it on his own PSN account. Darren Carza
Way cool! Some extremely valid points! I appreciate you penning this article and also the rest of the website is really good. Randall Nading
If some one needs expert view regarding running a blog after that i propose him/her to visit this website, Keep up the fastidious job. Vito Novak
Thanks a lot for the blog. Much thanks again. Great. Russ Guilmain
Hi there very cool blog!! Man .. Excellent .. Wonderful .. Keven Correa
Hi there! Thiss is myy first visxit to your blog! We are a grou of volunteers andd starting a neew initiative in a commhnity inn the
samne niche. Yourr bloog provided us useful information to work on. Yoou havce ddone a marvellous
job!
Amazing! Its genuinely amazing paragraph, I have got much clear idea regarding from this piece of writing. Hugh Magouirk
Very neat blog article. Really looking forward to read more. Will Crellin
Good article! We are linking to this particularly great post on our website. Keep up the great writing. Sang Badgley
Appreciating the commitment yyou put into yyour website and detaied iformation yoou offer.
It’s good to come across a blo every once in a while thuat isn’t the same outdated rehhashed material.
Gret read! I’ve bookmarked your site and I’m adding your RSS feeds to my Googpe account.
Youu really mwke iit appear really easy allng wioth your
presentation but I to fjnd this matter to be really
something that I believe I’d by no meas understand.
It kind off feels tooo compllex annd very extensive for me.
I’m having a lookk forward oon yoour subsequent publish, I’ll attempt to get the ddangle off it!
Do you mind if I quote a few of your articles as long as I provide credit and sources back to your blog? My blog site is in the very same niche as yours and my users would genuinely benefit from a lot of the information you provide here. Please let me know if this okay with you. Thank you!
An inpressive share! I have jjust orwarded this onjto a colleague who
was doing a little omework oon this. Annd hhe actuyally bought me
lunch because I stumble upon itt for him… lol. So let mme reword this….
Thank YOU for thhe meal!! Butt yeah, thanks for spending slme tme to
disxuss thjs mastter here oon yoour web
page.
Excellent post. I am facingg some oof these isxues
as well..
I got what you intend, regards for putting up.Woh I am delighted to find this website through google.
Ahaa, its ggood conversation concerning thiss aticle at this place att thhis
web site, I have read all that, so aat this time mme also commentting
at this place.
If you are going for finest contents like me, simpky visit thi websxite everry
day ffor the reason that itt gives quality contents, thanks
Hey man, I’m looking forward to your new content with great content.
Thznk yoou a bunch for saring this with all folks you actually refognise what you’re
speaking about! Bookmarked. Kindly also discuss with myy ite =).
We could havve a hyplerlink altwrnate arrazngement ammong us
I amm ure thbis piece of writing has toiched all thee internet
viewers, its really really good piece of writing on buildding up new weblog.
Wow, marvelous wweblog format! How lokng
have you evewr been runnhing a blog for? youu mazke blogging look easy.
The overall glance oof your website is great, let alone the
conent material!
Sweet blog! I fokund it while surfing around oon Yaahoo News.
Do you havee any suggestions onn how to get listed iin Yahoo
News?I’ve besn tryinjg foor a while butt I nevcer seem too get there!
Thank you
Keep onn writing, grest job!
Wow, this paragraph iss fastidious, my younger sister iss analyzing these things, therefopre I am gong tto inform her.
Hello There. I found your weblog the usee of msn. Thaat iss
ann extremely neatlyy wrritten article. I’ll be
sure too bookmark iit and retturn to read
more of your helpful information. Thanks for the post.
I will certainly comeback.
Hey I know this is offf ttopic buut I was wondering if you knsw of any widgets I coulpd aadd to mmy blog that automaticaally tweet my newest twitter updates.
I’ve ben looking for a plug-in like this for quite sme time and was hoping mybe you wokuld havve some experience with somethinng like this.
Pleaqse let me know iif you rrun into anything.
I truly enjooy readiing your blog and I llok forward
tto yoour new updates.
I wass very happy tto discover thi website. I need to to thank you forr your timee ffor this fantastic read!!
I definitely lovedd every lkttle biit oof itt andd i alsso have you saved as a favorite to look aat
nnew informatgion in yolur web site.
My spouse and I stumbled ovr hewre from a different webb address annd thought I migt ass ell chheck things out.
I like what I see soo nnow i’m following you. Lookk forward to
looking att your web paage forr a second time.
Heeya i aam for thee first time here. I came across this board and I fibd It ruly useful
& it helped mee out much. I hope to give something bck
and hlp others likee you helped me.
Thankfulness to myy father who told me about tjis web site,this webpage iss in fact amazing.
I was urious if you ever considered changing thee page layout
oof yiur site? Its very well written; I love what youve goot
tto say. Buut maybe you could a little more in tthe way of content soo people could coonnect with it better.
Youve goot aan awful lot off text for onnly having 1 oor
ttwo pictures. Mahbe you could space it ouut better?
Woah! I’m really digging thee template/theme of this site.
It’s simple, yet effective. A lott of imes it’s vdry difficult tto gett tyat “perfect balance” between usability
and vsual appeal. Imusat say yoou have done a excellent
job with this. Additionally, the blog loads very fast ffor me oon Opera.
Excepgional Blog!
Wayy cool! Soome etremely valid points! I appreciawte you pennig this plst plus tthe est of the ite iis alsso very good.
Whhat a stuff of un-ambiguity and preserveneses oof precious experiience concernhing unexpected emotions.
I kniw this iif offf topic but I’m loojing inyo strting my own blog and was
wondrring wjat all iss reqyired tto get sett up?
I’m assuming having a blog like yours wouod cost a pretty penny?
I’m nnot vry internet savvy so I’m not 100% positive. Any recommendations orr advice would be greatly appreciated.
Cheers
Thanks for sharing yoiur thoughts abou ブログ運営.
Regards
It’s in realitry a ggreat annd useful piece of information. I am satisfied that you simply shared this helpful ibformation wiith us.
Please stray uus up tto date like this. Thank youu for sharing.
Hiya! Quick question that’s totally off topic. Do youu know how tto make your
sitee mobile friendly? My webite lloks weirfd whdn viewing from my apple iphone.
I’m trying too fond a theme or plugin that might be ale to correct this
issue. If you have any suggestions, plrase share.
Thank you!
Thank you for sharing superb informations. Your web-site is so cool. I’m impressed by the details that you have on this site. It reveals how nicely you understand this subject. Bookmarked this website page, will come back for more articles. You, my pal, ROCK! I found just the info I already searched everywhere and just could not come across. What an ideal website.
I didn’t think it would be such a good article, thanks again.
I’m impressed, I have to say. Actually rarely do I encounter a weblog that’s both educative and entertaining, and let me inform you, you may have hit the nail on the head. Your thought is excellent; the issue is one thing that not enough people are talking intelligently about. I’m very completely happy that I stumbled throughout this in my search for one thing relating to this.
You actually make it seem so easy along with your presentation but I in finding this topic to be actually something which I think I’d by no means understand. It seems too complex and extremely broad for me. I’m having a look forward in your next submit, I¦ll attempt to get the cling of it!
That is the appropriate weblog for anyone who needs to find out about this topic. You understand a lot its nearly hard to argue with you (not that I truly would want…HaHa). You positively put a new spin on a topic thats been written about for years. Great stuff, just great!
It iss not my first time too pay a quuick visit this website, i am browsing his weeb page daillyy annd obtain fastidious dqta from here daily.
F*ckin’ awesome things here. I’m very glad to see your post. Thanks a lot and i’m looking forward to contact you. Will you please drop me a mail?
I have learn some just right stuff here. Certainly value bookmarking for revisiting. I wonder how a lot attempt you set to make this type of fantastic informative site.
Wow that was strange. I just wrote an extremely long comment but after I clicked submit my comment didn’t show up. Grrrr… well I’m not writing all that over again. Regardless, just wanted to say great blog!
I have learn a few excellent stuff here. Certainly value bookmarking for revisiting. I surprise how a lot effort you set to make this type of excellent informative website.