{"id":1096,"date":"2013-09-14T08:21:53","date_gmt":"2013-09-14T08:21:53","guid":{"rendered":"http:\/\/musilda.cz\/?p=1096"},"modified":"2013-09-14T08:21:53","modified_gmt":"2013-09-14T08:21:53","slug":"jak-vytvorit-vlastni-sablonu-pro-wordpress-4-dil","status":"publish","type":"post","link":"https:\/\/affinite.io\/cs\/jak-vytvorit-vlastni-sablonu-pro-wordpress-4-dil\/","title":{"rendered":"Jak vytvo\u0159it vlastn\u00ed \u0161ablonu pro WordPress – 4 d\u00edl"},"content":{"rendered":"\n

Od naps\u00e1n\u00ed posledn\u00edho d\u00edlu n\u00e1vodu, jak si vytvo\u0159it vlastn\u00ed WordPress \u0161ablonu, je to ji\u017e n\u011bjak\u00e1 doba, je na \u010dase p\u0159isp\u011bt nov\u00fdm d\u00edlem. V t\u00e9to \u010d\u00e1sti si dopln\u00edme soubory \u0161ablony o single.php<\/strong>, page.php<\/strong>. <\/p>\n\n\n\n

D\u00e1le si vytvo\u0159\u00edme soubor content.php a p\u0159iprav\u00edme se tak na podporu post form\u00e1t\u016f, kter\u00e9 budou sou\u010d\u00e1st\u00ed n\u011bkter\u00e9ho z dal\u0161\u00edch d\u00edl\u016f. Z\u00e1rove\u0148 dopln\u00edme \u0161ablonu o podporu n\u00e1hledov\u00fdch obr\u00e1zk\u016f.<\/p>\n\n\n\n\n\n\n\n

Soubor single.php<\/h2>\n\n\n\n

Soubor single.php<\/strong> zobrazuje detail \u010dl\u00e1nku. Pokud tedy v arch\u00edvu, nebo v\u00fdpisu kategorie kliknete na „Cel\u00fd \u010dl\u00e1nek“, WordPress zavol\u00e1 single.php<\/strong>.<\/p>\n\n\n\n

<?php get_header(); ?>\n<div class=\"row-fluid\">\n  <div class=\"span8\">\n\t <?php if (have_posts()) : while (have_posts()) : the_post(); ?>\n    <!--BEGIN .hentry -->\n\t\t<div <?php post_class() ?> id=\"post-<?php the_ID(); ?>\">\n\t\t  <h1 class=\"entry-title\"><?php the_title(); ?><\/h1>\n\t\t\t<!--BEGIN .entry-content -->\n\t\t\t<div class=\"entry-content\">\n\t\t\t<?php the_content(__('Read more...', 'theme')); ?>\n      <\/div>\n      <!--END .hentry-->\n\t\t<\/div>\n\t<?php endwhile; else: ?>\n\t\t<div id=\"post-0\" <?php post_class() ?>>\n\t\t\t<h1 class=\"entry-title\">\n<?php _e('Error 404 - Not Found', 'theme') ?>\n                        <\/h1>\n\t\t\t<!--BEGIN .entry-content-->\n\t\t\t<div class=\"entry-content\">\n<p><?php _e(\"Sorry, but you are looking for\nsomething that isn't here.\", \"theme\") ?><\/p>\n\t\t\t<\/div>\n\t\t<\/div>\n\t<?php endif; ?>\n\t<\/div>\n  <div class=\"span4\">\n    <?php get_sidebar(); ?>\n  <\/div>\n<\/div>\n<?php get_footer(); ?><\/code><\/pre>\n\n\n\n

Jak m\u016f\u017eete vid\u011bt, v souboru je nejprve loop a pokud nen\u00ed \u017e\u00e1dn\u00fd odpov\u00eddaj\u00edc\u00ed obsah, pomoc\u00ed else je zobrazeno upozorn\u011bn\u00ed o nenalezen\u00ed obsahu.<\/p>\n\n\n\n

Soubor page.php<\/h2>\n\n\n\n

Soubor page.php je v podstat\u011b statick\u00e1 str\u00e1nka. Pokud vytvo\u0159\u00edte nap\u0159\u00edklad str\u00e1nku Kontakt, p\u0159i zobrazen\u00ed WordPress zavol\u00e1 soubor page.php<\/strong>.<\/p>\n\n\n\n

<?php get_header(); ?>\n<div class=\"row-fluid\">\n  <div class=\"span8\">\n\t\t<?php if (have_posts()) : while (have_posts()) : the_post(); ?>\n\t\t  <!--BEGIN .hentry -->\n\t\t  <div <?php post_class() ?> id=\"post-<?php the_ID(); ?>\">\n        <h1 class=\"page-title\"><?php\tthe_title(); ?><\/h1>\n\t\t\t\t<div class=\"entry-content\">\n          <?php the_content(); ?>\n\t\t\t\t<\/div>\n\t\t\t<\/div>\n\t\t<?php endwhile; else: ?>\n\t\t\t<div id=\"post-0\" <?php post_class() ?>>\n\t\t\t\t<h1 class=\"entry-title\"><?php _e('Error 404 -\n        Not Found', 'theme') ?><\/h1>\n\t\t\t<div class=\"entry-content\">\n\t\t\t\t<p><?php _e(\"Sorry, but you are looking for\n        something that isn't here.\", \"theme\") ?><\/p>\n\t\t\t<\/div>\n\t\t <\/div>\n   <?php endif; ?>\n\t<\/div>\n  <div class=\"span4\">\n    <?php get_sidebar(); ?>\n  <\/div>\n<\/div>\n<?php get_footer(); ?><\/code><\/pre>\n\n\n\n

M\u016f\u017eete vid\u011bt, \u017ee soubory jsou si podobn\u00e9, ale oba pln\u00ed jinou funkci.<\/p>\n\n\n\n

Content.php<\/h2>\n\n\n\n

Zde bude pot\u0159eba trochu pov\u00edd\u00e1n\u00ed.<\/p>\n\n\n\n

Soubor content n\u00e1m umo\u017en\u00ed zjednodu\u0161it k\u00f3d v cel\u00e9 \u0161ablon\u011b a z\u00e1rove\u0148 si p\u0159iprav\u00edme \u0161ablonu na podporu post form\u00e1t\u016f. Do content.php<\/strong> toti\u017e p\u0159esuneme loop. Tedy tu \u010d\u00e1st k\u00f3du, kter\u00e1 za\u010d\u00edn\u00e1 if(have_post…. D\u00edky tomu budeme m\u00edt loop pro single, archive, category, tag, author a search na jednom m\u00edst\u011b. Pokud se pak rozhodnete, \u017ee budete cht\u00edt u p\u0159\u00edsp\u011bvku zobrazovat datum vytvo\u0159en\u00ed, uprav\u00edte pouze content.php<\/strong> a nemus\u00edte upravovat dal\u0161\u00ed soubory. Vyj\u00edmkou je page.php<\/strong>.<\/p>\n\n\n\n

Soubor content.php<\/strong> budeme z ostatn\u00edch soubor\u016f volat pomoc\u00ed:<\/p>\n\n\n\n

get_template_part( 'content', get_post_format() );\n<\/code><\/pre>\n\n\n\n

Funkce get_template_part na\u010dte definovan\u00fd soubor a funkce get_post_format ur\u010duje jeho n\u00e1zev v z\u00e1vislosti na pou\u017eit\u00e9m post format. Jestli\u017ee tedy nem\u00e1te podporu post form\u00e1t\u016f, nemus\u00edte si s t\u00edm d\u011blat starosti, na\u010dte se v\u017edy content.php<\/strong>. Pokud \u0161ablona podporuje nap\u0159\u00edklad video a existuje soubor content-video.php<\/strong>, na\u010dte se tento soubor. V p\u0159\u00edpad\u011b, \u017ee neexistuje, op\u011bt se zobraz\u00ed content.php<\/strong>.<\/p>\n\n\n\n

<div <?php post_class() ?> id=\"post-<?php the_ID(); ?>\">\n  <?php\n  if ( is_search()|| is_home() || is_archive()\n   || is_tag() || is_category() ) :\n  ?>\n\n\t  <h2 class=\"title\">\n\t    <a href=\"<?php the_permalink() ?>\"\n      title=\"<?php the_title(); ?>\">\n        <?php the_title(); ?>\n      <\/a>\n\t  <\/h2>\n\n      <div class=\"post-content\">\n\t     <?php echo the_excerpt();?>\n\t   <\/div>\n\n  <?php else: ?>\n\n  <h1 class=\"entry-title\"><?php the_title(); ?><\/h1>\n  <div class=\"post-content\">\n\t<?php the_content(__('Read more...', 'theme')); ?>\n  <\/div>\n\n  <?php endif: ?>\n<\/div><\/code><\/pre>\n\n\n\n

Jak vid\u00edte, soubor je rozd\u011blen podm\u00ednkou na dv\u011b \u010d\u00e1sti. Prvn\u00ed je pro v\u0161echny archive, category, author, tag, search soubory a obsahuje excerpt, tedy v\u00fdpis \u010d\u00e1sti \u010dl\u00e1nku. Druh\u00fd je pro single a obsahuje v\u00fdpis cel\u00e9ho \u010dl\u00e1nku – content.<\/p>\n\n\n\n

Nyn\u00ed mus\u00edte proj\u00edt author.php<\/strong>, archive.php<\/strong>, category.php<\/strong>, search.php<\/strong>, tag.php<\/strong> a single.php<\/strong> a \u010d\u00e1st mezi while a endhile nahradit<\/p>\n\n\n\n

<?php get_template_part( 'content', get_post_format() ); ?><\/code><\/pre>\n\n\n\n

V\u00fdsledek tedy bude:<\/p>\n\n\n\n

<?php if (have_posts()) :\n     while (have_posts()) : the_post();  ?>\n\n   <?php get_template_part( 'content', get_post_format() ); ?>\n\n    <?php endwhile;  ?><\/code><\/pre>\n\n\n\n

Tento postup v\u00e1m v budoucnu u\u0161et\u0159\u00ed spoustu pr\u00e1ce. Mo\u017en\u00e1 se nyn\u00ed pt\u00e1te, pro\u010d jsem to takto nepsal od za\u010d\u00e1tku, je to proto\u017ee jsem cht\u011bl, aby jste vid\u011bli, jak je v souborech um\u00edst\u011bn loop a co obsahuje.<\/p>\n\n\n\n

N\u00e1hledov\u00e9 obr\u00e1zky<\/h2>\n\n\n\n

N\u00e1hledov\u00e9 obr\u00e1zky jsem popsal v \u010dl\u00e1nku N\u00e1hledov\u00e9 obr\u00e1zky ve WordPressu<\/a> a proto se budu zab\u00fdvat jen jejich vlo\u017een\u00edm do \u0161ablony.<\/p>\n\n\n\n

Pro podporu n\u00e1hledov\u00fdch obr\u00e1zk\u016f mus\u00edme do functions.php vlo\u017eit tento k\u00f3d:<\/p>\n\n\n\n

if ( function_exists( 'add_theme_support' ) ) {\n\tadd_theme_support( 'post-thumbnails' );\n\tset_post_thumbnail_size( 56, 56, true ); \/\/ Normal post thumbnails\n\tadd_image_size( 'sidebar', 50, 50, true ); \/\/ Sidebar thumbnail\n        add_image_size( 'blog', 619, 300, true ); \/\/ Blog thumbnail\n\n}<\/code><\/pre>\n\n\n\n

Jen pro vysv\u011btlen\u00ed, rovnou vytvo\u0159\u00edme mal\u00e9 n\u00e1hledy pro vyu\u017eit\u00ed v sidebaru ve velikosti 50×50 pixel\u016f a n\u00e1hledov\u00e9 obr\u00e1zky, kter\u00e9 pou\u017eijeme ve v\u00fdpisu \u010dl\u00e1nk\u016f budou p\u0159es celou \u0161\u00ed\u0159ku obsahu, proto 619px.<\/p>\n\n\n\n

Do souboru content.php vlo\u017e\u00edme tento k\u00f3d:<\/p>\n\n\n\n

<?php if ( has_post_thumbnail() ) : ?>\n       <div class=\"entry-thumbnail\">\n\t\t<?php the_post_thumbnail('blog'); ?>\n       <\/div>\n<?php endif; ?><\/code><\/pre>\n\n\n\n

V\u00fdsledek bude vypadat takto:<\/p>\n\n\n\n

<div <?php post_class() ?> id=\"post-<?php the_ID(); ?>\">\n  <?php\n  if ( is_search()|| is_home() || is_archive()\n   || is_tag() || is_category() ) :\n  ?>\n\n  <?php if ( has_post_thumbnail() && ! post_password_required() ) : ?>\n\t\t<div class=\"entry-thumbnail\">\n\t\t\t<?php the_post_thumbnail(); ?>\n\t\t<\/div>\n\t<?php endif; ?>\n\n\t  <h2 class=\"title\">\n\t    <a href=\"<?php the_permalink() ?>\"\n      title=\"<?php the_title(); ?>\">\n        <?php the_title(); ?>\n      <\/a>\n\t  <\/h2>\n\n      <div class=\"post-content\">\n\t     <?php echo the_excerpt();?>\n\t   <\/div>\n\n  <?php else: ?>\n\n  <h1 class=\"entry-title\"><?php the_title(); ?><\/h1>\n  <div class=\"post-content\">\n\t<?php the_content(__('Read more...', 'theme')); ?>\n  <\/div>\n\n  <?php endif: ?>\n<\/div><\/code><\/pre>\n\n\n\n

A to je v\u0161e.<\/p>\n\n\n\n

D\u00edky tomu, \u017ee jsme vytvo\u0159ili content.php<\/strong> a do n\u011bj p\u0159esunuli loop, nemus\u00edte ji\u017e nikam jinam vkl\u00e1dat k\u00f3d pro zobrazen\u00ed thumbnails. V tuto chv\u00edli m\u00e1me \u0161ablonu, kter\u00e1 um\u00ed zobrazit v\u00fdpisy \u010dl\u00e1nk\u016f, detail \u010dl\u00e1nku, str\u00e1nku a nav\u00edc podporuje n\u00e1hledov\u00e9 obr\u00e1zky.<\/p>\n\n\n\n

Douf\u00e1m, \u017ee se mi poda\u0159\u00ed dal\u0161\u00ed d\u00edl napsat d\u0159\u00edve, ne\u017e za t\u0159i m\u011bs\u00edce. V n\u011bm se chci zam\u011b\u0159it na vytvo\u0159en\u00ed custom homepage, vytvo\u0159en\u00ed template pro str\u00e1nku se sidebarem a se sidebarem na lev\u00e9 stran\u011b.<\/p>\n\n\n\n

Seri\u00e1l: Jak vytvo\u0159it vlastn\u00ed \u0161ablonu pro WordPress<\/h2>\n\n\n\n