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

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

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

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

EisukeAkimoto

August 25, 2013
Tweet

Other Decks in Technology

Transcript

  1. 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ΑΓҾ༻
  2. 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 Ͱ νΣοΫͯ͠ ʠσϑΥϧτͰηοτ͞ΕΔઃఆ܈ʡ Λมߋ͢Δ͜ͱ͕ڧ͘ਪ঑͞Εͯ ͍·͢ɻ
  3. 5th WordBench Chiba ᶄRVFSZ@QPTUT ͱ HFU@QPTUT HFU@QPTUTͱ͸ ςϯϓϨʔτλά get_posts() ͸ɺෳ਺ͷϧʔϓΛ࡞੒͢ΔͨΊͷγϯϓϧͳ

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

    λάͰ͢ɻ͜ΕΛ࢖ͬͯɺ࠷৽ͷ౤ߘ΍৚݅ʹҰக͢Δ౤ߘΛऔಘ͢Δ͜ͱ͕Ͱ͖ ·͢ɻ ͭ·Γɺ ʠσϑΥϧτͰηοτ͞ΕΔઃఆ܈ʡ ɹʹӨڹͤͣϧʔϓΛ࡞੒͢Δ͜ͱ͕ Ͱ͖·͢ɻ
  5. 5th WordBench Chiba ᶄRVFSZ@QPTUT ͱ HFU@QPTUT HFU@QPTUT͕దͯ͠Δ৔໘ ϝΠϯͷϧʔϓͷଞʹɺ৽ͨʹهࣄͷҰཡΛද͍ࣔͤͨ͞ͱ͖ɻ ྫ͑͹ ʮαΠυόʔʹಛఆͷΧςΰϦʔʹଐ͢ΔهࣄΛ̑݅ද͓͖͍ࣔͤͯͨ͞ʯ

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

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

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

    query_posts() ͰʠઃఆΛ্ॻ͖ͤͣͱ΋ʡ ද͍ࣔͤͨ͞಺༰ͰهࣄͷҰཡ͕औಘ͞ΕΔʂ
  9. 5th WordBench Chiba ᶅQSF@HFU@QPTUT ͬͯͳΜͩʁ <?php query_posts( array( ’ posts_per_page’

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

    post_type’ => ’ post’ ’ order’ => ’ DESC’
  11. 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’ ); ?>
  12. 5th WordBench Chiba ᶅQSF@HFU@QPTUT ͬͯͳΜͩʁ ࢖༻Ͱ͖Δύϥϝʔλʔ͸ɺ81@2VFSZ ͱಉ͡Ͱ͢ɻ ࠓ·Ͱ͸࣮ࡍʹ࢖༻͢ΔςϯϓϨʔτʹ query_posts() ͱॻ͍ͯ఺ࡏ͠

    ͍ͯͨʮΫΤϦʔͷΧελϚΠζʯ͕ɺGVODUJPOTQIQ ʹͯ pre_get_ posts ͷઃఆΛߦ͏͜ͱͰɺ8FC αΠτશମͷϝΠϯϧʔϓΛ؅ཧ͢Δ ͜ͱ͕ՄೳͰ͢ɻ ͜ͷํ๏ʹ׳Ε͓ͯ͘ͱɺσʔλϕʔεʹ΋༏͘͠ແବͷগͳ͍ςϯϓϨ ʔτ࡞੒͕Մೳʹʂʂ ಺༰ͷཧղ·Ͱ͸͠ͳ͘ͱ΋ɺ·ͣ͸खΛಈ͔ͯ͠ pre_get_posts ͷ ΂ΜΓ͞Λ࣮ײͯ͠Έ·͠ΐ͏ʂʂ
  13. 5th WordBench Chiba ࢀߟʹ͍͍ͤͯͨͩͨ͞هࣄ 8PSE1SFTT ͰϖʔδૹΓ͕ಈ͔ͳ͍ͷ͸Ͳ͏ߟ͑ͯ΋ RVFSZ@QPTUT ͕ѱ͍ ʲQSF@HFU@QPTUTɺ8PSE1SFTT ϧʔϓ·ͱΊʳ

    IUUQOPUOJMDSFBUJWFDPNCMPHBSDIJWFT ϓϥάΠϯ"1* ΞΫγϣϯϑοΫҰཡ QSFHFUQPTUT IUUQXQEPDTTPVSDFGPSHFKQ ϓϥάΠϯ @"1* ΞΫγϣϯϑοΫ Ұཡ QSF@HFU@QPTUT