wp/wp-content/themes/twentythirteen/functions.php
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
equal deleted inserted replaced
4:346c88efed21 5:5e2f62d02dcd
     4  *
     4  *
     5  * Sets up the theme and provides some helper functions, which are used in the
     5  * Sets up the theme and provides some helper functions, which are used in the
     6  * theme as custom template tags. Others are attached to action and filter
     6  * theme as custom template tags. Others are attached to action and filter
     7  * hooks in WordPress to change core functionality.
     7  * hooks in WordPress to change core functionality.
     8  *
     8  *
     9  * When using a child theme (see http://codex.wordpress.org/Theme_Development
     9  * When using a child theme (see https://codex.wordpress.org/Theme_Development
    10  * and http://codex.wordpress.org/Child_Themes), you can override certain
    10  * and https://codex.wordpress.org/Child_Themes), you can override certain
    11  * functions (those wrapped in a function_exists() call) by defining them first
    11  * functions (those wrapped in a function_exists() call) by defining them first
    12  * in your child theme's functions.php file. The child theme's functions.php
    12  * in your child theme's functions.php file. The child theme's functions.php
    13  * file is included before the parent theme's file, so the child theme
    13  * file is included before the parent theme's file, so the child theme
    14  * functions would be used.
    14  * functions would be used.
    15  *
    15  *
    16  * Functions that are not pluggable (not wrapped in function_exists()) are
    16  * Functions that are not pluggable (not wrapped in function_exists()) are
    17  * instead attached to a filter or action hook.
    17  * instead attached to a filter or action hook.
    18  *
    18  *
    19  * For more information on hooks, actions, and filters, @link http://codex.wordpress.org/Plugin_API
    19  * For more information on hooks, actions, and filters, @link https://codex.wordpress.org/Plugin_API
    20  *
    20  *
    21  * @package WordPress
    21  * @package WordPress
    22  * @subpackage Twenty_Thirteen
    22  * @subpackage Twenty_Thirteen
    23  * @since Twenty Thirteen 1.0
    23  * @since Twenty Thirteen 1.0
    24  */
    24  */
    54  * formats, and post thumbnails.
    54  * formats, and post thumbnails.
    55  * @uses register_nav_menu() To add support for a navigation menu.
    55  * @uses register_nav_menu() To add support for a navigation menu.
    56  * @uses set_post_thumbnail_size() To set a custom post thumbnail size.
    56  * @uses set_post_thumbnail_size() To set a custom post thumbnail size.
    57  *
    57  *
    58  * @since Twenty Thirteen 1.0
    58  * @since Twenty Thirteen 1.0
    59  *
       
    60  * @return void
       
    61  */
    59  */
    62 function twentythirteen_setup() {
    60 function twentythirteen_setup() {
    63 	/*
    61 	/*
    64 	 * Makes Twenty Thirteen available for translation.
    62 	 * Makes Twenty Thirteen available for translation.
    65 	 *
    63 	 *
    72 
    70 
    73 	/*
    71 	/*
    74 	 * This theme styles the visual editor to resemble the theme style,
    72 	 * This theme styles the visual editor to resemble the theme style,
    75 	 * specifically font, colors, icons, and column width.
    73 	 * specifically font, colors, icons, and column width.
    76 	 */
    74 	 */
    77 	add_editor_style( array( 'css/editor-style.css', 'fonts/genericons.css', twentythirteen_fonts_url() ) );
    75 	add_editor_style( array( 'css/editor-style.css', 'genericons/genericons.css', twentythirteen_fonts_url() ) );
    78 
    76 
    79 	// Adds RSS feed links to <head> for posts and comments.
    77 	// Adds RSS feed links to <head> for posts and comments.
    80 	add_theme_support( 'automatic-feed-links' );
    78 	add_theme_support( 'automatic-feed-links' );
    81 
    79 
    82 	/*
    80 	/*
    83 	 * Switches default core markup for search form, comment form,
    81 	 * Switches default core markup for search form, comment form,
    84 	 * and comments to output valid HTML5.
    82 	 * and comments to output valid HTML5.
    85 	 */
    83 	 */
    86 	add_theme_support( 'html5', array( 'search-form', 'comment-form', 'comment-list' ) );
    84 	add_theme_support( 'html5', array(
       
    85 		'search-form', 'comment-form', 'comment-list', 'gallery', 'caption'
       
    86 	) );
    87 
    87 
    88 	/*
    88 	/*
    89 	 * This theme supports all available post formats by default.
    89 	 * This theme supports all available post formats by default.
    90 	 * See http://codex.wordpress.org/Post_Formats
    90 	 * See https://codex.wordpress.org/Post_Formats
    91 	 */
    91 	 */
    92 	add_theme_support( 'post-formats', array(
    92 	add_theme_support( 'post-formats', array(
    93 		'aside', 'audio', 'chat', 'gallery', 'image', 'link', 'quote', 'status', 'video'
    93 		'aside', 'audio', 'chat', 'gallery', 'image', 'link', 'quote', 'status', 'video'
    94 	) );
    94 	) );
    95 
    95 
   144 
   144 
   145 		$query_args = array(
   145 		$query_args = array(
   146 			'family' => urlencode( implode( '|', $font_families ) ),
   146 			'family' => urlencode( implode( '|', $font_families ) ),
   147 			'subset' => urlencode( 'latin,latin-ext' ),
   147 			'subset' => urlencode( 'latin,latin-ext' ),
   148 		);
   148 		);
   149 		$fonts_url = add_query_arg( $query_args, "//fonts.googleapis.com/css" );
   149 		$fonts_url = add_query_arg( $query_args, '//fonts.googleapis.com/css' );
   150 	}
   150 	}
   151 
   151 
   152 	return $fonts_url;
   152 	return $fonts_url;
   153 }
   153 }
   154 
   154 
   155 /**
   155 /**
   156  * Enqueue scripts and styles for the front end.
   156  * Enqueue scripts and styles for the front end.
   157  *
   157  *
   158  * @since Twenty Thirteen 1.0
   158  * @since Twenty Thirteen 1.0
   159  *
       
   160  * @return void
       
   161  */
   159  */
   162 function twentythirteen_scripts_styles() {
   160 function twentythirteen_scripts_styles() {
   163 	/*
   161 	/*
   164 	 * Adds JavaScript to pages with the comment form to support
   162 	 * Adds JavaScript to pages with the comment form to support
   165 	 * sites with threaded comments (when in use).
   163 	 * sites with threaded comments (when in use).
   170 	// Adds Masonry to handle vertical alignment of footer widgets.
   168 	// Adds Masonry to handle vertical alignment of footer widgets.
   171 	if ( is_active_sidebar( 'sidebar-1' ) )
   169 	if ( is_active_sidebar( 'sidebar-1' ) )
   172 		wp_enqueue_script( 'jquery-masonry' );
   170 		wp_enqueue_script( 'jquery-masonry' );
   173 
   171 
   174 	// Loads JavaScript file with functionality specific to Twenty Thirteen.
   172 	// Loads JavaScript file with functionality specific to Twenty Thirteen.
   175 	wp_enqueue_script( 'twentythirteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '2013-07-18', true );
   173 	wp_enqueue_script( 'twentythirteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20150330', true );
   176 
   174 
   177 	// Add Source Sans Pro and Bitter fonts, used in the main stylesheet.
   175 	// Add Source Sans Pro and Bitter fonts, used in the main stylesheet.
   178 	wp_enqueue_style( 'twentythirteen-fonts', twentythirteen_fonts_url(), array(), null );
   176 	wp_enqueue_style( 'twentythirteen-fonts', twentythirteen_fonts_url(), array(), null );
   179 
   177 
   180 	// Add Genericons font, used in the main stylesheet.
   178 	// Add Genericons font, used in the main stylesheet.
   181 	wp_enqueue_style( 'genericons', get_template_directory_uri() . '/fonts/genericons.css', array(), '2.09' );
   179 	wp_enqueue_style( 'genericons', get_template_directory_uri() . '/genericons/genericons.css', array(), '3.03' );
   182 
   180 
   183 	// Loads our main stylesheet.
   181 	// Loads our main stylesheet.
   184 	wp_enqueue_style( 'twentythirteen-style', get_stylesheet_uri(), array(), '2013-07-18' );
   182 	wp_enqueue_style( 'twentythirteen-style', get_stylesheet_uri(), array(), '2013-07-18' );
   185 
   183 
   186 	// Loads the Internet Explorer specific stylesheet.
   184 	// Loads the Internet Explorer specific stylesheet.
   206 
   204 
   207 	if ( is_feed() )
   205 	if ( is_feed() )
   208 		return $title;
   206 		return $title;
   209 
   207 
   210 	// Add the site name.
   208 	// Add the site name.
   211 	$title .= get_bloginfo( 'name' );
   209 	$title .= get_bloginfo( 'name', 'display' );
   212 
   210 
   213 	// Add the site description for the home/front page.
   211 	// Add the site description for the home/front page.
   214 	$site_description = get_bloginfo( 'description', 'display' );
   212 	$site_description = get_bloginfo( 'description', 'display' );
   215 	if ( $site_description && ( is_home() || is_front_page() ) )
   213 	if ( $site_description && ( is_home() || is_front_page() ) )
   216 		$title = "$title $sep $site_description";
   214 		$title = "$title $sep $site_description";
   217 
   215 
   218 	// Add a page number if necessary.
   216 	// Add a page number if necessary.
   219 	if ( $paged >= 2 || $page >= 2 )
   217 	if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() )
   220 		$title = "$title $sep " . sprintf( __( 'Page %s', 'twentythirteen' ), max( $paged, $page ) );
   218 		$title = "$title $sep " . sprintf( __( 'Page %s', 'twentythirteen' ), max( $paged, $page ) );
   221 
   219 
   222 	return $title;
   220 	return $title;
   223 }
   221 }
   224 add_filter( 'wp_title', 'twentythirteen_wp_title', 10, 2 );
   222 add_filter( 'wp_title', 'twentythirteen_wp_title', 10, 2 );
   225 
   223 
   226 /**
   224 /**
   227  * Register two widget areas.
   225  * Register two widget areas.
   228  *
   226  *
   229  * @since Twenty Thirteen 1.0
   227  * @since Twenty Thirteen 1.0
   230  *
       
   231  * @return void
       
   232  */
   228  */
   233 function twentythirteen_widgets_init() {
   229 function twentythirteen_widgets_init() {
   234 	register_sidebar( array(
   230 	register_sidebar( array(
   235 		'name'          => __( 'Main Widget Area', 'twentythirteen' ),
   231 		'name'          => __( 'Main Widget Area', 'twentythirteen' ),
   236 		'id'            => 'sidebar-1',
   232 		'id'            => 'sidebar-1',
   256 if ( ! function_exists( 'twentythirteen_paging_nav' ) ) :
   252 if ( ! function_exists( 'twentythirteen_paging_nav' ) ) :
   257 /**
   253 /**
   258  * Display navigation to next/previous set of posts when applicable.
   254  * Display navigation to next/previous set of posts when applicable.
   259  *
   255  *
   260  * @since Twenty Thirteen 1.0
   256  * @since Twenty Thirteen 1.0
   261  *
       
   262  * @return void
       
   263  */
   257  */
   264 function twentythirteen_paging_nav() {
   258 function twentythirteen_paging_nav() {
   265 	global $wp_query;
   259 	global $wp_query;
   266 
   260 
   267 	// Don't print empty markup if there's only one page.
   261 	// Don't print empty markup if there's only one page.
   289 if ( ! function_exists( 'twentythirteen_post_nav' ) ) :
   283 if ( ! function_exists( 'twentythirteen_post_nav' ) ) :
   290 /**
   284 /**
   291  * Display navigation to next/previous post when applicable.
   285  * Display navigation to next/previous post when applicable.
   292 *
   286 *
   293 * @since Twenty Thirteen 1.0
   287 * @since Twenty Thirteen 1.0
   294 *
       
   295 * @return void
       
   296 */
   288 */
   297 function twentythirteen_post_nav() {
   289 function twentythirteen_post_nav() {
   298 	global $post;
   290 	global $post;
   299 
   291 
   300 	// Don't print empty markup if there's nowhere to navigate.
   292 	// Don't print empty markup if there's nowhere to navigate.
   322  * Print HTML with meta information for current post: categories, tags, permalink, author, and date.
   314  * Print HTML with meta information for current post: categories, tags, permalink, author, and date.
   323  *
   315  *
   324  * Create your own twentythirteen_entry_meta() to override in a child theme.
   316  * Create your own twentythirteen_entry_meta() to override in a child theme.
   325  *
   317  *
   326  * @since Twenty Thirteen 1.0
   318  * @since Twenty Thirteen 1.0
   327  *
       
   328  * @return void
       
   329  */
   319  */
   330 function twentythirteen_entry_meta() {
   320 function twentythirteen_entry_meta() {
   331 	if ( is_sticky() && is_home() && ! is_paged() )
   321 	if ( is_sticky() && is_home() && ! is_paged() )
   332 		echo '<span class="featured-post">' . __( 'Sticky', 'twentythirteen' ) . '</span>';
   322 		echo '<span class="featured-post">' . esc_html__( 'Sticky', 'twentythirteen' ) . '</span>';
   333 
   323 
   334 	if ( ! has_post_format( 'link' ) && 'post' == get_post_type() )
   324 	if ( ! has_post_format( 'link' ) && 'post' == get_post_type() )
   335 		twentythirteen_entry_date();
   325 		twentythirteen_entry_date();
   336 
   326 
   337 	// Translators: used between list items, there is a space after the comma.
   327 	// Translators: used between list items, there is a space after the comma.
   391 if ( ! function_exists( 'twentythirteen_the_attached_image' ) ) :
   381 if ( ! function_exists( 'twentythirteen_the_attached_image' ) ) :
   392 /**
   382 /**
   393  * Print the attached image with a link to the next attached image.
   383  * Print the attached image with a link to the next attached image.
   394  *
   384  *
   395  * @since Twenty Thirteen 1.0
   385  * @since Twenty Thirteen 1.0
   396  *
       
   397  * @return void
       
   398  */
   386  */
   399 function twentythirteen_the_attached_image() {
   387 function twentythirteen_the_attached_image() {
   400 	/**
   388 	/**
   401 	 * Filter the image attachment size to use.
   389 	 * Filter the image attachment size to use.
   402 	 *
   390 	 *
   423 		'numberposts'    => -1,
   411 		'numberposts'    => -1,
   424 		'post_status'    => 'inherit',
   412 		'post_status'    => 'inherit',
   425 		'post_type'      => 'attachment',
   413 		'post_type'      => 'attachment',
   426 		'post_mime_type' => 'image',
   414 		'post_mime_type' => 'image',
   427 		'order'          => 'ASC',
   415 		'order'          => 'ASC',
   428 		'orderby'        => 'menu_order ID'
   416 		'orderby'        => 'menu_order ID',
   429 	) );
   417 	) );
   430 
   418 
   431 	// If there is more than 1 attachment in a gallery...
   419 	// If there is more than 1 attachment in a gallery...
   432 	if ( count( $attachment_ids ) > 1 ) {
   420 	if ( count( $attachment_ids ) > 1 ) {
   433 		foreach ( $attachment_ids as $attachment_id ) {
   421 		foreach ( $attachment_ids as $attachment_id ) {
   441 		if ( $next_id )
   429 		if ( $next_id )
   442 			$next_attachment_url = get_attachment_link( $next_id );
   430 			$next_attachment_url = get_attachment_link( $next_id );
   443 
   431 
   444 		// or get the URL of the first image attachment.
   432 		// or get the URL of the first image attachment.
   445 		else
   433 		else
   446 			$next_attachment_url = get_attachment_link( array_shift( $attachment_ids ) );
   434 			$next_attachment_url = get_attachment_link( reset( $attachment_ids ) );
   447 	}
   435 	}
   448 
   436 
   449 	printf( '<a href="%1$s" title="%2$s" rel="attachment">%3$s</a>',
   437 	printf( '<a href="%1$s" title="%2$s" rel="attachment">%3$s</a>',
   450 		esc_url( $next_attachment_url ),
   438 		esc_url( $next_attachment_url ),
   451 		the_title_attribute( array( 'echo' => false ) ),
   439 		the_title_attribute( array( 'echo' => false ) ),
   471 	$has_url = get_url_in_content( $content );
   459 	$has_url = get_url_in_content( $content );
   472 
   460 
   473 	return ( $has_url ) ? $has_url : apply_filters( 'the_permalink', get_permalink() );
   461 	return ( $has_url ) ? $has_url : apply_filters( 'the_permalink', get_permalink() );
   474 }
   462 }
   475 
   463 
       
   464 if ( ! function_exists( 'twentythirteen_excerpt_more' ) && ! is_admin() ) :
       
   465 /**
       
   466  * Replaces "[...]" (appended to automatically generated excerpts) with ...
       
   467  * and a Continue reading link.
       
   468  *
       
   469  * @since Twenty Thirteen 1.4
       
   470  *
       
   471  * @param string $more Default Read More excerpt link.
       
   472  * @return string Filtered Read More excerpt link.
       
   473  */
       
   474 function twentythirteen_excerpt_more( $more ) {
       
   475 	$link = sprintf( '<a href="%1$s" class="more-link">%2$s</a>',
       
   476 		esc_url( get_permalink( get_the_ID() ) ),
       
   477 			/* translators: %s: Name of current post */
       
   478 			sprintf( __( 'Continue reading %s <span class="meta-nav">&rarr;</span>', 'twentythirteen' ), '<span class="screen-reader-text">' . get_the_title( get_the_ID() ) . '</span>' )
       
   479 		);
       
   480 	return ' &hellip; ' . $link;
       
   481 }
       
   482 add_filter( 'excerpt_more', 'twentythirteen_excerpt_more' );
       
   483 endif;
       
   484 
   476 /**
   485 /**
   477  * Extend the default WordPress body classes.
   486  * Extend the default WordPress body classes.
   478  *
   487  *
   479  * Adds body classes to denote:
   488  * Adds body classes to denote:
   480  * 1. Single or multiple authors.
   489  * 1. Single or multiple authors.
   502 
   511 
   503 /**
   512 /**
   504  * Adjust content_width value for video post formats and attachment templates.
   513  * Adjust content_width value for video post formats and attachment templates.
   505  *
   514  *
   506  * @since Twenty Thirteen 1.0
   515  * @since Twenty Thirteen 1.0
   507  *
       
   508  * @return void
       
   509  */
   516  */
   510 function twentythirteen_content_width() {
   517 function twentythirteen_content_width() {
   511 	global $content_width;
   518 	global $content_width;
   512 
   519 
   513 	if ( is_attachment() )
   520 	if ( is_attachment() )
   521  * Add postMessage support for site title and description for the Customizer.
   528  * Add postMessage support for site title and description for the Customizer.
   522  *
   529  *
   523  * @since Twenty Thirteen 1.0
   530  * @since Twenty Thirteen 1.0
   524  *
   531  *
   525  * @param WP_Customize_Manager $wp_customize Customizer object.
   532  * @param WP_Customize_Manager $wp_customize Customizer object.
   526  * @return void
       
   527  */
   533  */
   528 function twentythirteen_customize_register( $wp_customize ) {
   534 function twentythirteen_customize_register( $wp_customize ) {
   529 	$wp_customize->get_setting( 'blogname' )->transport         = 'postMessage';
   535 	$wp_customize->get_setting( 'blogname' )->transport         = 'postMessage';
   530 	$wp_customize->get_setting( 'blogdescription' )->transport  = 'postMessage';
   536 	$wp_customize->get_setting( 'blogdescription' )->transport  = 'postMessage';
   531 	$wp_customize->get_setting( 'header_textcolor' )->transport = 'postMessage';
   537 	$wp_customize->get_setting( 'header_textcolor' )->transport = 'postMessage';
   537  *
   543  *
   538  * Binds JavaScript handlers to make the Customizer preview
   544  * Binds JavaScript handlers to make the Customizer preview
   539  * reload changes asynchronously.
   545  * reload changes asynchronously.
   540  *
   546  *
   541  * @since Twenty Thirteen 1.0
   547  * @since Twenty Thirteen 1.0
   542  *
       
   543  * @return void
       
   544  */
   548  */
   545 function twentythirteen_customize_preview_js() {
   549 function twentythirteen_customize_preview_js() {
   546 	wp_enqueue_script( 'twentythirteen-customizer', get_template_directory_uri() . '/js/theme-customizer.js', array( 'customize-preview' ), '20130226', true );
   550 	wp_enqueue_script( 'twentythirteen-customizer', get_template_directory_uri() . '/js/theme-customizer.js', array( 'customize-preview' ), '20141120', true );
   547 }
   551 }
   548 add_action( 'customize_preview_init', 'twentythirteen_customize_preview_js' );
   552 add_action( 'customize_preview_init', 'twentythirteen_customize_preview_js' );