Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ノンプログラマーのためのWordPressループことはじめ

 ノンプログラマーのためのWordPressループことはじめ

第5回WordBench千葉で発表したスライドです。 #wbchiba
http://andask.net

Bce9bd5aeebac5212a4548cd94774153?s=128

EisukeAkimoto

August 25, 2013
Tweet

Transcript

  1. 5th WordBench Chiba ϊϯϓϩάϥϚʔͷͨΊͷ 8PSE1SFTT ϧʔϓ͜ͱ͸͡Ί

  2. 5th WordBench Chiba

  3. ࣗݾ঺հ ϑϦʔϥϯεͷ΢Σϒ԰͞Μ ࠷ۙ͸ 8PSE1SFTT Λ࢖༻͢ΔҊ͕݅ओ ɾίʔϙϨʔτ ɾΞύϨϧ ɾϏϡʔςΟʔɹFUD 5th WordBench

    Chiba
  4. 5th WordBench Chiba ϒϩά ͋Γ·͢ an d ask.net

  5. ຊ೔ͷ͓͠ͳ͕͖ 㾎8PSE1SFTT ʹ͔ܽͤͳ͍ϧʔϓͷॻ͖ํ ᶃ8PSE1SFTT ͷॲཧͷྲྀΕ ᶄRVFSZ@QPTUTͱHFU@QPTUT ᶅQSF@HFU@QPTUT ͬͯͳΜͩʁ 5th WordBench

    Chiba
  6. 5th WordBench Chiba ᶃ8PSE1SFTTͷॲཧͷྲྀΕ

  7. 5th WordBench Chiba ᶃ8PSE1SFTTͷॲཧͷྲྀΕ PC Web Database

  8. 5th WordBench Chiba ᶃ8PSE1SFTTͷॲཧͷྲྀΕ IUUQFYBNQMFDPN Q ͜ͷ 63- ΁ΞΫηεͨ͠৔߹

  9. 5th WordBench Chiba ᶃ8PSE1SFTTͷॲཧͷྲྀΕ 63- Λ΋ͱʹσʔλϕʔε͔Β ৚݅ʹ߹͏౤ߘσʔλͷऔಘ

  10. 5th WordBench Chiba ᶃ8PSE1SFTTͷॲཧͷྲྀΕ ͜Ε͕8PSE1SFTT ʂ ʂ ͍ͨͬͯγϯϓϧ

  11. 5th WordBench Chiba ᶃ8PSE1SFTTͷॲཧͷྲྀΕ ͭ·Γ

  12. 5th WordBench Chiba ᶃ8PSE1SFTTͷॲཧͷྲྀΕ 63- Λ΋ͱʹσʔλϕʔε͔Β ৚݅ʹ߹͏౤ߘσʔλͷऔಘ

  13. 5th WordBench Chiba ᶃ8PSE1SFTTͷॲཧͷྲྀΕ

  14. 5th WordBench Chiba ᶃ8PSE1SFTTͷॲཧͷྲྀΕ Web Web PC Theme Database

  15. 5th WordBench Chiba ᶃ8PSE1SFTTͷॲཧͷྲྀΕ 63- Λ΋ͱʹσʔλϕʔε͔Β ৚݅ʹ߹͏౤ߘσʔλͷऔಘ

  16. 5th WordBench Chiba ᶃ8PSE1SFTTͷॲཧͷྲྀΕ ʮIBWF@QPTUT ʯʁ ςʔϚͰσʔλΛ੔ܗ

  17. 5th WordBench Chiba ᶃ8PSE1SFTTͷॲཧͷྲྀΕ 1$ʮදࣔ͞ΕͨΑʂʂʯ Web

  18. 5th WordBench Chiba ᶄRVFSZ@QPTUT ͱ HFU@QPTUT

  19. 5th WordBench Chiba ᶄRVFSZ@QPTUT ͱ HFU@QPTUT RVFSZ@QPTUT ͱ͸ query_posts() ͸

    8PSE1SFTT ͕౤ߘΛදࣔ͢Δͷʹ࢖༻͍ͯ͠ΔϝΠϯΫΤϦʔ Λஔ͖׵͑ΔखஈͰ͢ɻ͜Ε͸ϝΠϯΫΤϦʔΛҰ୴ԣʹஔ͍͓͖ͯɺ৽͍͠ΫΤϦʔ Ͱ্ॻ͖͠·͢ɻquery_posts Λݺͼग़ͨ͋͠ͱ͸ɺΫΤϦʔͷվมΛ໭ͨ͢Ίʹɺ wp_reset_query() Λݺͼग़͠ɺΦϦδφϧͷϝΠϯΫΤϦʔΛ෮׆ͤ͞Δඞཁ͕ ͋Γ·͢ɻ  தུ query_posts Ͱ͸ͳ͘ pre_get_posts ϑΟϧλʔΛ༻͍ɺis_main_query Ͱ νΣοΫͯ͠ϝΠϯΫΤϦʔΛมߋ͢Δ͜ͱ͕ڧ͘ਪ঑͞Ε͍ͯ·͢ɻ 8PSE1SFTT$PEFYΑΓҾ༻
  20. 5th WordBench Chiba ᶄRVFSZ@QPTUT ͱ HFU@QPTUT RVFSZ@QPTUT ͱ͸ query_posts() ͸

    8PSE1SFTT ͕౤ߘΛදࣔ͢Δͷʹ࢖༻͍ͯ͠Δ ʠ౤ߘσʔλΛ ݺͼग़͢ઃఆ܈ʡ Λஔ͖׵͑ΔखஈͰ͢ɻ͜Ε͸ ʠσϑΥϧτͰηοτ͞ΕΔઃఆ܈ʡ Λ Ұ୴ԣʹஔ͍͓͖ͯɺ৽͍͠ ʠઃఆʡ Ͱ্ॻ͖͠·͢ɻquery_posts Λݺͼग़ͨ͋͠ ͱ͸ɺ ʠมߋ͞Εͨઃఆʡ Λ໭ͨ͢Ίʹɺwp_reset_query() Λݺͼग़͠ɺ ʠσϑΥϧ τͷઃఆ܈ʡ Λ෮׆ͤ͞Δඞཁ͕͋Γ·͢ɻ  தུ query_posts Ͱ͸ͳ͘ pre_get_posts ϑΟϧλʔΛ༻͍ɺis_main_query Ͱ νΣοΫͯ͠ ʠσϑΥϧτͰηοτ͞ΕΔઃఆ܈ʡ Λมߋ͢Δ͜ͱ͕ڧ͘ਪ঑͞Εͯ ͍·͢ɻ
  21. 5th WordBench Chiba ᶄRVFSZ@QPTUT ͱ HFU@QPTUT ͭ·Γ ʢͲ͏͍͏͜ͱʜʁʣ

  22. 5th WordBench Chiba ᶄRVFSZ@QPTUT ͱ HFU@QPTUT ·ͣ͸ɺ63- Λ΋ͱʹ ʠσϑΥϧτͰηοτ͞ΕΔઃఆ܈ʡ Λݺͼग़ͯ͠σʔλϕʔε͔Β৚݅ʹ߹͏౤ߘσʔλͷऔಘʂ

  23. 5th WordBench Chiba ᶄRVFSZ@QPTUT ͱ HFU@QPTUT query_posts() Ͱʠݺͼग़͞ΕͨઃఆΛ্ॻ͖ͯ͠ʡ σʔλϕʔε͔Β৚݅ʹ߹͏౤ߘσʔλͷ ʠ࠶ʡ

    औಘʂ มߋͨ͠ޙ͸ wp_reset_query() ͰઃఆΛݩʹ໭͢ɻͭ·Γͦ͏͍͏͜ͱʂ query_posts()
  24. 5th WordBench Chiba ᶄRVFSZ@QPTUT ͱ HFU@QPTUT HFU@QPTUTͱ͸ ςϯϓϨʔτλά get_posts() ͸ɺෳ਺ͷϧʔϓΛ࡞੒͢ΔͨΊͷγϯϓϧͳ

    λάͰ͢ɻ͜ΕΛ࢖ͬͯɺ࠷৽ͷ౤ߘ΍৚݅ʹҰக͢Δ౤ߘΛऔಘ͢Δ͜ͱ͕Ͱ͖ ·͢ɻ 8PSE1SFTT$PEFYΑΓҾ༻
  25. 5th WordBench Chiba ᶄRVFSZ@QPTUT ͱ HFU@QPTUT HFU@QPTUTͱ͸ ςϯϓϨʔτλά get_posts() ͸ɺෳ਺ͷϧʔϓΛ࡞੒͢ΔͨΊͷγϯϓϧͳ

    λάͰ͢ɻ͜ΕΛ࢖ͬͯɺ࠷৽ͷ౤ߘ΍৚݅ʹҰக͢Δ౤ߘΛऔಘ͢Δ͜ͱ͕Ͱ͖ ·͢ɻ ͭ·Γɺ ʠσϑΥϧτͰηοτ͞ΕΔઃఆ܈ʡ ɹʹӨڹͤͣϧʔϓΛ࡞੒͢Δ͜ͱ͕ Ͱ͖·͢ɻ
  26. 5th WordBench Chiba ᶄRVFSZ@QPTUT ͱ HFU@QPTUT

  27. 5th WordBench Chiba ᶄRVFSZ@QPTUT ͱ HFU@QPTUT

  28. 5th WordBench Chiba ᶄRVFSZ@QPTUT ͱ HFU@QPTUT αϒϧʔϓ get_posts() ϝΠϯϧʔϓ

  29. 5th WordBench Chiba ᶄRVFSZ@QPTUT ͱ HFU@QPTUT HFU@QPTUT͕దͯ͠Δ৔໘ ϝΠϯͷϧʔϓͷଞʹɺ৽ͨʹهࣄͷҰཡΛද͍ࣔͤͨ͞ͱ͖ɻ

  30. 5th WordBench Chiba ᶄRVFSZ@QPTUT ͱ HFU@QPTUT HFU@QPTUT͕దͯ͠Δ৔໘ ϝΠϯͷϧʔϓͷଞʹɺ৽ͨʹهࣄͷҰཡΛද͍ࣔͤͨ͞ͱ͖ɻ ྫ͑͹ ʮαΠυόʔʹಛఆͷΧςΰϦʔʹଐ͢ΔهࣄΛ̑݅ද͓͖͍ࣔͤͯͨ͞ʯ

    ʮαΠυόʔʹਓؾͷ͋ΔهࣄΛ̑݅ද͓͖͍ࣔͤͯͨ͞ʯ ʮಛఆͷ౤ߘΛআ͍ͯɺ͓͢͢ΊͷهࣄΛ̍̌݅ද͍ࣔͨ͠ʯ ͳͲͳͲʜ
  31. 5th WordBench Chiba ᶄRVFSZ@QPTUT ͱ HFU@QPTUT HFU@QPTUT͕దͯ͠Δ৔໘ ϝΠϯͷϧʔϓͷଞʹɺ৽ͨʹهࣄͷҰཡΛද͍ࣔͤͨ͞ͱ͖ɻ ྫ͑͹ ʮαΠυόʔʹಛఆͷΧςΰϦʔʹଐ͢ΔهࣄΛ̑݅ද͓͖͍ࣔͤͯͨ͞ʯ

    ʮαΠυόʔʹਓؾͷ͋ΔهࣄΛ̑݅ද͓͖͍ࣔͤͯͨ͞ʯ ʮಛఆͷ౤ߘΛআ͍ͯɺ͓͢͢ΊͷهࣄΛ̍̌݅ද͍ࣔͨ͠ʯ ͳͲͳͲʜ ͜͏͍ͬͨ৔໘Ͱ get_posts() Λ࢖͑͹ɺ8PSE1SFTT ͕ߦͬͯ͘ΕΔ 63- ʹΑΔσϑΥϧτͷॲཧʹӨڹΛ༩͑ͣɺ༧ظͤ͵໰୊΋ى͜͠ʹ͍͘ϧʔϓͷ ࡞੒͕Ͱ͖·͢ʂ
  32. 5th WordBench Chiba ᶅQSF@HFU@QPTUT ͬͯͳΜͩʁ

  33. 5th WordBench Chiba ᶅQSF@HFU@QPTUT ͬͯͳΜͩʁ <QSF> ʮ͋Β͔͡Ίʯ ʮʜҎલͷʯ ʮʜͷલ෦ʹ͋Δʯͷҙɻ Ҿ༻ɿHPP

    ࣙయ ͋Β͔͡Ί HFU@QPTUT
  34. 5th WordBench Chiba ᶅQSF@HFU@QPTUT ͬͯͳΜͩʁ <QSF> ʮ͋Β͔͡Ίʯ ʮʜҎલͷʯ ʮʜͷલ෦ʹ͋Δʯͷҙɻ Ҿ༻ɿHPP

    ࣙయ ͋Β͔͡Ί HFU@QPTUT ʊਓਓਓਓਓਓਓਓਓਓਓਓਓʊ ʼɹ͋Β͔͡Ί HFU@QPTUT ʂɹʻ ʉ:?:?:?:?:?:?:?:?:?ʉ
  35. 5th WordBench Chiba ᶅQSF@HFU@QPTUT ͬͯͳΜͩʁ Web Web Theme Database

  36. 5th WordBench Chiba ᶅQSF@HFU@QPTUT ͬͯͳΜͩʁ Web Web Theme Database pre_get_posts

  37. 5th WordBench Chiba ᶅQSF@HFU@QPTUT ͬͯͳΜͩʁ Web Web Theme Database pre_get_posts

    63- Λ΋ͱʹ ʠσϑΥϧτͰηοτ͞ΕΔઃఆ܈ʡ ʹɺߋʹ ʠઃఆΛࢦఆʡ ্ͨ͠Ͱσʔλϕʔε͔Β ৚݅ʹ߹͏౤ߘσʔλͷऔಘʂ
  38. 5th WordBench Chiba ᶅQSF@HFU@QPTUT ͬͯͳΜͩʁ Web Web Theme Database pre_get_posts

    query_posts() ͰʠઃఆΛ্ॻ͖ͤͣͱ΋ʡ ද͍ࣔͤͨ͞಺༰ͰهࣄͷҰཡ͕औಘ͞ΕΔʂ
  39. 5th WordBench Chiba ᶅQSF@HFU@QPTUT ͬͯͳΜͩʁ ͜͜Ͱɺ࣮ࡍͷίʔυΛݟͯ·͠ΐ͏ɻ

  40. 5th WordBench Chiba ᶅQSF@HFU@QPTUT ͬͯͳΜͩʁ <?php query_posts( array( ’ posts_per_page’

    => 10, ’ post_type’ => ’ post’ , ’ order’ => ’ DESC’ , ) ); while( have_posts() ): the_post(); …
  41. 5th WordBench Chiba ᶅQSF@HFU@QPTUT ͬͯͳΜͩʁ ’ posts_per_page’ => 10 ’

    post_type’ => ’ post’ ’ order’ => ’ DESC’
  42. 5th WordBench Chiba ᶅQSF@HFU@QPTUT ͬͯͳΜͩʁ <?php function wbchiba_get_posts($query) { if(

    is_admin() || !$query->is_main_query() ) return; if ( $query->is_home() ) { $query->set(’ posts_per_page’ => 10 ); $query->set(’ post_type’ => ’ post’ ); $query->set(’ order’ => ’ DESC’ ); } } add_action(’ pre_get_posts’ , ’ wbchiba_get_posts’ ); ?>
  43. 5th WordBench Chiba ᶅQSF@HFU@QPTUT ͬͯͳΜͩʁ ࢖༻Ͱ͖Δύϥϝʔλʔ͸ɺ81@2VFSZ ͱಉ͡Ͱ͢ɻ ࠓ·Ͱ͸࣮ࡍʹ࢖༻͢ΔςϯϓϨʔτʹ query_posts() ͱॻ͍ͯ఺ࡏ͠

    ͍ͯͨʮΫΤϦʔͷΧελϚΠζʯ͕ɺGVODUJPOTQIQ ʹͯ pre_get_ posts ͷઃఆΛߦ͏͜ͱͰɺ8FC αΠτશମͷϝΠϯϧʔϓΛ؅ཧ͢Δ ͜ͱ͕ՄೳͰ͢ɻ ͜ͷํ๏ʹ׳Ε͓ͯ͘ͱɺσʔλϕʔεʹ΋༏͘͠ແବͷগͳ͍ςϯϓϨ ʔτ࡞੒͕Մೳʹʂʂ ಺༰ͷཧղ·Ͱ͸͠ͳ͘ͱ΋ɺ·ͣ͸खΛಈ͔ͯ͠ pre_get_posts ͷ ΂ΜΓ͞Λ࣮ײͯ͠Έ·͠ΐ͏ʂʂ
  44. 5th WordBench Chiba ᶅQSF@HFU@QPTUT ͬͯͳΜͩʁ ίϐϖϓϩάϥϚʔଔۀͷۙಓ͸ ·ͣखΛಈ͔͢͜ͱ͔Βʂ

  45. 5th WordBench Chiba ࢀߟʹ͍͍ͤͯͨͩͨ͞هࣄ 8PSE1SFTT ͰϖʔδૹΓ͕ಈ͔ͳ͍ͷ͸Ͳ͏ߟ͑ͯ΋ RVFSZ@QPTUT ͕ѱ͍ ʲQSF@HFU@QPTUTɺ8PSE1SFTT ϧʔϓ·ͱΊʳ

    IUUQOPUOJMDSFBUJWFDPNCMPHBSDIJWFT ϓϥάΠϯ"1* ΞΫγϣϯϑοΫҰཡ QSFHFUQPTUT IUUQXQEPDTTPVSDFGPSHFKQ ϓϥάΠϯ @"1* ΞΫγϣϯϑοΫ Ұཡ QSF@HFU@QPTUT
  46. 5th WordBench Chiba ͝ਗ਼ௌ͍͖ͨͩ͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ