{"id":5921,"date":"2021-04-16T15:51:44","date_gmt":"2021-04-16T15:51:44","guid":{"rendered":"https:\/\/musilda.cz\/?p=5921"},"modified":"2021-04-16T15:51:44","modified_gmt":"2021-04-16T15:51:44","slug":"jak-ziskat-id-stranky-na-zaklade-nazvu-v-url","status":"publish","type":"post","link":"https:\/\/affinite.io\/cs\/jak-ziskat-id-stranky-na-zaklade-nazvu-v-url\/","title":{"rendered":"Jak z\u00edskat id str\u00e1nky na z\u00e1klad\u011b n\u00e1zvu v url"},"content":{"rendered":"\n

Z\u00edskat url pomoc\u00ed id str\u00e1nky jde pom\u011brn\u011b snadno, ale co kdy\u017e se dostanete do situace, \u017ee to pot\u0159ebujte obr\u00e1cen\u011b? <\/p>\n\n\n\n

Dostal jsem se do situace, \u017ee jsem pot\u0159eboval ze sitemapy vyhodit url custom 404 str\u00e1nky. Vytvo\u0159il jsem si toti\u017e p\u0159esm\u011brov\u00e1n\u00ed na str\u00e1nku, kterou jsem poskl\u00e1dal pomoc\u00ed Gutenbergu, aby trochu vypadala. Vtipn\u00fd obr\u00e1zek, formul\u00e1\u0159 na vyhled\u00e1v\u00e1n\u00ed a odkazy na kategorie produkt\u016f. <\/p>\n\n\n\n

SEO majstri mi ale hned hodili na hlavu, \u017ee je ta str\u00e1nka v xml sitemap. A \u017ee mus\u00ed pry\u010d. <\/p>\n\n\n\n

Na webu byl Yoast SEO a ten m\u00e1 pro vylou\u010den\u00ed post\u016f ze sitemapy filter, kter\u00fd jsem cht\u011bl pou\u017e\u00edt. Filter ale p\u0159ij\u00edm\u00e1 pole id post\u016f.<\/p>\n\n\n\n

Co\u017e byl probl\u00e9m, proto\u017ee jsem necht\u011bl ps\u00e1t podm\u00ednky a kontrolovat, zda jsem na develu, nebo produkci a podle toho tam vkl\u00e1dat id str\u00e1nky (devel obasahem neodpov\u00eddal \u00fapln\u011b produkci). V\u011bd\u011bl jsem ale, \u017ee na obou webech bude m\u00edt str\u00e1nka slug „stranka-nenalezena“. <\/p>\n\n\n\n

A WordPress m\u00e1 pr\u00edma funkci, kter\u00e1 v\u00e1m vr\u00e1t\u00ed objekt post, podle jeho slugu – get_page_by_path().<\/p>\n\n\n\n

Tahle funkce m\u00e1 t\u0159i argumenty get_page_by_path( $path, $output, $post_type ). <\/p>\n\n\n\n

Prvn\u00ed je slug postu – stranka-nenalezena
Druh\u00fd je typ vr\u00e1cen\u00e9 hodnoty – defaultn\u011b je to object
T\u0159et\u00ed je custom post type – defaultn\u011b page<\/p>\n\n\n\n

No a v\u00fdsledek je zde:<\/p>\n\n\n\n

add_filter(\u00a0'wpseo_exclude_from_sitemap_by_post_ids',\u00a0function\u00a0()\u00a0{\n\n\u00a0\u00a0\u00a0\u00a0$ids\u00a0=\u00a0array(\u00a012,\u00a013,\u00a014,\u00a04552\u00a0);\n\u00a0\u00a0\u00a0\u00a0$page\u00a0=\u00a0get_page_by_path(\u00a0'stranka-nenalezena'\u00a0);\n\u00a0\u00a0\u00a0\u00a0if\u00a0(\u00a0is_object(\u00a0$page\u00a0)\u00a0){\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$ids[]\u00a0=\u00a0$page->ID;\n\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0return\u00a0$ids;\n}\u00a0);<\/pre>\n\n\n\n

PS: stejn\u011b to sem p\u00ed\u0161u, abych to za p\u016fl roku na\u0161el \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"

Z\u00edskat url pomoc\u00ed id str\u00e1nky jde pom\u011brn\u011b snadno, ale co kdy\u017e se dostanete do situace, \u017ee to pot\u0159ebujte obr\u00e1cen\u011b? Dostal jsem se do situace, \u017ee jsem pot\u0159eboval ze sitemapy vyhodit url custom 404 str\u00e1nky. Vytvo\u0159il jsem si toti\u017e p\u0159esm\u011brov\u00e1n\u00ed na str\u00e1nku, kterou jsem poskl\u00e1dal pomoc\u00ed Gutenbergu, aby trochu vypadala. Vtipn\u00fd obr\u00e1zek, formul\u00e1\u0159 na vyhled\u00e1v\u00e1n\u00ed a<\/p>\n","protected":false},"author":1,"featured_media":5600,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[19],"tags":[],"class_list":["post-5921","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-snippety"],"_links":{"self":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/posts\/5921"}],"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=5921"}],"version-history":[{"count":0,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/posts\/5921\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/media\/5600"}],"wp:attachment":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/media?parent=5921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/categories?post=5921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/tags?post=5921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}