WordPressを読む 39-3 /blog/wp-includes/post-template.php 3
2015/01/07
目次
/blog/wp-includes/post-template.php 3
関数 wp_list_pages()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | /** * Retrieve or display list of pages in list (li) format. * * @since 1.5.0 * * @see get_pages() * * @param array|string $args { * Array or string of arguments. Optional. * * @type int $child_of Display only the sub-pages of a single page by ID. Default 0 (all pages). * @type string $authors Comma-separated list of author IDs. Default empty (all authors). * @type string $date_format PHP date format to use for the listed pages. Relies on the 'show_date' parameter. * Default is the value of 'date_format' option. * @type int $depth Number of levels in the hierarchy of pages to include in the generated list. * Accepts -1 (any depth), 0 (all pages), 1 (top-level pages only), and n (pages to * the given n depth). Default 0. * @type bool $echo Whether or not to echo the list of pages. Default true. * @type string $exclude Comma-separated list of page IDs to exclude. Default empty. * @type array $include Comma-separated list of page IDs to include. Default empty. * @type string $link_after Text or HTML to follow the page link label. Default null. * @type string $link_before Text or HTML to precede the page link label. Default null. * @type string $post_type Post type to query for. Default 'page'. * @type string $post_status Comma-separated list of post statuses to include. Default 'publish'. * @type string $show_date Whether to display the page publish or modified date for each page. Accepts * 'modified' or any other value. An empty value hides the date. Default empty. * @type string $sort_column Comma-separated list of column names to sort the pages by. Accepts 'post_author', * 'post_date', 'post_title', 'post_name', 'post_modified', 'post_modified_gmt', * 'menu_order', 'post_parent', 'ID', 'rand', or 'comment_count'. Default 'post_title'. * @type string $title_li List heading. Passing a null or empty value will result in no heading, and the list * will not be wrapped with unordered list `<ul>` tags. Default 'Pages'. * @type Walker $walker Walker instance to use for listing pages. Default empty (Walker_Page). * } * @return string HTML list of pages. */ function wp_list_pages( $args = '' ) { $defaults = array( 'depth' => 0, 'show_date' => '', 'date_format' => get_option( 'date_format' ), 'child_of' => 0, 'exclude' => '', 'title_li' => __( 'Pages' ), 'echo' => 1, 'authors' => '', 'sort_column' => 'menu_order, post_title', 'link_before' => '', 'link_after' => '', 'walker' => '', ); $r = wp_parse_args( $args, $defaults ); $output = ''; $current_page = 0; // sanitize, mostly to keep spaces out $r['exclude'] = preg_replace( '/[^0-9,]/', '', $r['exclude'] ); // Allow plugins to filter an array of excluded pages (but don't put a nullstring into the array) $exclude_array = ( $r['exclude'] ) ? explode( ',', $r['exclude'] ) : array(); /** * Filter the array of pages to exclude from the pages list. * * @since 2.1.0 * * @param array $exclude_array An array of page IDs to exclude. */ $r['exclude'] = implode( ',', apply_filters( 'wp_list_pages_excludes', $exclude_array ) ); // Query pages. $r['hierarchical'] = 0; $pages = get_pages( $r ); if ( ! empty( $pages ) ) { if ( $r['title_li'] ) { $output .= '<li class="pagenav">' . $r['title_li'] . '<ul>'; } global $wp_query; if ( is_page() || is_attachment() || $wp_query->is_posts_page ) { $current_page = get_queried_object_id(); } elseif ( is_singular() ) { $queried_object = get_queried_object(); if ( is_post_type_hierarchical( $queried_object->post_type ) ) { $current_page = $queried_object->ID; } } $output .= walk_page_tree( $pages, $r['depth'], $current_page, $r ); if ( $r['title_li'] ) { $output .= '</ul></li>'; } } /** * Filter the HTML output of the pages to list. * * @since 1.5.1 * * @see wp_list_pages() * * @param string $output HTML output of the pages list. * @param array $r An array of page-listing arguments. */ $html = apply_filters( 'wp_list_pages', $output, $r ); if ( $r['echo'] ) { echo $html; } else { return $html; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | /** * Display or retrieve list of pages with optional home link. * * The arguments are listed below and part of the arguments are for {@link * wp_list_pages()} function. Check that function for more info on those * arguments. * * @since 2.7.0 * * @param array|string $args { * Optional. Arguments to generate a page menu. {@see wp_list_pages()} * for additional arguments. * * @type string $sort_column How to short the list of pages. Accepts post column names. * Default 'menu_order, post_title'. * @type string $menu_class Class to use for the div ID containing the page list. Default 'menu'. * @type bool $echo Whether to echo the list or return it. Accepts true (echo) or false (return). * Default true. * @type string $link_before The HTML or text to prepend to $show_home text. Default empty. * @type string $link_after The HTML or text to append to $show_home text. Default empty. * @type int|string $show_home Whether to display the link to the home page. Can just enter the text * you'd like shown for the home link. 1|true defaults to 'Home'. * } * @return string html menu */ function wp_page_menu( $args = array() ) { $defaults = array('sort_column' => 'menu_order, post_title', 'menu_class' => 'menu', 'echo' => true, 'link_before' => '', 'link_after' => ''); $args = wp_parse_args( $args, $defaults ); /** * Filter the arguments used to generate a page-based menu. * * @since 2.7.0 * * @see wp_page_menu() * * @param array $args An array of page menu arguments. */ $args = apply_filters( 'wp_page_menu_args', $args ); $menu = ''; $list_args = $args; // Show Home in the menu if ( ! empty($args['show_home']) ) { if ( true === $args['show_home'] || '1' === $args['show_home'] || 1 === $args['show_home'] ) $text = __('Home'); else $text = $args['show_home']; $class = ''; if ( is_front_page() && !is_paged() ) $class = 'class="current_page_item"'; $menu .= '<li ' . $class . '><a href="' . home_url( '/' ) . '">' . $args['link_before'] . $text . $args['link_after'] . '</a></li>'; // If the front page is a page, add it to the exclude list if (get_option('show_on_front') == 'page') { if ( !empty( $list_args['exclude'] ) ) { $list_args['exclude'] .= ','; } else { $list_args['exclude'] = ''; } $list_args['exclude'] .= get_option('page_on_front'); } } $list_args['echo'] = false; $list_args['title_li'] = ''; $menu .= str_replace( array( "\r", "\n", "\t" ), '', wp_list_pages($list_args) ); if ( $menu ) $menu = '<ul>' . $menu . '</ul>'; $menu = '<div class="' . esc_attr($args['menu_class']) . '">' . $menu . "</div>\n"; /** * Filter the HTML output of a page-based menu. * * @since 2.7.0 * * @see wp_page_menu() * * @param string $menu The HTML output. * @param array $args An array of arguments. */ $menu = apply_filters( 'wp_page_menu', $menu, $args ); if ( $args['echo'] ) echo $menu; else return $menu; } |
関数 walk_page_tree()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | // // Page helpers // /** * Retrieve HTML list content for page list. * * @uses Walker_Page to create HTML list content. * @since 2.1.0 * @see Walker_Page::walk() for parameters and return description. */ function walk_page_tree($pages, $depth, $current_page, $r) { if ( empty($r['walker']) ) $walker = new Walker_Page; else $walker = $r['walker']; foreach ( (array) $pages as $page ) { if ( $page->post_parent ) $r['pages_with_children'][ $page->post_parent ] = true; } $args = array($pages, $depth, $r, $current_page); return call_user_func_array(array($walker, 'walk'), $args); } |
関数 walk_page_dropdown_tree()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | /** * Retrieve HTML dropdown (select) content for page list. * * @uses Walker_PageDropdown to create HTML dropdown content. * @since 2.1.0 * @see Walker_PageDropdown::walk() for parameters and return description. */ function walk_page_dropdown_tree() { $args = func_get_args(); if ( empty($args[2]['walker']) ) // the user's options are the third parameter $walker = new Walker_PageDropdown; else $walker = $args[2]['walker']; return call_user_func_array(array($walker, 'walk'), $args); } |
クラス Walker_Page::クラス変数
継承元::Walker /blog/wp-includes/class-wp-walker.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | /** * Create HTML list of pages. * * @since 2.1.0 * @uses Walker */ class Walker_Page extends Walker { /** * @see Walker::$tree_type * @since 2.1.0 * @var string */ public $tree_type = 'page'; /** * @see Walker::$db_fields * @since 2.1.0 * @todo Decouple this. * @var array */ public $db_fields = array ('parent' => 'post_parent', 'id' => 'ID'); |
クラス Walker_Page::start_lvl()
1 2 3 4 5 6 7 8 9 10 11 12 | /** * @see Walker::start_lvl() * @since 2.1.0 * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of page. Used for padding. * @param array $args */ public function start_lvl( &$output, $depth = 0, $args = array() ) { $indent = str_repeat("\t", $depth); $output .= "\n$indent<ul class='children'>\n"; } |
クラス Walker_Page::end_lvl()
1 2 3 4 5 6 7 8 9 10 11 12 | /** * @see Walker::end_lvl() * @since 2.1.0 * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of page. Used for padding. * @param array $args */ public function end_lvl( &$output, $depth = 0, $args = array() ) { $indent = str_repeat("\t", $depth); $output .= "$indent</ul>\n"; } |
クラス Walker_Page::start_el()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | /** * @see Walker::start_el() * @since 2.1.0 * * @param string $output Passed by reference. Used to append additional content. * @param object $page Page data object. * @param int $depth Depth of page. Used for padding. * @param int $current_page Page ID. * @param array $args */ public function start_el( &$output, $page, $depth = 0, $args = array(), $current_page = 0 ) { if ( $depth ) { $indent = str_repeat( "\t", $depth ); } else { $indent = ''; } $css_class = array( 'page_item', 'page-item-' . $page->ID ); if ( isset( $args['pages_with_children'][ $page->ID ] ) ) { $css_class[] = 'page_item_has_children'; } if ( ! empty( $current_page ) ) { $_current_page = get_post( $current_page ); if ( in_array( $page->ID, $_current_page->ancestors ) ) { $css_class[] = 'current_page_ancestor'; } if ( $page->ID == $current_page ) { $css_class[] = 'current_page_item'; } elseif ( $_current_page && $page->ID == $_current_page->post_parent ) { $css_class[] = 'current_page_parent'; } } elseif ( $page->ID == get_option('page_for_posts') ) { $css_class[] = 'current_page_parent'; } /** * Filter the list of CSS classes to include with each page item in the list. * * @since 2.8.0 * * @see wp_list_pages() * * @param array $css_class An array of CSS classes to be applied * to each list item. * @param WP_Post $page Page data object. * @param int $depth Depth of page, used for padding. * @param array $args An array of arguments. * @param int $current_page ID of the current page. */ $css_classes = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page ) ); if ( '' === $page->post_title ) { $page->post_title = sprintf( __( '#%d (no title)' ), $page->ID ); } $args['link_before'] = empty( $args['link_before'] ) ? '' : $args['link_before']; $args['link_after'] = empty( $args['link_after'] ) ? '' : $args['link_after']; /** This filter is documented in wp-includes/post-template.php */ $output .= $indent . sprintf( '<li class="%s"><a href="%s">%s%s%s</a>', $css_classes, get_permalink( $page->ID ), $args['link_before'], apply_filters( 'the_title', $page->post_title, $page->ID ), $args['link_after'] ); if ( ! empty( $args['show_date'] ) ) { if ( 'modified' == $args['show_date'] ) { $time = $page->post_modified; } else { $time = $page->post_date; } $date_format = empty( $args['date_format'] ) ? '' : $args['date_format']; $output .= " " . mysql2date( $date_format, $time ); } } |
クラス Walker_Page::end_el()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /** * @see Walker::end_el() * @since 2.1.0 * * @param string $output Passed by reference. Used to append additional content. * @param object $page Page data object. Not used. * @param int $depth Depth of page. Not Used. * @param array $args */ public function end_el( &$output, $page, $depth = 0, $args = array() ) { $output .= "</li>\n"; } } |
Walker_Page::クラス定義終了