is_author()<\/li>\n<\/ul>\nJedn\u00e1 se pouze o \u010d\u00e1st funkc\u00ed, kter\u00e9 m\u016f\u017eete pou\u017e\u00edt, pro kompletn\u00ed seznam doporu\u010duji Codex.<\/p>\n
\u00dapravy loopu pomoc\u00ed WP_query, get_posts a query_posts<\/h2>\n
A nyn\u00ed se dost\u00e1v\u00e1me do zaj\u00edmav\u00e9 \u010d\u00e1sti \u010dl\u00e1nku. Pokud si tuto \u010d\u00e1st pe\u010dliv\u011b p\u0159e\u010dtete a dopln\u00edte znalosti z Codexu, tak budete schopni vy\u0159e\u0161it mnoho probl\u00e9m\u016f typu – pot\u0159ebuji vypsat \u010dl\u00e1nky z kategorie, podle autora, se\u0159azen\u00e9 dle abecedy, atd…<\/p>\n
V\u0161echny t\u0159i funkce umo\u017e\u0148uji manipulovat s definov\u00e1n\u00edm loopu a proto si vysv\u011btl\u00edme, kdy je vhodn\u00e9 jakou pou\u017e\u00edt.<\/p>\n
<\/h2>\n
Get_posts je funkce, kter\u00e1 vyp\u00ed\u0161e \u010dl\u00e1nky, dle p\u0159esn\u011b definovan\u00fdch argument\u016f. Jedn\u00e1 se o template tag, tak\u017ee se jedn\u00e1 pouze o v\u00fdpis \u010dl\u00e1nk\u016f dle parametr\u016f, funkce nijak nemanipuluje s defaultn\u00edm loopem, na rozd\u00edl od druh\u00fdch dvou funkc\u00ed. T\u00edm se dost\u00e1v\u00e1m k vyu\u017eit\u00ed funkce. Get_posts m\u016f\u017eete v podstat\u011b definovat kde chcete v \u0161ablon\u011b, nijak t\u00edm neovlivn\u00edte dal\u0161\u00ed v\u00fdpisy. Toto je pom\u011brn\u011b \u010dast\u00fd probl\u00e9m a get_posts je \u0159e\u0161en\u00edm.<\/p>\n
Pou\u017eit\u00ed:<\/p>\n
\n$posts_array = get_posts( $args );\n<\/pre>\nSeznam argument\u016f:<\/p>\n
\n$args = array(\n\t'posts_per_page' => 5,\n\t'offset' => 0,\n\t'category' => '',\n\t'orderby' => 'post_date',\n\t'order' => 'DESC',\n\t'include' => '',\n\t'exclude' => '',\n\t'meta_key' => '',\n\t'meta_value' => '',\n\t'post_type' => 'post',\n\t'post_mime_type' => '',\n\t'post_parent' => '',\n\t'post_status' => 'publish',\n\t'suppress_filters' => true );\n<\/pre>\n\u010casto se m\u016f\u017eete setkat se z\u00e1pisem argument\u016f p\u0159\u00edmo v z\u00e1vork\u00e1ch, ale to je prost\u011b fujky a ned\u00e1 se v tom po\u0159\u00e1dn\u011b vyznat.<\/p>\n
P\u0159\u00edklad pou\u017eit\u00ed:<\/p>\n
\n$args= array(\n 'posts_per_page' => 5,\n 'category' => 1\n);\n\n$myposts = get_posts( $args );\nforeach ( $myposts as $post ) : setup_postdata( $post ); ?>\n<a href="\"><\/a><\/h2>\n\n<a href="\">Cel\u00fd \u010dl\u00e1nek<\/a>\n\n<\/pre>\n
Tento p\u0159\u00edklad vyp\u00ed\u0161e 5 posledn\u00edch \u010dl\u00e1nk\u016f z kategorie s ID 1.<\/p>\n
Query_post<\/h2>\n
Query_post je funkce, kter\u00e1 je podobn\u00e1 get_posts, m\u00e1 podobn\u00e9, nebo stejn\u00e9 argumenty pro zobrazov\u00e1n\u00ed \u010dl\u00e1nk\u016f, rozd\u00edl je v tom, \u017ee query_post ji\u017e manipuluje se z\u00e1kladn\u00edm loopem, proto je t\u0159eba pou\u017e\u00edvat funkci wp_reset_query(), kter\u00e1 nastav\u00ed loop na defaultn\u00ed hodnotu. Pro p\u0159\u00edklad, chcete m\u00edt nad ka\u017edou str\u00e1nkou m\u00edt box s v\u00fdpisem posledn\u00edch t\u0159\u00ed \u010dl\u00e1nk\u016f. Pokud pou\u017eijete query_post, ovlivn\u00ed argumenty i v\u00fdpis str\u00e1nky a v\u00e1m se pak bude zobrazovat trochu n\u011bco jin\u00e9ho, ne\u017e jste m\u011bli v z\u00e1m\u011bru.<\/p>\n
Pou\u017eit\u00ed:<\/p>\n
\n$args = array(\n\t'cat' => 2,\n\t'order' => 'ASC'\n);\nquery_posts( $args );\nwhile ( have_posts() ) : the_post();\n<a href="\"><\/a><\/h2>\n\n<a href="\">Cel\u00fd \u010dl\u00e1nek<\/a>\nendwhile;\nwp_reset_query();\n<\/pre>\n
Takto pou\u017eit\u00e1 funkce v\u00e1m vyp\u00ed\u0161e v\u0161echny \u010dl\u00e1nky z kategorie s ID 2, se\u0159azen\u00e9 vzestupn\u011b. Jak vid\u00edte, na rozd\u00edl od get_posts neproch\u00e1z\u00edme pole $myposts, ale upravili jsme p\u0159\u00edmo v\u00fdpis loopu. Na konci jsme vr\u00e1tili v\u00fdchoz\u00ed hodnoty pomoc\u00ed wp_reset_query.<\/p>\n
WP_query<\/h2>\n
WP_query je t\u0159\u00edda, pomoc\u00ed kter\u00e9 m\u016f\u017eete definovat v\u00fdpis obsahu v \u0161ablon\u011b. d\u00e1 se \u0159\u00edci, \u017ee pou\u017eit\u00ed je podobn\u00e9 query_posts, ale WP_query m\u00e1 mnoho argument\u016f, pomoc\u00ed kter\u00fdch zobraz\u00edte v\u0161e co budete cht\u00edt, od \u010dl\u00e1nk\u016f z kategorie, a\u017e po v\u00fdpis custom post type, dle taxonomies. P\u0159esto\u017ee, se mu\u017ee na prvn\u00ed pohled zd\u00e1t pr\u00e1ce s t\u0159\u00eddou slo\u017eit\u00e1, nen\u00ed tomu tak a z\u00e1kladn\u00ed pou\u017eit\u00ed je snadn\u00e9.<\/p>\n
P\u0159\u00edklad:<\/p>\n
\n$args = array(\n'post_type' => 'post',\n'cat' => 2\n);\n\n$query = new WP_Query( $args );\n\nif ( $the_query->have_posts() ) :\nwhile ( $the_query->have_posts() ) : $the_query->the_post(); ?>\n<\/h1>\n\n\n\n\n>p>Nenalezeno<\/p>\n\n\n<\/pre>\n
Jak vid\u00edte pou\u017eit\u00ed je t\u00e9m\u011b\u0159 identick\u00e9, jako query_posts, ale mo\u017enosti t\u0159\u00eddy jsou daleko v\u011bt\u0161\u00ed, viz Codex<\/a>. J\u00e1 osobn\u011b query_posts v\u016fbec nepou\u017e\u00edv\u00e1m, proto\u017ee WP_query nab\u00edz\u00ed v\u011bt\u0161\u00ed mo\u017enosti.<\/p>\nZ\u00e1v\u011br<\/h2>\n
Nyn\u00ed ji\u017e m\u016f\u017eete upravovat v\u00fdpis obsahu v \u0161ablon\u011b jak budete pot\u0159ebovat a v\u011b\u0159te, \u017ee mnoho probl\u00e9m\u016f, na kter\u00e9 nar\u00e1\u017e\u00edm na diskuz\u00edch, nebo mne n\u011bkdo pros\u00ed o pomoc, se daj\u00ed vy\u0159e\u0161it pomoc\u00ed spr\u00e1vn\u00e9ho pou\u017eit\u00ed t\u011bchto dvou funkc\u00ed a jedn\u00e9 t\u0159\u00eddy.
\nD\u00edky get_posts lehce vytvo\u0159\u00edte v\u00fdpis posledn\u00edch \u010dl\u00e1nk\u016f z kategorie do sidebaru a pomoc\u00ed WP_query si z hlavn\u00ed strany m\u016f\u017eete klidn\u011b magaz\u00edn. <\/p>\n","protected":false},"excerpt":{"rendered":"
Pokud jste se setkali p\u0159i pro\u010d\u00edt\u00e1n\u00ed Codexu, nebo n\u011bkter\u00e9ho z n\u00e1vod\u016f pro WordPress s pojmem Loop a chcete porozum\u011bt, co tento v\u00fdraz znamen\u00e1, je tento \u010dl\u00e1nek ur\u010den v\u00e1m. Loop je jedn\u00edm ze z\u00e1kladn\u00edch kamen\u016f WordPress \u0161ablony a d\u00e1 se \u0159\u00edci, \u017ee bez n\u011bj by nebyla pr\u00e1ce s t\u00edmto redak\u010dn\u00edm syst\u00e9mem tak jednoduch\u00e1.<\/p>\n","protected":false},"author":1,"featured_media":8614,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,30],"tags":[173,229,314,546],"class_list":["post-970","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-navody","category-wordpress","tag-get_posts","tag-loop","tag-query","tag-wp_query"],"_links":{"self":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/posts\/970"}],"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=970"}],"version-history":[{"count":0,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/posts\/970\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/media\/8614"}],"wp:attachment":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/media?parent=970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/categories?post=970"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/tags?post=970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}