~canonical-sysadmins/wordpress/4.7.4

« back to all changes in this revision

Viewing changes to wp-content/themes/twentyfourteen/inc/template-tags.php

  • Committer: Jacek Nykis
  • Date: 2015-01-05 16:17:05 UTC
  • Revision ID: jacek.nykis@canonical.com-20150105161705-w544l1h5mcg7u4w9
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Custom template tags for Twenty Fourteen
 
4
 *
 
5
 * @package WordPress
 
6
 * @subpackage Twenty_Fourteen
 
7
 * @since Twenty Fourteen 1.0
 
8
 */
 
9
 
 
10
if ( ! function_exists( 'twentyfourteen_paging_nav' ) ) :
 
11
/**
 
12
 * Display navigation to next/previous set of posts when applicable.
 
13
 *
 
14
 * @since Twenty Fourteen 1.0
 
15
 *
 
16
 * @global WP_Query   $wp_query   WordPress Query object.
 
17
 * @global WP_Rewrite $wp_rewrite WordPress Rewrite object.
 
18
 */
 
19
function twentyfourteen_paging_nav() {
 
20
        global $wp_query, $wp_rewrite;
 
21
 
 
22
        // Don't print empty markup if there's only one page.
 
23
        if ( $wp_query->max_num_pages < 2 ) {
 
24
                return;
 
25
        }
 
26
 
 
27
        $paged        = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1;
 
28
        $pagenum_link = html_entity_decode( get_pagenum_link() );
 
29
        $query_args   = array();
 
30
        $url_parts    = explode( '?', $pagenum_link );
 
31
 
 
32
        if ( isset( $url_parts[1] ) ) {
 
33
                wp_parse_str( $url_parts[1], $query_args );
 
34
        }
 
35
 
 
36
        $pagenum_link = remove_query_arg( array_keys( $query_args ), $pagenum_link );
 
37
        $pagenum_link = trailingslashit( $pagenum_link ) . '%_%';
 
38
 
 
39
        $format  = $wp_rewrite->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : '';
 
40
        $format .= $wp_rewrite->using_permalinks() ? user_trailingslashit( $wp_rewrite->pagination_base . '/%#%', 'paged' ) : '?paged=%#%';
 
41
 
 
42
        // Set up paginated links.
 
43
        $links = paginate_links( array(
 
44
                'base'     => $pagenum_link,
 
45
                'format'   => $format,
 
46
                'total'    => $wp_query->max_num_pages,
 
47
                'current'  => $paged,
 
48
                'mid_size' => 1,
 
49
                'add_args' => array_map( 'urlencode', $query_args ),
 
50
                'prev_text' => __( '&larr; Previous', 'twentyfourteen' ),
 
51
                'next_text' => __( 'Next &rarr;', 'twentyfourteen' ),
 
52
        ) );
 
53
 
 
54
        if ( $links ) :
 
55
 
 
56
        ?>
 
57
        <nav class="navigation paging-navigation" role="navigation">
 
58
                <h1 class="screen-reader-text"><?php _e( 'Posts navigation', 'twentyfourteen' ); ?></h1>
 
59
                <div class="pagination loop-pagination">
 
60
                        <?php echo $links; ?>
 
61
                </div><!-- .pagination -->
 
62
        </nav><!-- .navigation -->
 
63
        <?php
 
64
        endif;
 
65
}
 
66
endif;
 
67
 
 
68
if ( ! function_exists( 'twentyfourteen_post_nav' ) ) :
 
69
/**
 
70
 * Display navigation to next/previous post when applicable.
 
71
 *
 
72
 * @since Twenty Fourteen 1.0
 
73
 */
 
74
function twentyfourteen_post_nav() {
 
75
        // Don't print empty markup if there's nowhere to navigate.
 
76
        $previous = ( is_attachment() ) ? get_post( get_post()->post_parent ) : get_adjacent_post( false, '', true );
 
77
        $next     = get_adjacent_post( false, '', false );
 
78
 
 
79
        if ( ! $next && ! $previous ) {
 
80
                return;
 
81
        }
 
82
 
 
83
        ?>
 
84
        <nav class="navigation post-navigation" role="navigation">
 
85
                <h1 class="screen-reader-text"><?php _e( 'Post navigation', 'twentyfourteen' ); ?></h1>
 
86
                <div class="nav-links">
 
87
                        <?php
 
88
                        if ( is_attachment() ) :
 
89
                                previous_post_link( '%link', __( '<span class="meta-nav">Published In</span>%title', 'twentyfourteen' ) );
 
90
                        else :
 
91
                                previous_post_link( '%link', __( '<span class="meta-nav">Previous Post</span>%title', 'twentyfourteen' ) );
 
92
                                next_post_link( '%link', __( '<span class="meta-nav">Next Post</span>%title', 'twentyfourteen' ) );
 
93
                        endif;
 
94
                        ?>
 
95
                </div><!-- .nav-links -->
 
96
        </nav><!-- .navigation -->
 
97
        <?php
 
98
}
 
99
endif;
 
100
 
 
101
if ( ! function_exists( 'twentyfourteen_posted_on' ) ) :
 
102
/**
 
103
 * Print HTML with meta information for the current post-date/time and author.
 
104
 *
 
105
 * @since Twenty Fourteen 1.0
 
106
 */
 
107
function twentyfourteen_posted_on() {
 
108
        if ( is_sticky() && is_home() && ! is_paged() ) {
 
109
                echo '<span class="featured-post">' . __( 'Sticky', 'twentyfourteen' ) . '</span>';
 
110
        }
 
111
 
 
112
        // Set up and print post meta information.
 
113
        printf( '<span class="entry-date"><a href="%1$s" rel="bookmark"><time class="entry-date" datetime="%2$s">%3$s</time></a></span> <span class="byline"><span class="author vcard"><a class="url fn n" href="%4$s" rel="author">%5$s</a></span></span>',
 
114
                esc_url( get_permalink() ),
 
115
                esc_attr( get_the_date( 'c' ) ),
 
116
                esc_html( get_the_date() ),
 
117
                esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
 
118
                get_the_author()
 
119
        );
 
120
}
 
121
endif;
 
122
 
 
123
/**
 
124
 * Find out if blog has more than one category.
 
125
 *
 
126
 * @since Twenty Fourteen 1.0
 
127
 *
 
128
 * @return boolean true if blog has more than 1 category
 
129
 */
 
130
function twentyfourteen_categorized_blog() {
 
131
        if ( false === ( $all_the_cool_cats = get_transient( 'twentyfourteen_category_count' ) ) ) {
 
132
                // Create an array of all the categories that are attached to posts
 
133
                $all_the_cool_cats = get_categories( array(
 
134
                        'hide_empty' => 1,
 
135
                ) );
 
136
 
 
137
                // Count the number of categories that are attached to the posts
 
138
                $all_the_cool_cats = count( $all_the_cool_cats );
 
139
 
 
140
                set_transient( 'twentyfourteen_category_count', $all_the_cool_cats );
 
141
        }
 
142
 
 
143
        if ( 1 !== (int) $all_the_cool_cats ) {
 
144
                // This blog has more than 1 category so twentyfourteen_categorized_blog should return true
 
145
                return true;
 
146
        } else {
 
147
                // This blog has only 1 category so twentyfourteen_categorized_blog should return false
 
148
                return false;
 
149
        }
 
150
}
 
151
 
 
152
/**
 
153
 * Flush out the transients used in twentyfourteen_categorized_blog.
 
154
 *
 
155
 * @since Twenty Fourteen 1.0
 
156
 */
 
157
function twentyfourteen_category_transient_flusher() {
 
158
        // Like, beat it. Dig?
 
159
        delete_transient( 'twentyfourteen_category_count' );
 
160
}
 
161
add_action( 'edit_category', 'twentyfourteen_category_transient_flusher' );
 
162
add_action( 'save_post',     'twentyfourteen_category_transient_flusher' );
 
163
 
 
164
/**
 
165
 * Display an optional post thumbnail.
 
166
 *
 
167
 * Wraps the post thumbnail in an anchor element on index
 
168
 * views, or a div element when on single views.
 
169
 *
 
170
 * @since Twenty Fourteen 1.0
 
171
 */
 
172
function twentyfourteen_post_thumbnail() {
 
173
        if ( post_password_required() || is_attachment() || ! has_post_thumbnail() ) {
 
174
                return;
 
175
        }
 
176
 
 
177
        if ( is_singular() ) :
 
178
        ?>
 
179
 
 
180
        <div class="post-thumbnail">
 
181
        <?php
 
182
                if ( ( ! is_active_sidebar( 'sidebar-2' ) || is_page_template( 'page-templates/full-width.php' ) ) ) {
 
183
                        the_post_thumbnail( 'twentyfourteen-full-width' );
 
184
                } else {
 
185
                        the_post_thumbnail();
 
186
                }
 
187
        ?>
 
188
        </div>
 
189
 
 
190
        <?php else : ?>
 
191
 
 
192
        <a class="post-thumbnail" href="<?php the_permalink(); ?>">
 
193
        <?php
 
194
                if ( ( ! is_active_sidebar( 'sidebar-2' ) || is_page_template( 'page-templates/full-width.php' ) ) ) {
 
195
                        the_post_thumbnail( 'twentyfourteen-full-width' );
 
196
                } else {
 
197
                        the_post_thumbnail();
 
198
                }
 
199
        ?>
 
200
        </a>
 
201
 
 
202
        <?php endif; // End is_singular()
 
203
}