[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-content/themes/twentyfifteen/inc/ -> template-tags.php (source)

   1  <?php
   2  /**
   3   * Custom template tags for Twenty Fifteen
   4   *
   5   * Eventually, some of the functionality here could be replaced by core features.
   6   *
   7   * @package WordPress
   8   * @subpackage Twenty_Fifteen
   9   * @since Twenty Fifteen 1.0
  10   */
  11  
  12  if ( ! function_exists( 'twentyfifteen_comment_nav' ) ) :
  13      /**
  14       * Display navigation to next/previous comments when applicable.
  15       *
  16       * @since Twenty Fifteen 1.0
  17       */
  18  	function twentyfifteen_comment_nav() {
  19          // Are there comments to navigate through?
  20          if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) :
  21              ?>
  22          <nav class="navigation comment-navigation">
  23          <h2 class="screen-reader-text">
  24              <?php
  25              /* translators: Hidden accessibility text. */
  26              _e( 'Comment navigation', 'twentyfifteen' );
  27              ?>
  28          </h2>
  29          <div class="nav-links">
  30              <?php
  31              $prev_link = get_previous_comments_link( __( 'Older Comments', 'twentyfifteen' ) );
  32              if ( $prev_link ) {
  33                  printf( '<div class="nav-previous">%s</div>', $prev_link );
  34              }
  35  
  36              $next_link = get_next_comments_link( __( 'Newer Comments', 'twentyfifteen' ) );
  37              if ( $next_link ) {
  38                  printf( '<div class="nav-next">%s</div>', $next_link );
  39              }
  40              ?>
  41              </div><!-- .nav-links -->
  42          </nav><!-- .comment-navigation -->
  43              <?php
  44          endif;
  45      }
  46  endif;
  47  
  48  if ( ! function_exists( 'twentyfifteen_entry_meta' ) ) :
  49      /**
  50       * Prints HTML with meta information for the categories, tags.
  51       *
  52       * @since Twenty Fifteen 1.0
  53       */
  54  	function twentyfifteen_entry_meta() {
  55          if ( is_sticky() && is_home() && ! is_paged() ) {
  56              printf( '<span class="sticky-post">%s</span>', __( 'Featured', 'twentyfifteen' ) );
  57          }
  58  
  59          $format = get_post_format();
  60          if ( current_theme_supports( 'post-formats', $format ) ) {
  61              printf(
  62                  '<span class="entry-format">%1$s<a href="%2$s">%3$s</a></span>',
  63                  sprintf(
  64                      '<span class="screen-reader-text">%s </span>',
  65                      /* translators: Hidden accessibility text. */
  66                      _x( 'Format', 'Used before post format.', 'twentyfifteen' )
  67                  ),
  68                  esc_url( get_post_format_link( $format ) ),
  69                  get_post_format_string( $format )
  70              );
  71          }
  72  
  73          if ( in_array( get_post_type(), array( 'post', 'attachment' ), true ) ) {
  74              $time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
  75  
  76              if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
  77                  $time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%4$s</time>';
  78              }
  79  
  80              $time_string = sprintf(
  81                  $time_string,
  82                  esc_attr( get_the_date( 'c' ) ),
  83                  get_the_date(),
  84                  esc_attr( get_the_modified_date( 'c' ) ),
  85                  get_the_modified_date()
  86              );
  87  
  88              printf(
  89                  '<span class="posted-on"><span class="screen-reader-text">%1$s </span><a href="%2$s" rel="bookmark">%3$s</a></span>',
  90                  /* translators: Hidden accessibility text. */
  91                  _x( 'Posted on', 'Used before publish date.', 'twentyfifteen' ),
  92                  esc_url( get_permalink() ),
  93                  $time_string
  94              );
  95          }
  96  
  97          if ( 'post' === get_post_type() ) {
  98              if ( is_singular() || is_multi_author() ) {
  99                  printf(
 100                      '<span class="byline"><span class="author vcard"><span class="screen-reader-text">%1$s </span><a class="url fn n" href="%2$s">%3$s</a></span></span>',
 101                      /* translators: Hidden accessibility text. */
 102                      _x( 'Author', 'Used before post author name.', 'twentyfifteen' ),
 103                      esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
 104                      get_the_author()
 105                  );
 106              }
 107  
 108              $categories_list = get_the_category_list( _x( ', ', 'Used between list items, there is a space after the comma.', 'twentyfifteen' ) );
 109              if ( $categories_list && twentyfifteen_categorized_blog() ) {
 110                  printf(
 111                      '<span class="cat-links"><span class="screen-reader-text">%1$s </span>%2$s</span>',
 112                      /* translators: Hidden accessibility text. */
 113                      _x( 'Categories', 'Used before category names.', 'twentyfifteen' ),
 114                      $categories_list
 115                  );
 116              }
 117  
 118              $tags_list = get_the_tag_list( '', _x( ', ', 'Used between list items, there is a space after the comma.', 'twentyfifteen' ) );
 119              if ( $tags_list && ! is_wp_error( $tags_list ) ) {
 120                  printf(
 121                      '<span class="tags-links"><span class="screen-reader-text">%1$s </span>%2$s</span>',
 122                      /* translators: Hidden accessibility text. */
 123                      _x( 'Tags', 'Used before tag names.', 'twentyfifteen' ),
 124                      $tags_list
 125                  );
 126              }
 127          }
 128  
 129          if ( is_attachment() && wp_attachment_is_image() ) {
 130              // Retrieve attachment metadata.
 131              $metadata = wp_get_attachment_metadata();
 132  
 133              printf(
 134                  '<span class="full-size-link"><span class="screen-reader-text">%1$s </span><a href="%2$s">%3$s &times; %4$s</a></span>',
 135                  /* translators: Hidden accessibility text. */
 136                  _x( 'Full size', 'Used before full size attachment link.', 'twentyfifteen' ),
 137                  esc_url( wp_get_attachment_url() ),
 138                  $metadata['width'],
 139                  $metadata['height']
 140              );
 141          }
 142  
 143          if ( ! is_single() && ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
 144              echo '<span class="comments-link">';
 145              /* translators: %s: Post title. Only visible to screen readers. */
 146              comments_popup_link( sprintf( __( 'Leave a comment<span class="screen-reader-text"> on %s</span>', 'twentyfifteen' ), get_the_title() ) );
 147              echo '</span>';
 148          }
 149      }
 150  endif;
 151  
 152  /**
 153   * Determine whether blog/site has more than one category.
 154   *
 155   * @since Twenty Fifteen 1.0
 156   *
 157   * @return bool True of there is more than one category, false otherwise.
 158   */
 159  function twentyfifteen_categorized_blog() {
 160      $all_the_cool_cats = get_transient( 'twentyfifteen_categories' );
 161      if ( false === $all_the_cool_cats ) {
 162          // Create an array of all the categories that are attached to posts.
 163          $all_the_cool_cats = get_categories(
 164              array(
 165                  'fields'     => 'ids',
 166                  'hide_empty' => 1,
 167  
 168                  // We only need to know if there is more than one category.
 169                  'number'     => 2,
 170              )
 171          );
 172  
 173          // Count the number of categories that are attached to the posts.
 174          $all_the_cool_cats = count( $all_the_cool_cats );
 175  
 176          set_transient( 'twentyfifteen_categories', $all_the_cool_cats );
 177      }
 178  
 179      if ( $all_the_cool_cats > 1 || is_preview() ) {
 180          // This blog has more than 1 category so twentyfifteen_categorized_blog() should return true.
 181          return true;
 182      } else {
 183          // This blog has only 1 category so twentyfifteen_categorized_blog() should return false.
 184          return false;
 185      }
 186  }
 187  
 188  /**
 189   * Flush out the transients used in {@see twentyfifteen_categorized_blog()}.
 190   *
 191   * @since Twenty Fifteen 1.0
 192   */
 193  function twentyfifteen_category_transient_flusher() {
 194      // Like, beat it. Dig?
 195      delete_transient( 'twentyfifteen_categories' );
 196  }
 197  add_action( 'edit_category', 'twentyfifteen_category_transient_flusher' );
 198  add_action( 'save_post', 'twentyfifteen_category_transient_flusher' );
 199  
 200  if ( ! function_exists( 'twentyfifteen_post_thumbnail' ) ) :
 201      /**
 202       * Display an optional post thumbnail.
 203       *
 204       * Wraps the post thumbnail in an anchor element on index views, or a div
 205       * element when on single views.
 206       *
 207       * @since Twenty Fifteen 1.0
 208       */
 209  	function twentyfifteen_post_thumbnail() {
 210          if ( post_password_required() || is_attachment() || ! has_post_thumbnail() ) {
 211              return;
 212          }
 213  
 214          if ( is_singular() ) :
 215              ?>
 216  
 217          <div class="post-thumbnail">
 218              <?php the_post_thumbnail(); ?>
 219      </div><!-- .post-thumbnail -->
 220  
 221      <?php else : ?>
 222  
 223      <a class="post-thumbnail" href="<?php the_permalink(); ?>" aria-hidden="true">
 224          <?php
 225              the_post_thumbnail( 'post-thumbnail', array( 'alt' => get_the_title() ) );
 226          ?>
 227      </a>
 228  
 229          <?php
 230      endif; // End is_singular().
 231      }
 232  endif;
 233  
 234  if ( ! function_exists( 'twentyfifteen_get_link_url' ) ) :
 235      /**
 236       * Return the post URL.
 237       *
 238       * Falls back to the post permalink if no URL is found in the post.
 239       *
 240       * @since Twenty Fifteen 1.0
 241       *
 242       * @see get_url_in_content()
 243       *
 244       * @return string The Link format URL.
 245       */
 246  	function twentyfifteen_get_link_url() {
 247          $has_url = get_url_in_content( get_the_content() );
 248  
 249          return $has_url ? $has_url : apply_filters( 'the_permalink', get_permalink() );
 250      }
 251  endif;
 252  
 253  if ( ! function_exists( 'twentyfifteen_excerpt_more' ) && ! is_admin() ) :
 254      /**
 255       * Replaces "[...]" (appended to automatically generated excerpts) with ... and a 'Continue reading' link.
 256       *
 257       * @since Twenty Fifteen 1.0
 258       *
 259       * @return string 'Continue reading' link prepended with an ellipsis.
 260       */
 261  	function twentyfifteen_excerpt_more( $more ) {
 262          $link = sprintf(
 263              '<a href="%1$s" class="more-link">%2$s</a>',
 264              esc_url( get_permalink( get_the_ID() ) ),
 265              /* translators: %s: Post title. Only visible to screen readers. */
 266              sprintf( __( 'Continue reading %s', 'twentyfifteen' ), '<span class="screen-reader-text">' . get_the_title( get_the_ID() ) . '</span>' )
 267          );
 268          return ' &hellip; ' . $link;
 269      }
 270      add_filter( 'excerpt_more', 'twentyfifteen_excerpt_more' );
 271  endif;
 272  
 273  if ( ! function_exists( 'twentyfifteen_the_custom_logo' ) ) :
 274      /**
 275       * Displays the optional custom logo.
 276       *
 277       * Does nothing if the custom logo is not available.
 278       *
 279       * @since Twenty Fifteen 1.5
 280       */
 281  	function twentyfifteen_the_custom_logo() {
 282          if ( function_exists( 'the_custom_logo' ) ) {
 283              the_custom_logo();
 284          }
 285      }
 286  endif;
 287  
 288  if ( ! function_exists( 'wp_body_open' ) ) :
 289      /**
 290       * Fire the wp_body_open action.
 291       *
 292       * Added for backward compatibility to support pre-5.2.0 WordPress versions.
 293       *
 294       * @since Twenty Fifteen 2.5
 295       */
 296  	function wp_body_open() {
 297          /**
 298           * Triggered after the opening <body> tag.
 299           *
 300           * @since Twenty Fifteen 2.5
 301           */
 302          do_action( 'wp_body_open' );
 303      }
 304  endif;


Generated : Tue Mar 19 08:20:01 2024 Cross-referenced by PHPXref