{"id":2507,"date":"2015-07-19T07:09:20","date_gmt":"2015-07-19T07:09:20","guid":{"rendered":"http:\/\/musilda.cz\/?p=2507"},"modified":"2015-07-19T07:09:20","modified_gmt":"2015-07-19T07:09:20","slug":"co-jsou-to-uzivatelska-pole-neboli-custom-fields","status":"publish","type":"post","link":"https:\/\/affinite.io\/cs\/co-jsou-to-uzivatelska-pole-neboli-custom-fields\/","title":{"rendered":"Co jsou to u\u017eivatelsk\u00e1 pole, neboli custom fields"},"content":{"rendered":"\n
S pojem custom fields, nebo tak\u00e9 u\u017eivatelsk\u00e9 pole, jste se ji\u017e ur\u010dit\u011b setkali. Pro ty, co netu\u0161\u00ed o co se jedn\u00e1, je zde podrobn\u00fd popis na co se pou\u017e\u00edvaj\u00ed a jak je vyu\u017e\u00edt.<\/p>\n\n\n\n\n\n\n\n
Pokud jste n\u011bkdy p\u00e1trali na diskuzn\u00edch f\u00f3rech, po \u0159e\u0161en\u00ed n\u011bjak\u00e9ho probl\u00e9mu, \u010dasto se v odpov\u011bd\u00edch vyskytuje pojem custom field. \u010cesk\u00fd ekvivalent k tomuto v\u00fdrazu je „u\u017eivatelsk\u00e9 pole“.<\/p>\n\n\n\n
Abychom pochopili, jak funguj\u00ed u\u017eivatelsk\u00e1 pole, mus\u00edme za\u010d\u00edt od za\u010d\u00e1tku, tedy od post\u016f.<\/p>\n\n\n\n
Ve WordPressu, jsu z\u00e1kladn\u00ed data o obsahu ukl\u00e1d\u00e1ny do tabulky wp_posts. To „wp_“ je pou\u017eito pro \u00fa\u010dely tohoto \u010dl\u00e1nku, p\u0159i instalaci si prefix tabulek m\u016f\u017eete zm\u011bnit, tak\u017ee se tabulka m\u016f\u017ee jmenovat web_posts.<\/p>\n\n\n\n
V t\u00e9to tabulce jsou r\u016fzn\u00e9 sloupce, jako je ID, post_title, post_content a dal\u0161\u00ed. Bohu\u017eel je jich v\u0161ak omezen\u00e9 mno\u017estv\u00ed.<\/p>\n\n\n\n
Co v\u0161ak d\u011blat, v p\u0159\u00edpad\u011b, \u017ee n\u00e1m 23 sloupc\u016f tabulky wp_posts nesta\u010d\u00ed?<\/p>\n\n\n\n
Pak p\u0159ich\u00e1zej\u00ed na \u0159adu u\u017eivatelsk\u00e1 pole. <\/strong><\/p>\n\n\n\n V\u00fdvoj\u00e1\u0159i WordPressu tak do datab\u00e1ze p\u0159idali dal\u0161\u00ed tabulku, kter\u00e1 m\u00e1 pouh\u00e9 \u010dty\u0159i sloupce a jmenuje se wp_postmeta.<\/p>\n\n\n\n Kdy\u017e n\u011bkdo p\u00ed\u0161e, nebo mluv\u00ed o metadata, custom fields, nebo u\u017eivatelsk\u00fdch pol\u00edch, t\u00fdk\u00e1 se to dat z t\u00e9to tabulky.<\/p>\n\n\n\n N\u00e1zvy sloupc\u016f tabulky jsou:<\/p>\n\n\n\n Proto\u017ee jsou v tabulce wp_post sloupce konkr\u00e9tn\u011b pojmenov\u00e1ny a slou\u017e\u00ed k ulo\u017een\u00ed ur\u010dit\u00e9ho \u00fadaje, zde si m\u016f\u017eete nadefinovat n\u00e1zev hodnoty, kterou chcete ulo\u017eit a k n\u00ed p\u0159i\u0159adit konkr\u00e9tn\u00ed hodnotu. Pomoc\u00ed post_id pak samotn\u00fd \u00fadaj p\u0159i\u0159ad\u00edte ke konkr\u00e9tn\u00edmu obsahu, pro na\u0161e pot\u0159eby „\u010dl\u00e1nku“.<\/p>\n\n\n\n P\u0159\u00edklad pou\u017eit\u00ed<\/strong><\/p>\n\n\n\n Vytvo\u0159\u00edte \u0161ablonu, ve kter\u00e9 budete cht\u00edt zobrazit, kolikr\u00e1t byl p\u0159e\u010dten n\u011bjak\u00fd \u010dl\u00e1nek. Uvedu jednoduch\u00fd postup, kter\u00fd nepo\u010d\u00edt\u00e1 s t\u00edm, \u017ee jeden \u010dten\u00e1\u0159 m\u016f\u017ee \u010dl\u00e1nek na\u010d\u00edst znovu, ale prost\u011b p\u0159i ka\u017ed\u00e9m zobrazen\u00ed aktualizujeme hodnotu.<\/p>\n\n\n\n Budeme pracovat s t\u00edm, \u017ee k\u00f3d pro po\u010det zobrazen\u00ed vlo\u017e\u00edme do single.php, co\u017e je detail \u010dl\u00e1nku.<\/p>\n\n\n\n Jako prvn\u00ed z\u00edsk\u00e1me hodnotu na\u010dten\u00ed z datab\u00e1ze:<\/strong><\/p>\n\n\n\n $zobrazeni = get_post_meta($post->ID,’pocet-nacteni‘, true);<\/p>\n\n\n\n Ve funkci get_post_meta<\/strong> vid\u00edme id \u010dl\u00e1nku, jm\u00e9no u\u017eivatelsk\u00e9ho pole a hodnotu true. Posledn\u00ed hodota je tam proto\u017ee pot\u00e9 funkce vrac\u00ed hodnotu jako \u0159et\u011bzec. Kdy\u017e ji nepou\u017eijeme, nebo pou\u017eijeme false, bude se n\u00e1m vracet pole a my budeme muset zobrazit hodnotu pomoc\u00ed echo $zobrazeni[0]<\/strong>.<\/p>\n\n\n\n Je\u0161t\u011b pozn\u00e1mka k $post->ID<\/strong>. Pokud se v\u00e1m tato hodnota nena\u010dte, tak p\u0159ed funkci (osobn\u011b to d\u00e1v\u00e1m na za\u010d\u00e1tek souboru) p\u0159idejte global $post;<\/strong>.<\/p>\n\n\n\n Druh\u00fdm krokem bude jej\u00ed update a ulo\u017een\u00ed:<\/strong><\/p>\n\n\n\n Nyn\u00ed mus\u00edme p\u0159i\u010d\u00edst jedno zobrazen\u00ed, tak\u017ee pou\u017eijeme $zobrazeni++;<\/strong> a novou hodnotu ulo\u017e\u00edme do datab\u00e1ze – update_post_meta($post->ID, ‚pocet-nacteni‘, $zobrazeni);<\/strong>.<\/p>\n\n\n\n Pro\u010d update a ne add_post_meta? Proto\u017ee je to jednodu\u0161\u0161\u00ed a update_post_meta zkontroluje, zda ji\u017e takov\u00e1 hodnota existuje a pokud ne, tak ji vytvo\u0159\u00ed. V\u00e1m pak odpad\u00e1 nutnost ps\u00e1t kontrolu a k\u00f3d je p\u0159ehledn\u011bj\u0161\u00ed. A taky je to rychlej\u0161\u00ed.<\/p>\n\n\n\n Zobrazen\u00ed ulo\u017een\u00e9ho u\u017eivatelsk\u00e9ho pole:<\/strong><\/p>\n\n\n\n Proto\u017ee ji\u017e m\u00e1te k dispozici inkrementovanou hodnotu, sta\u010d\u00ed ji jen vypsat – echo $zobrazeni;.<\/strong><\/p>\n\n\n\n Cel\u00fd k\u00f3d:<\/strong><\/p>\n\n\n\n Zobrazen\u00ed a \u00faprava u\u017eivatelsk\u00e9ho pole v administraci<\/strong><\/p>\n\n\n\n Proto\u017ee ji\u017e v\u00edme, jak z\u00edskat a upravit hodnotu custom fieldu v datab\u00e1zi, uk\u00e1\u017eeme si, jak je mo\u017en\u00e9 upravit jeho hodnotu, nebo jej p\u0159idat ru\u010dn\u011b z administrace \u010dl\u00e1nku.<\/p>\n\n\n\n Pod editorem, v jednom z metabox\u016f najdete dialog nadepsan\u00fd jako „Uivatelsk\u00e1 pole“. Pokud jej nevid\u00edte, mus\u00edte povolit jeho zobrazen\u00ed. Pod horm\u00edm okrajem str\u00e1nky je mal\u00e1 z\u00e1lo\u017eka „Nastaven\u00ed zobrazen\u00fdch informac\u00ed“, kde za\u0161krtnete p\u0159\u00edslu\u0161n\u00fd checkbox.<\/p>\n\n\n Pak najd\u011bte na str\u00e1nce tento metabox<\/p>\n\n\n Jak vid\u00edte, m\u016f\u017eete ka\u017edou hodnotu u\u017eivatelsk\u00e9ho pole zm\u011bnit, nebo m\u016f\u017eete konkr\u00e9tn\u00ed custom field smazat.<\/p>\n\n\n\n Ve spodn\u00ed \u010d\u00e1sti metaboxu pak m\u016f\u017eete vytvo\u0159it nov\u00e9 u\u017eivatelsk\u00e9 pole<\/p>\n\n\n Tip: <\/strong>pokud nechcete, aby se u\u017eivatelsk\u00e9 pole zobrazovalo v tomto metaboxu, dejte na za\u010d\u00e1tek jeho n\u00e1zvu podtr\u017e\u00edtko. Nap\u0159\u00edklad _tajne_pole se nezobraz\u00ed a u\u017eivatel jej tak nebude moci p\u0159epsat.<\/p>\n\n\n\n Ostatn\u00ed u\u017eivatelsk\u00e1 pole<\/strong><\/p>\n\n\n\n V \u010dl\u00e1nku jsme se v\u011bnovali p\u0159edev\u0161\u00edm post\u016fm, proto\u017ee zde jsou custom fieldy nejv\u00edce pou\u017e\u00edvan\u00e9. Av\u0161ak krom\u011b wp_postmeta, existuj\u00ed i tabulky wp_commentmeta a wp_usermeta, jen\u017e funguj\u00ed obdobn\u011b.<\/p>\n\n\n\n Tak\u017ee, kdy\u017e budete cht\u00edt p\u0159idat k u\u017eivateli nap\u0159\u00edklad jeho hmotnost, budete postupovat obdobn\u011b, pouze s odpov\u00eddaj\u00edc\u00edmi funkcemi.<\/p>\n\n\n\n Z\u00e1v\u011br<\/strong><\/p>\n\n\n\n V tomto \u010dl\u00e1nku jsem cht\u011bl popsat, jak funguj\u00ed u\u017eivatelsk\u00e1 pole a proto\u017ee WordPress je r\u00e1jem plugin\u016f, tak m\u00e1te mo\u017enost vytv\u00e1\u0159et custom fieldy „klikac\u00edm zp\u016fsobem“, nap\u0159\u00edklad d\u00edky v\u00fdborn\u00e9mu pluginu Advanced custom field.<\/p>\n\n\n\n V ka\u017ed\u00e9m p\u0159\u00edpad\u011b zva\u017ete, kdy m\u00e1 smysl pou\u017e\u00edt robustn\u00ed plugin a kdy je lep\u0161\u00ed, napsat n\u011bkolik \u0159\u00e1dk\u016f php.<\/p>\n","protected":false},"excerpt":{"rendered":" S pojem custom fields, nebo tak\u00e9 u\u017eivatelsk\u00e9 pole, jste se ji\u017e ur\u010dit\u011b setkali. Pro ty, co netu\u0161\u00ed o co se jedn\u00e1, je zde podrobn\u00fd popis na co se pou\u017e\u00edvaj\u00ed a jak je vyu\u017e\u00edt.<\/p>\n","protected":false},"author":1,"featured_media":1484,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,30],"tags":[108,395],"class_list":["post-2507","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-navody","category-wordpress","tag-custom-fields","tag-uzivatelska-pole"],"_links":{"self":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/posts\/2507"}],"collection":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/comments?post=2507"}],"version-history":[{"count":0,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/posts\/2507\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/media\/1484"}],"wp:attachment":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/media?parent=2507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/categories?post=2507"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/tags?post=2507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}\n
global $post;\n$zobrazeni = get_post_meta($post->ID,'pocet-nacteni', true);\n\n$zobrazeni++;\n\nupdate_post_meta($post->ID, 'pocet-nacteni', $zobrazeni);\n\necho $zobrazeni;\n<\/pre>\n\n\n\n
<\/figure><\/div>\n\n\n
<\/figure><\/div>\n\n\n
<\/figure><\/div>\n\n\n