Permalink
Browse files

R-07 (#28)

* R-07 - Register and render dynamic sidebar.

* R-07 - Layout for active and inactive sidebars.

* R-07 - Sidebar for singulars.

* R-07 - Widget areas outside of sidebar.

* R-07 - archive.php

* R-07 - date.php

* R-07 - author.php.

* R-07 - Code added on workshop: search and searchform templates, archive title filter, fixed empty excerpt bug in nav walker.

* R-07 - Documentation.
  • Loading branch information...
zzap committed Jan 4, 2019
1 parent 64f7257 commit 6e5382e0b758d6a0d183bc995002fb2ba0c92249
@@ -41,6 +41,7 @@ Sadržaj:
- [Komentari](#komentari)
- [Formati članka](#formati-članka)
- [Prilagođeni šablon članka](#prilagođeni-šablon-članka)
- [R-07 - Vidžeti i arhive](#r-07)

# R-01

@@ -298,3 +299,28 @@ Smernice za pisanje kvalitetnog CSS koda koji je moguće održavati na duge staz

- [Prilagođeni šablon članka](https://developer.wordpress.org/themes/template-files-section/page-template-files/#creating-page-templates-for-specific-post-types)
- [@since 4.7](https://make.wordpress.org/core/2016/11/03/post-type-templates-in-4-7/)

# R-07

## Vidžeti i bočna traka

- [Bočna traka](https://developer.wordpress.org/themes/functionality/sidebars/)
- [register_sidebar()](https://developer.wordpress.org/reference/functions/register_sidebar/)
- filter [widgets_init](https://developer.wordpress.org/reference/hooks/widgets_init/)
- [get_sidebar()](https://developer.wordpress.org/reference/functions/get_sidebar/)
- *sidebar.php* - [dynamic_sidebar()](https://developer.wordpress.org/reference/functions/dynamic_sidebar/), [is_active_sidebar()](https://developer.wordpress.org/reference/functions/is_active_sidebar/)
- Ukoliko je aktivna bočna traka može se dodati klasa na `<body>`, putem filtera [body_class](https://developer.wordpress.org/reference/hooks/body_class/), kako bi se CSS-om lakše definisao layout.

## Arhive

- [wphierarchy.com](https://wphierarchy.com/)
- [Hijerarhija šablona](https://developer.wordpress.org/themes/basics/template-hierarchy/)
- *archives.php* - Odnosi se na sve arhive i preuzima od *index.php* ulogu šablona za arhive.
- Prikaz naziva arhive [the_archive_title()](https://developer.wordpress.org/reference/functions/the_archive_title/) i filter pomoću koga se može izmeniti [get_the_archive_title](https://developer.wordpress.org/reference/hooks/get_the_archive_title/)
- Prikaz opisa arhive [the_archive_description()](https://developer.wordpress.org/reference/functions/the_archive_description/) i filter pomoću koga se može izmeniti [get_the_archive_description](https://developer.wordpress.org/reference/hooks/get_the_archive_description/)
- *date.php* - Odnosi se na sve arhive datuma: godina, mesec i dan; preuzima od *archives.php* ulogu šablona za arhive datuma. Dodatna klasifikacija se može dobiti upotrebom *year.php*, *month.php* i *day.php* šablona.
- *author.php* - Odnosi se na arhive autora i preuzima od *archives.php* ulogu šablona za arhive autora. Moguća upotreba avatara, biografije, prilagođenog izbornika za veze ka društvenim mrežama i sl. [is_nav_menu()](https://developer.wordpress.org/reference/functions/is_nav_menu/)
- *search.php* - Arhiva rezultata pretrage.
- *searchform.php* - Posebni deo šablona koji sadrži formular za pretagu.
- [get_search_form()](https://developer.wordpress.org/reference/functions/get_search_form/)
- [get_search_query()](https://developer.wordpress.org/reference/functions/get_search_query/)
@@ -0,0 +1,33 @@
<?php
/**
* Archive Template File
*
* @link https://developer.wordpress.org/themes/template-files-section/post-template-files/
* @link https://wphierarchy.com/
*
* @package WordPress
*/
get_header(); ?>

<header class="archive-header">
<?php
the_archive_title( '<h1 class="archive-title">', '</h1>' );
the_archive_description( '<p>', '</p>' );
echo get_search_form();
?>
</header>

<?php if ( have_posts() ) : ?>
<div class="content-area">
<?php while ( have_posts() ) : the_post();
get_template_part( '/template-parts/entry' );
endwhile; // have_posts() ?>
</div>
<?php endif; // have_posts()
get_sidebar();
get_footer();
@@ -0,0 +1,37 @@
<?php
/**
* Author archives template
*
* @link https://developer.wordpress.org/themes/template-files-section/post-template-files/
* @link https://wphierarchy.com/
*
* @package WordPress
*/
get_header(); ?>

<header class="archive-header">
<?php
echo get_avatar( get_the_author_meta( 'ID' ), 200 );
the_archive_title( '<h1 class="archive-title">', '</h1>' );
the_archive_description( '<p>', '</p>' );
if ( is_nav_menu( 'social' ) ) :
wp_nav_menu( array(
'menu' => 'social'
) );
endif;
?>
</header>

<?php if ( have_posts() ) : ?>
<div class="content-area">
<?php while ( have_posts() ) : the_post();
get_template_part( '/template-parts/entry' );
endwhile; // have_posts() ?>
</div>
<?php endif; // have_posts()
get_sidebar();
get_footer();
@@ -77,4 +77,4 @@
endif; // get_option( 'comment_registration' ) && ! is_user_logged_in()
endif; // comments_open()
?>
</div>
</div>
@@ -0,0 +1,47 @@
<?php
/**
* Date Archives Template
*
* @link https://developer.wordpress.org/themes/template-files-section/post-template-files/
* @link https://wphierarchy.com/
*
* @package WordPress
*/
get_header(); ?>

<header class="archive-header">
<?php
the_archive_title( '<h1 class="archive-title">', '</h1>' );
the_archive_description( '<p>', '</p>' );
?>
</header>

<?php if ( have_posts() ) : ?>
<div class="content-area">
<ol>
<?php while ( have_posts() ) : the_post(); ?>
<li>
<article id="entry-<?php the_ID(); ?>" <?php post_class( 'entry' ); ?>>
<header class="entry-header">
<?php
// Translators: 1. published date in DATE_W3C format, 2. published date in F jS format
$date = sprintf( __( '<time class="entry-date published updated" datetime="%1$s">%2$s</time>', 'radionica' ),
get_the_date( DATE_W3C ),
get_the_date( 'F jS' )
);
the_title(
'<h2 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark" title="' . the_title_attribute( array( 'echo' => false ) ) . '">',
'</a>, ' . $date . '</h2>'
);
?>
</header>
</article>
</li>
<?php endwhile; // have_posts() ?>
</ol>
</div>
<?php endif; // have_posts()
get_sidebar();
get_footer();
@@ -121,6 +121,24 @@ function radionica_setup() {
'audio',
'chat'
) );
/**
* Post Formats
*
* @link https://developer.wordpress.org/reference/functions/add_theme_support/#post-formats
* @link https://developer.wordpress.org/themes/functionality/post-formats/
*/
add_theme_support( 'post-formats', array(
'aside',
'gallery',
'link',
'image',
'quote',
'status',
'video',
'audio',
'chat'
) );
}
add_action( 'after_setup_theme', 'radionica_setup' );
@@ -140,6 +158,45 @@ function radionica_enqueue_scripts() {
}
add_action( 'wp_enqueue_scripts', 'radionica_enqueue_scripts' );
/**
* Register sidebars and widgets
*
* This function is attached to 'widgets_init' action hook.
*/
function radionica_widgets_init() {
// Main sidebar
register_sidebar( array(
'name' => esc_html__( 'Main Sidebar', 'radionica' ),
'description' => esc_html__( 'Visible on all archives.', 'radionica' ),
'id' => 'sidebar-main',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
// Singular sidebar
register_sidebar( array(
'name' => esc_html__( 'Singular Sidebar', 'radionica' ),
'description' => esc_html__( 'Visible on Sidebar template for posts and pages.', 'radionica' ),
'id' => 'sidebar-singular',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
// Header sidebar
register_sidebar( array(
'name' => esc_html__( 'Header Sidebar', 'radionica' ),
'description' => esc_html__( 'Visible in header.', 'radionica' ),
'id' => 'sidebar-header',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="screen-reader-text">',
'after_title' => '</h3>',
) );
}
add_action( 'widgets_init', 'radionica_widgets_init' );
/**
* Navigation Walker
*/
@@ -149,4 +206,4 @@ function radionica_enqueue_scripts() {
/**
* Template functions
*/
require_once get_parent_theme_file_path( '/inc/template-functions.php' );
require_once get_parent_theme_file_path( '/inc/template-functions.php' );
@@ -21,6 +21,12 @@
<header>
<div class="wrapper">
<?php
if ( is_active_sidebar( 'sidebar-header' ) ) : ?>
<aside class="widget-area widget-area-header" role="complementary">
<?php dynamic_sidebar( 'sidebar-header' ); ?>
</aside>
<?php endif;
if ( has_custom_logo() ) :
the_custom_logo();
else :
@@ -101,4 +107,4 @@
</header>

<main>
<div class="wrapper">
<div class="wrapper">
@@ -135,9 +135,9 @@ public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0
if ( $depth == 1 && $item->object == 'post' ) :
$post_id = $item->object_id;
$description = $item->description;
$excerpt = get_the_excerpt( $post_id );
$post = get_post( $post_id );
$content = wp_trim_words( $post->post_content, 15 );
$post_object = get_post( $post_id );
$content = wp_trim_words( $post_object->post_content, 15 );
// Entry title.
$item_output .= '<div class="entry-title">';
@@ -152,14 +152,15 @@ public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0
// Entry summary.
$item_output .= '<div class="entry-summary">';
if ( has_excerpt( $post_id ) ) :
$excerpt = get_the_excerpt( $post_id );
$item_output .= apply_filters( 'the_excerpt', get_the_excerpt( $post_id ) );
else :
$item_output .= apply_filters( 'the_content', $content );
endif;
$item_output .= '</div>';
// Entry image.
if ( has_post_thumbnail() ) :
if ( has_post_thumbnail( $post_id ) ) :
$item_output .= '<div class="entry-image">';
$item_output .= get_the_post_thumbnail( $post_id, 'medium' );
$item_output .= '</div>';
@@ -190,4 +191,4 @@ public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0
}
}
endif; // ! class_exists( 'Custom_Walker_Nav_Menu' )
endif; // ! class_exists( 'Custom_Walker_Nav_Menu' )
@@ -184,3 +184,91 @@ function radionica_document_title_separator( $sep ) {
return $sep;
}
add_filter( 'document_title_separator', 'radionica_document_title_separator' );
/**
* Body classes
*
* Custom classes for <body> tag. This function
* is attached to 'body_class' filter hook. For
* overriding in child themes, simply remove
* the filter hook.
*
* @param array Existing class values.
* @return array Filtered class values.
*/
function radionica_body_class( $classes ) {
if ( is_active_sidebar( 'sidebar-main' ) && ( is_archive() || is_home() || is_search() ) ) :
$classes[] = 'has-sidebar';
endif;
if ( is_active_sidebar( 'sidebar-singular' ) && is_page_template( 'template-sidebar.php' ) ) :
$classes[] = 'has-sidebar';
endif;
if ( is_active_sidebar( 'sidebar-singular' ) && is_page_template( 'template-sidebar.php' ) ) :
$classes[] = 'has-sidebar';
endif;
return $classes;
}
add_filter( 'body_class', 'radionica_body_class' );
/**
* Filters the archive title. This function is
* attached to 'get_the_archive_title' filter hook.
*
* @since 4.1.0
*
* @param string $title Archive title to be displayed.
*/
function radionica_get_the_archive_title( $title ) {
if ( is_author() ) :
/* translators: Author archive title. 1: Author name */
$title = sprintf( __( '%s', 'radionica' ), '<span class="vcard">' . get_the_author() . '</span>' );
endif;
if ( is_search() ) :
if ( get_search_query() ) :
$title = sprintf( esc_html__( 'Search results for: %s', 'radionica' ),
get_search_query()
);
else :
$title = esc_html__( 'Search results', 'radionica' );
endif;
endif;
return $title;
}
add_filter( 'get_the_archive_title', 'radionica_get_the_archive_title' );
/**
* Filters the archive description. This function is attached to
* 'get_the_archive_description' filter hook.
*
* @since 4.1.0
*
* @param string $description Archive description to be displayed.
*/
function radionica_get_the_archive_description( $description ) {
if ( is_year() ) :
$description = sprintf( esc_html__( 'Archive for %s year.', 'radionica' ),
get_the_date( _x( 'Y', 'yearly archives date format' ) )
);
elseif ( is_month() ) :
$description = sprintf( esc_html__( 'Archive for %1$s of %2$s year.', 'radionica' ),
get_the_date( _x( 'F', 'monthly archives date format' ) ),
get_the_date( _x( 'Y', 'yearly archives date format' ) )
);
elseif ( is_day() ) :
$description = sprintf( esc_html__( 'Posts published on %1$s of %2$s, in %3$s year.', 'radionica' ),
get_the_date( _x( 'jS', 'daily archives date format' ) ),
get_the_date( _x( 'F', 'monthly archives date format' ) ),
get_the_date( _x( 'Y', 'yearly archives date format' ) )
);
endif;
return $description;
}
add_filter( 'get_the_archive_description', 'radionica_get_the_archive_description' );
Oops, something went wrong.

0 comments on commit 6e5382e

Please sign in to comment.