-- 创造无限可能

wordpress主要函数

2022-05-17 00:14:55
431 人浏览 6 人点赞
有用,点赞支持一下
  • single_post_title()

    /**
    * 显示或返回页面的title
    *
    * 仅限single.php文件使用
    *
    *
    * @param string $prefix  前缀
    * @param bool   $display 是否输出,默认输出
    * @return string|void Title when retrieving.
    */
    function single_post_title( $prefix = '', $display = true ) {
      $_post = get_queried_object();
    
      if ( ! isset( $_post->post_title ) ) {
          return;
      }
    
      /**
       * Filters the page title for a single post.
       *
       * @since 0.71
       *
       * @param string  $_post_title The single post page title.
       * @param WP_Post $_post       The current post.
       */
      $title = apply_filters( 'single_post_title', $_post->post_title, $_post );
      if ( $display ) {
          echo $prefix . $title;
      } else {
          return $prefix . $title;
      }
    }
    
  • _e()
    /**
    * 翻译并输出文本
    *
    * @param string $text   需要翻译的文本.
    * @param string $domain 定义域,指定用那种定义域翻译文本
    */
    function _e( $text, $domain = 'default' ) {
      echo translate( $text, $domain );
    }
    
  • have_posts() 判断是否有文章
    ```php
    /**
    • 判断当前是否有文章
    • 使用$wp_query查询到文章后,使用have_posts()循环判断是否有文章,如果有,执行the_post(),然后迭代一次,再进行判断,如此反复
    • @global WP_Query $wp_query WordPress Query object.
    • @return bool True if posts are available, false if end of the loop.
      */
      function have_posts() {
      global $wp_query;
      return $wp_query->have_posts();
      }

/**

  • 完整例子
    */
    if ( have_posts() ) {
    while ( have_posts() ) {
      the_post();
      // 处理当前文章的数据,如:
      the_title();
    
    }
    }
    ```
  • the_post()
    /**
    * 获取当前文章,并迭代一次
    * @global WP_Query $wp_query WordPress Query object.
    */
    function the_post() {
      global $wp_query;
      $wp_query->the_post();
    }
    
  1. the_posts_pagination()
    ```php
    /**
    • 显示当前文章的上下篇
      *
    • @param array $args Optional. See get_the_posts_pagination() for available arguments.
    • Default empty array.
      /
      function the_posts_pagination( $args = array() ) {
      echo get_the_posts_pagination( $args );
      }
      /*
  • 调用
    */
    the_posts_pagination(
    array(
    ‘prev_text’ => ‘上一篇文本’,
    ‘next_text’ => ‘下一篇文本’,
    ‘before_page_number’ => ‘’,
    )
    );
    ```
  • has_nav_menu()

    /**
    * 判断是否存在对应名称的菜单
    *
    * @since 3.0.0
    *
    * @param string $location Menu location identifier.
    * @return bool Whether location has a menu.
    */
    function has_nav_menu( $location ) {
      $has_nav_menu = false;
    
      $registered_nav_menus = get_registered_nav_menus();
      if ( isset( $registered_nav_menus[ $location ] ) ) {
          $locations    = get_nav_menu_locations();
          $has_nav_menu = ! empty( $locations[ $location ] );
      }
    
      /**
       * Filters whether a nav menu is assigned to the specified location.
       *
       * @since 4.3.0
       *
       * @param bool   $has_nav_menu Whether there is a menu assigned to a location.
       * @param string $location     Menu location.
       */
      return apply_filters( 'has_nav_menu', $has_nav_menu, $location );
    }
    
  1. wp_nav_menu()
    ```php

/**

  • 显示一个导航菜单
    *
  • @since 3.0.0
  • @since 4.7.0 Added the item_spacing argument.
  • @since 5.5.0 Added the container_aria_label argument.
    *
  • @param array $args {
  • Optional. Array of nav menu arguments.
    *
  • @type int|string|WP_Term $menu Desired menu. Accepts a menu ID, slug, name, or object.
  • Default empty.
  • @type string $menu_class CSS class to use for the ul element which forms the menu.
  • Default ‘menu’.
  • @type string $menu_id The ID that is applied to the ul element which forms the menu.
  • Default is the menu slug, incremented.
  • @type string $container Whether to wrap the ul, and what to wrap it with.
  • Default ‘div’.
  • @type string $container_class Class that is applied to the container.
  • Default ‘menu-{menu slug}-container’.
  • @type string $container_id The ID that is applied to the container. Default empty.
  • @type string $container_aria_label The aria-label attribute that is applied to the container
  • when it’s a nav element. Default empty.
  • @type callable|bool $fallback_cb If the menu doesn’t exist, a callback function will fire.
  • Default is ‘wp_page_menu’. Set to false for no fallback.
  • @type string $before Text before the link markup. Default empty.
  • @type string $after Text after the link markup. Default empty.
  • @type string $link_before Text before the link text. Default empty.
  • @type string $link_after Text after the link text. Default empty.
  • @type bool $echo Whether to echo the menu or return it. Default true.
  • @type int $depth How many levels of the hierarchy are to be included.
  • 0 means all. Default 0.
  • Default 0.
  • @type object $walker Instance of a custom walker class. Default empty.
  • @type string $theme_location Theme location to be used. Must be registered with
  • register_nav_menu() in order to be selectable by the user.
  • @type string $items_wrap How the list items should be wrapped. Uses printf() format with
  • numbered placeholders. Default is a ul with an id and class.
  • @type string $item_spacing Whether to preserve whitespace within the menu’s HTML.
  • Accepts ‘preserve’ or ‘discard’. Default ‘preserve’.
  • }
  • @return void|string|false Void if ‘echo’ argument is true, menu output if ‘echo’ is false.
  • False if there are no items or no menu was found.
    */
    function wp_nav_menu( $args = array() ) {
    static $menu_id_slugs = array();

    $defaults = array(
    ‘menu’ => ‘’,
    ‘container’ => ‘div’,
    ‘container_class’ => ‘’,
    ‘container_id’ => ‘’,
    ‘container_aria_label’ => ‘’,
    ‘menu_class’ => ‘menu’,
    ‘menu_id’ => ‘’,
    ‘echo’ => true,
    ‘fallback_cb’ => ‘wp_page_menu’,
    ‘before’ => ‘’,
    ‘after’ => ‘’,
    ‘link_before’ => ‘’,
    ‘link_after’ => ‘’,
    ‘items_wrap’ => ‘<ul id="%1$s" class="%2$s">%3$s</ul>‘,
    ‘item_spacing’ => ‘preserve’,
    ‘depth’ => 0,
    ‘walker’ => ‘’,
    ‘theme_location’ => ‘’,
    );

    $args = wp_parse_args( $args, $defaults );

    if ( ! in_array( $args[‘item_spacing’], array( ‘preserve’, ‘discard’ ), true ) ) {
    // Invalid value, fall back to default.
    $args[‘item_spacing’] = $defaults[‘item_spacing’];
    }

    /**

    • Filters the arguments used to display a navigation menu.
      *
    • @since 3.0.0
      *
    • @see wp_nav_menu()
      *
    • @param array $args Array of wp_nav_menu() arguments.
      */
      $args = apply_filters( ‘wp_nav_menu_args’, $args );
      $args = (object) $args;

    /**

    • Filters whether to short-circuit the wp_nav_menu() output.
      *
    • Returning a non-null value from the filter will short-circuit wp_nav_menu(),
    • echoing that value if $args->echo is true, returning that value otherwise.
      *
    • @since 3.9.0
      *
    • @see wp_nav_menu()
      *
    • @param string|null $output Nav menu output to short-circuit with. Default null.
    • @param stdClass $args An object containing wp_nav_menu() arguments.
      */
      $nav_menu = apply_filters( ‘pre_wp_nav_menu’, null, $args );

    if ( null !== $nav_menu ) {
    if ( $args->echo ) {
    echo $nav_menu;
    return;
    }

    return $nav_menu;
    }

    // Get the nav menu based on the requested menu.
    $menu = wp_get_nav_menu_object( $args->menu );

    // Get the nav menu based on the theme_location.
    $locations = get_nav_menu_locations();
    if ( ! $menu && $args->theme_location && $locations && isset( $locations[ $args->theme_location ] ) ) {
    $menu = wp_get_nav_menu_object( $locations[ $args->theme_location ] );
    }

    // Get the first menu that has items if we still can’t find a menu.
    if ( ! $menu && ! $args->theme_location ) {
    $menus = wp_get_nav_menus();
    foreach ( $menus as $menu_maybe ) {
    $menu_items = wp_get_nav_menu_items( $menu_maybe->term_id, array( ‘update_post_term_cache’ => false ) );
    if ( $menu_items ) {
    $menu = $menu_maybe;
    break;
    }
    }
    }

    if ( empty( $args->menu ) ) {
    $args->menu = $menu;
    }

    // If the menu exists, get its items.
    if ( $menu && ! is_wp_error( $menu ) && ! isset( $menu_items ) ) {
    $menu_items = wp_get_nav_menu_items( $menu->term_id, array( ‘update_post_term_cache’ => false ) );
    }

    /*

    • If no menu was found:
      • Fall back (if one was specified), or bail.
        *
    • If no menu items were found:
      • Fall back, but only if no theme location was specified.
      • Otherwise, bail.
        */
        if ( ( ! $menu || is_wp_error( $menu ) || ( isset( $menu_items ) && empty( $menu_items ) && ! $args->theme_location ) )
        && isset( $args->fallback_cb ) && $args->fallback_cb && is_callable( $args->fallback_cb ) ) {
        return call_user_func( $args->fallback_cb, (array) $args );
        }

    if ( ! $menu || is_wp_error( $menu ) ) {
    return false;
    }

    $nav_menu = ‘’;
    $items = ‘’;

    $show_container = false;
    if ( $args->container ) {
    /**

    • Filters the list of HTML tags that are valid for use as menu containers.
      *
    • @since 3.0.0
      *
    • @param string[] $tags The acceptable HTML tags for use as menu containers.
    • Default is array containing ‘div’ and ‘nav’.
      */
      $allowed_tags = apply_filters( ‘wp_nav_menu_container_allowedtags’, array( ‘div’, ‘nav’ ) );

    if ( is_string( $args->container ) && in_array( $args->container, $allowed_tags, true ) ) {
    $show_container = true;
    $class = $args->container_class ? ‘ class=”‘ . esc_attr( $args->container_class ) . ‘“‘ : ‘ class=”menu-‘ . $menu->slug . ‘-container”‘;
    $id = $args->container_id ? ‘ id=”‘ . esc_attr( $args->container_id ) . ‘“‘ : ‘’;
    $aria_label = ( ‘nav’ === $args->container && $args->container_aria_label ) ? ‘ aria-label=”‘ . esc_attr( $args->container_aria_label ) . ‘“‘ : ‘’;
    $nav_menu .= ‘<’ . $args->container . $id . $class . $aria_label . ‘>’;
    }
    }

    // Set up the $menu_item variables.
    _wp_menu_item_classes_by_context( $menu_items );

    $sorted_menu_items = array();
    $menu_items_with_children = array();
    foreach ( (array) $menu_items as $menu_item ) {
    $sorted_menu_items[ $menu_item->menu_order ] = $menu_item;
    if ( $menu_item->menu_item_parent ) {
    $menu_items_with_children[ $menu_item->menu_item_parent ] = true;
    }
    }

    // Add the menu-item-has-children class where applicable.
    if ( $menu_items_with_children ) {
    foreach ( $sorted_menu_items as &$menu_item ) {
    if ( isset( $menu_items_with_children[ $menu_item->ID ] ) ) {
    $menu_item->classes[] = ‘menu-item-has-children’;
    }
    }
    }

    unset( $menu_items, $menu_item );

    /**

    • Filters the sorted list of menu item objects before generating the menu’s HTML.
      *
    • @since 3.1.0
      *
    • @param array $sorted_menu_items The menu items, sorted by each menu item’s menu order.
    • @param stdClass $args An object containing wp_nav_menu() arguments.
      */
      $sorted_menu_items = apply_filters( ‘wp_nav_menu_objects’, $sorted_menu_items, $args );

    $items .= walk_nav_menu_tree( $sorted_menu_items, $args->depth, $args );
    unset( $sorted_menu_items );

    // Attributes.
    if ( ! empty( $args->menu_id ) ) {
    $wrap_id = $args->menu_id;
    } else {
    $wrap_id = ‘menu-‘ . $menu->slug;

    while ( in_array( $wrap_id, $menu_id_slugs, true ) ) {
    if ( preg_match( ‘#-(\d+)$#’, $wrap_id, $matches ) ) {
    $wrap_id = preg_replace( ‘#-(\d+)$#’, ‘-‘ . ++$matches[1], $wrap_id );
    } else {
    $wrap_id = $wrap_id . ‘-1’;
    }
    }
    }
    $menu_id_slugs[] = $wrap_id;

    $wrap_class = $args->menu_class ? $args->menu_class : ‘’;

    /**

    • Filters the HTML list content for navigation menus.
      *
    • @since 3.0.0
      *
    • @see wp_nav_menu()
      *
    • @param string $items The HTML list content for the menu items.
    • @param stdClass $args An object containing wp_nav_menu() arguments.
      /
      $items = apply_filters( ‘wp_nav_menu_items’, $items, $args );
      /*
    • Filters the HTML list content for a specific navigation menu.
      *
    • @since 3.0.0
      *
    • @see wp_nav_menu()
      *
    • @param string $items The HTML list content for the menu items.
    • @param stdClass $args An object containing wpnav_menu() arguments.
      */
      $items = apply_filters( “wp_nav_menu
      {$menu->slug}_items”, $items, $args );

    // Don’t print any markup if there are no items at this point.
    if ( empty( $items ) ) {
    return false;
    }

    $nav_menu .= sprintf( $args->items_wrap, esc_attr( $wrap_id ), esc_attr( $wrap_class ), $items );
    unset( $items );

    if ( $show_container ) {
    $nav_menu .= ‘</‘ . $args->container . ‘>’;
    }

    /**

    • Filters the HTML content for navigation menus.
      *
    • @since 3.0.0
      *
    • @see wp_nav_menu()
      *
    • @param string $nav_menu The HTML content for the navigation menu.
    • @param stdClass $args An object containing wp_nav_menu() arguments.
      */
      $nav_menu = apply_filters( ‘wp_nav_menu’, $nav_menu, $args );

    if ( $args->echo ) {
    echo $nav_menu;
    } else {
    return $nav_menu;
    }
    }
    wp_nav_menu(
    array(

       'theme_location' => 'social',
       'menu_class'     => 'social-links-menu',
       'depth'          => 1,
       'link_before'    => '<span class="screen-reader-text">',
       'link_after'     => '</span>' . twentyseventeen_get_svg( array( 'icon' => 'chain' ) ),
    

    )
    );
    ```

    • wp_footer()
      ```php
  • 触发wp_footer钩子
    *
  • See {@see ‘wp_footer’}.
    *
  • @since 1.5.1
    /
    function wp_footer() {
    /*
    • Prints scripts or data before the closing body tag on the front end.
      *
    • @since 1.5.1
      */
      do_action( ‘wp_footer’ );
      }
      ```
      • language_attributes()
        ```php
        /**
  • 输出当前网站Html标签的语言属性
    *
  • Builds up a set of HTML attributes containing the text direction and language
  • information for the page.
    *
  • @since 2.1.0
  • @since 4.3.0 Converted into a wrapper for get_language_attributes().
    *
  • @param string $doctype Optional. The type of HTML document. Accepts ‘xhtml’ or ‘html’. Default ‘html’.
    */
    function language_attributes( $doctype = ‘html’ ) {
    echo get_language_attributes( $doctype );
    }
    - bloginfo() 根据名称输出对应的博客基础信息
    ```php
    bloginfo( 'charset' );
    
    1. wp_head() 触发wp_head钩子
      function wp_head() {
      /**
      * Prints scripts or data in the head tag on the front end.
      *
      * @since 1.5.0
      */
      do_action( 'wp_head' );
      }
      
    2. wp_body_open()
      /**
      * Shim for wp_body_open, ensuring backward compatibility with versions of WordPress older than 5.2.
      */
      function wp_body_open() {
       do_action( 'wp_body_open' );
      }
      
    3. is_single()
      ```php

/**

  • Determines whether the query is for an existing single post.
    *
  • Works for any post type, except attachments and pages
    *
  • If the $post parameter is specified, this function will additionally
  • check if the query is for one of the Posts specified.
    *
  • For more information on this and similar theme functions, check out
  • the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  • Conditional Tags} article in the Theme Developer Handbook.
    *
  • @since 1.5.0
    *
  • @see is_page()
  • @see is_singular()
  • @global WP_Query $wp_query WordPress Query object.
    *
  • @param int|string|int[]|string[] $post Optional. Post ID, title, slug, or array of such
  • to check against. Default empty.
  • @return bool Whether the query is for an existing single post.
    */
    function is_single( $post = ‘’ ) {
    global $wp_query;

    if ( ! isset( $wp_query ) ) {

     _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
     return false;
    

    }

    return $wp_query->is_single( $post );
    }
    ```

  1. has_post_thumbnail()
    ```php

/**

  • 判断是否有缩略图,默认判断当前文章
    *
  • @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
  • @return bool Whether the post has an image attached.
    */
    function has_post_thumbnail( $post = null ) {
    $thumbnail_id = get_post_thumbnail_id( $post );
    $has_thumbnail = (bool) $thumbnail_id;

    /**

    • Filters whether a post has a post thumbnail.
      *
    • @since 5.1.0
      *
    • @param bool $has_thumbnail true if the post has a post thumbnail, otherwise false.
    • @param int|WP_Post|null $post Post ID or WP_Post object. Default is global $post.
    • @param int|false $thumbnail_id Post thumbnail ID or false if the post does not exist.
      */
      return (bool) apply_filters( ‘has_post_thumbnail’, $has_thumbnail, $post, $thumbnail_id );
      }
      ```
      1. get_queried_object_id()
        ```php
        /**
  • Retrieves the ID of the currently queried object.
    *
  • Wrapper for WP_Query::get_queried_object_id().
    *
  • @since 3.1.0
    *
  • @global WP_Query $wp_query WordPress Query object.
    *
  • @return int ID of the queried object.
    */
    function get_queried_object_id() {
    global $wp_query;
    return $wp_query->get_queried_object_id();
    }
    ```
    1. get_the_post_thumbnail()
      ```php
      /**
  • 获取文章的缩略图,默认是当前文章
    *
  • When a theme adds ‘post-thumbnail’ support, a special ‘post-thumbnail’ image size
  • is registered, which differs from the ‘thumbnail’ image size managed via the
  • Settings > Media screen.
    *
  • When using the_post_thumbnail() or related functions, the ‘post-thumbnail’ image
  • size is used by default, though a different size can be specified instead as needed.
    *
  • @since 2.9.0
  • @since 4.4.0 $post can be a post ID or WP_Post object.
    *
  • @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
  • @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of
  • width and height values in pixels (in that order). Default ‘post-thumbnail’.
  • @param string|array $attr Optional. Query string or array of attributes. Default empty.
  • @return string The post thumbnail image tag.
    */
    function get_the_post_thumbnail( $post = null, $size = ‘post-thumbnail’, $attr = ‘’ ) {
    $post = get_post( $post );

    if ( ! $post ) {

     return '';
    

    }

    $post_thumbnail_id = get_post_thumbnail_id( $post );

    /**

    • Filters the post thumbnail size.
      *
    • @since 2.9.0
    • @since 4.9.0 Added the $post_id parameter.
      *
    • @param string|int[] $size Requested image size. Can be any registered image size name, or
    • an array of width and height values in pixels (in that order).
    • @param int $post_id The post ID.
      */
      $size = apply_filters( ‘post_thumbnail_size’, $size, $post->ID );

      if ( $post_thumbnail_id ) {

      /**

      • Fires before fetching the post thumbnail HTML.
        *
      • Provides “just in time” filtering of all filters in wp_get_attachment_image().
        *
      • @since 2.9.0
        *
      • @param int $post_id The post ID.
      • @param int $post_thumbnail_id The post thumbnail ID.
      • @param string|int[] $size Requested image size. Can be any registered image size name, or
      • an array of width and height values in pixels (in that order).
        */
        do_action( ‘begin_fetch_post_thumbnail_html’, $post->ID, $post_thumbnail_id, $size );

        if ( in_the_loop() ) {
        update_post_thumbnail_cache();
        }

        $html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr );

        /**

      • Fires after fetching the post thumbnail HTML.
        *
      • @since 2.9.0
        *
      • @param int $post_id The post ID.
      • @param int $post_thumbnail_id The post thumbnail ID.
      • @param string|int[] $size Requested image size. Can be any registered image size name, or
      • an array of width and height values in pixels (in that order).
        */
        do_action( ‘end_fetch_post_thumbnail_html’, $post->ID, $post_thumbnail_id, $size );

      } else {
      $html = ‘’;
      }

      /**

    • Filters the post thumbnail HTML.
      *
    • @since 2.9.0
      *
    • @param string $html The post thumbnail HTML.
    • @param int $post_id The post ID.
    • @param int $post_thumbnail_id The post thumbnail ID.
    • @param string|int[] $size Requested image size. Can be any registered image size name, or
    • an array of width and height values in pixels (in that order).
    • @param string $attr Query string of attributes.
      */
      return apply_filters( ‘post_thumbnail_html’, $html, $post->ID, $post_thumbnail_id, $size, $attr );
      }
      get_the_post_thumbnail( get_queried_object_id(), ‘twentyseventeen-featured-image’ )
      ```
      1. comments_template()
        ```php

/**

  • 加载评论模版
    *
  • Will not display the comments template if not on single post or page, or if
  • the post does not have comments.
    *
  • Uses the WordPress database object to query for the comments. The comments
  • are passed through the {@see ‘comments_array’} filter hook with the list of comments
  • and the post ID respectively.
    *
  • The $file path is passed through a filter hook called {@see ‘comments_template’},
  • which includes the TEMPLATEPATH and $file combined. Tries the $filtered path
  • first and if it fails it will require the default comment template from the
  • default theme. If either does not exist, then the WordPress process will be
  • halted. It is advised for that reason, that the default theme is not deleted.
    *
  • Will not try to get the comments if the post has none.
    *
  • @since 1.5.0
    *
  • @global WP_Query $wp_query WordPress Query object.
  • @global WP_Post $post Global post object.
  • @global wpdb $wpdb WordPress database abstraction object.
  • @global int $id
  • @global WP_Comment $comment Global comment object.
  • @global string $user_login
  • @global string $user_identity
  • @global bool $overridden_cpage
  • @global bool $withcomments
    *
  • @param string $file Optional. The file to load. Default ‘/comments.php’.
  • @param bool $separate_comments Optional. Whether to separate the comments by comment type.
  • Default false.
    */
    function comments_template( $file = ‘/comments.php’, $separate_comments = false ) {
    global $wp_query, $withcomments, $post, $wpdb, $id, $comment, $user_login, $user_identity, $overridden_cpage;

    if ( ! ( is_single() || is_page() || $withcomments ) || empty( $post ) ) {
    return;
    }

    if ( empty( $file ) ) {
    $file = ‘/comments.php’;
    }

    $req = get_option( ‘require_name_email’ );

    /*

    • Comment author information fetched from the comment cookies.
      */
      $commenter = wp_get_current_commenter();

    /*

    • The name of the current comment author escaped for use in attributes.
    • Escaped by sanitize_comment_cookies().
      */
      $comment_author = $commenter[‘comment_author’];

    /*

    • The email address of the current comment author escaped for use in attributes.
    • Escaped by sanitize_comment_cookies().
      */
      $comment_author_email = $commenter[‘comment_author_email’];

    /*

    • The URL of the current comment author escaped for use in attributes.
      */
      $comment_author_url = esc_url( $commenter[‘comment_author_url’] );

    $comment_args = array(
    ‘orderby’ => ‘comment_date_gmt’,
    ‘order’ => ‘ASC’,
    ‘status’ => ‘approve’,
    ‘post_id’ => $post->ID,
    ‘no_found_rows’ => false,
    ‘update_comment_meta_cache’ => false, // We lazy-load comment meta for performance.
    );

    if ( get_option( ‘thread_comments’ ) ) {
    $comment_args[‘hierarchical’] = ‘threaded’;
    } else {
    $comment_args[‘hierarchical’] = false;
    }

    if ( is_user_logged_in() ) {
    $comment_args[‘include_unapproved’] = array( get_current_user_id() );
    } else {
    $unapproved_email = wp_get_unapproved_comment_author_email();

    if ( $unapproved_email ) {
    $comment_args[‘include_unapproved’] = array( $unapproved_email );
    }
    }

    $per_page = 0;
    if ( get_option( ‘page_comments’ ) ) {
    $per_page = (int) get_query_var( ‘comments_per_page’ );
    if ( 0 === $per_page ) {
    $per_page = (int) get_option( ‘comments_per_page’ );
    }

    $comment_args[‘number’] = $per_page;
    $page = (int) get_query_var( ‘cpage’ );

    if ( $page ) {
    $comment_args[‘offset’] = ( $page - 1 ) * $per_page;
    } elseif ( ‘oldest’ === get_option( ‘default_comments_page’ ) ) {
    $comment_args[‘offset’] = 0;
    } else {
    // If fetching the first page of ‘newest’, we need a top-level comment count.
    $top_level_query = new WP_Comment_Query();
    $top_level_args = array(
    ‘count’ => true,
    ‘orderby’ => false,
    ‘post_id’ => $post->ID,
    ‘status’ => ‘approve’,
    );

    if ( $comment_args[‘hierarchical’] ) {
    $top_level_args[‘parent’] = 0;
    }

    if ( isset( $comment_args[‘include_unapproved’] ) ) {
    $top_level_args[‘include_unapproved’] = $comment_args[‘include_unapproved’];
    }

    /**

    • Filters the arguments used in the top level comments query.
      *
    • @since 5.6.0
      *
    • @see WP_Comment_Query::__construct()
      *
    • @param array $top_level_args {
    • The top level query arguments for the comments template.
      *
    • @type bool $count Whether to return a comment count.
    • @type string|array $orderby The field(s) to order by.
    • @type int $post_id The post ID.
    • @type string|array $status The comment status to limit results by.
    • }
      */
      $top_level_args = apply_filters( ‘comments_template_top_level_query_args’, $top_level_args );

    $top_level_count = $top_level_query->query( $top_level_args );

    $comment_args[‘offset’] = ( ceil( $top_level_count / $per_page ) - 1 ) * $per_page;
    }
    }

    /**

    • Filters the arguments used to query comments in comments_template().
      *
    • @since 4.5.0
      *
    • @see WP_Comment_Query::__construct()
      *
    • @param array $comment_args {
    • Array of WP_Comment_Query arguments.
      *
    • @type string|array $orderby Field(s) to order by.
    • @type string $order Order of results. Accepts ‘ASC’ or ‘DESC’.
    • @type string $status Comment status.
    • @type array $include_unapproved Array of IDs or email addresses whose unapproved comments
    • will be included in results.
    • @type int $post_id ID of the post.
    • @type bool $no_found_rows Whether to refrain from querying for found rows.
    • @type bool $update_comment_meta_cache Whether to prime cache for comment meta.
    • @type bool|string $hierarchical Whether to query for comments hierarchically.
    • @type int $offset Comment offset.
    • @type int $number Number of comments to fetch.
    • }
      */
      $comment_args = apply_filters( ‘comments_template_query_args’, $comment_args );

    $comment_query = new WP_Comment_Query( $comment_args );
    $_comments = $comment_query->comments;

    // Trees must be flattened before they’re passed to the walker.
    if ( $comment_args[‘hierarchical’] ) {
    $comments_flat = array();
    foreach ( $_comments as $_comment ) {
    $comments_flat[] = $_comment;
    $comment_children = $_comment->get_children(
    array(
    ‘format’ => ‘flat’,
    ‘status’ => $comment_args[‘status’],
    ‘orderby’ => $comment_args[‘orderby’],
    )
    );

    foreach ( $comment_children as $comment_child ) {
    $comments_flat[] = $comment_child;
    }
    }
    } else {
    $comments_flat = $_comments;
    }

    /**

    • Filters the comments array.
      *
    • @since 2.1.0
      *
    • @param array $comments Array of comments supplied to the comments template.
    • @param int $post_ID Post ID.
      */
      $wp_query->comments = apply_filters( ‘comments_array’, $comments_flat, $post->ID );

    $comments = &$wp_query->comments;
    $wp_query->comment_count = count( $wp_query->comments );
    $wp_query->max_num_comment_pages = $comment_query->max_num_pages;

    if ( $separate_comments ) {
    $wp_query->comments_by_type = separate_comments( $comments );
    $comments_by_type = &$wp_query->comments_by_type;
    } else {
    $wp_query->comments_by_type = array();
    }

    $overridden_cpage = false;

    if ( ‘’ == get_query_var( ‘cpage’ ) && $wp_query->max_num_comment_pages > 1 ) {
    set_query_var( ‘cpage’, ‘newest’ === get_option( ‘default_comments_page’ ) ? get_comment_pages_count() : 1 );
    $overridden_cpage = true;
    }

    if ( ! defined( ‘COMMENTS_TEMPLATE’ ) ) {
    define( ‘COMMENTS_TEMPLATE’, true );
    }

    $theme_template = STYLESHEETPATH . $file;

    /**

    • Filters the path to the theme template file used for the comments template.
      *
    • @since 1.5.1
      *
    • @param string $theme_template The path to the theme template file.
      */
      $include = apply_filters( ‘comments_template’, $theme_template );

    if ( file_exists( $include ) ) {
    require $include;
    } elseif ( file_exists( TEMPLATEPATH . $file ) ) {
    require TEMPLATEPATH . $file;
    } else { // Backward compat code will be removed in a future release.
    require ABSPATH . WPINC . ‘/theme-compat/comments.php’;
    }
    }
    ```

    1. get_search_form()
      ```php

/**

  • 显示搜索表单
    *
  • Will first attempt to locate the searchform.php file in either the child or
  • the parent, then load it. If it doesn’t exist, then the default search form
  • will be displayed. The default search form is HTML, which will be displayed.
  • There is a filter applied to the search form HTML in order to edit or replace
  • it. The filter is {@see ‘get_search_form’}.
    *
  • This function is primarily used by themes which want to hardcode the search
  • form into the sidebar and also by the search widget in WordPress.
    *
  • There is also an action that is called whenever the function is run called,
  • {@see ‘pre_get_search_form’}. This can be useful for outputting JavaScript that the
  • search relies on or various formatting that applies to the beginning of the
  • search. To give a few examples of what it can be used for.
    *
  • @since 2.7.0
  • @since 5.2.0 The $args array parameter was added in place of an $echo boolean flag.
    *
  • @param array $args {
  • Optional. Array of display arguments.
    *
  • @type bool $echo Whether to echo or return the form. Default true.
  • @type string $aria_label ARIA label for the search form. Useful to distinguish
  • multiple search forms on the same page and improve
  • accessibility. Default empty.
  • }
  • @return void|string Void if ‘echo’ argument is true, search form HTML if ‘echo’ is false.
    /
    function get_search_form( $args = array() ) {
    /*

    • Fires before the search form is retrieved, at the start of get_search_form().
      *
    • @since 2.7.0 as ‘get_search_form’ action.
    • @since 3.6.0
    • @since 5.5.0 The $args parameter was added.
      *
    • @link https://core.trac.wordpress.org/ticket/19321
      *
    • @param array $args The array of arguments for building the search form.
      */
      do_action( ‘pre_get_search_form’, $args );

      $echo = true;

      if ( ! is_array( $args ) ) {
      /*

      • Back compat: to ensure previous uses of get_search_form() continue to
      • function as expected, we handle a value for the boolean $echo param removed
      • in 5.2.0. Then we deal with the $args array and cast its defaults.
        */
        $echo = (bool) $args;

        // Set an empty array and allow default arguments to take over.
        $args = array();
        }

      // Defaults are to echo and to output no custom label on the form.
      $defaults = array(
      ‘echo’ => $echo,
      ‘aria_label’ => ‘’,
      );

      $args = wp_parse_args( $args, $defaults );

      /**

    • Filters the array of arguments used when generating the search form.
      *
    • @since 5.2.0
      *
    • @param array $args The array of arguments for building the search form.
      */
      $args = apply_filters( ‘search_form_args’, $args );

      // Ensure that the filtered arguments contain all required default values.
      $args = array_merge( $defaults, $args );

      $format = current_theme_supports( ‘html5’, ‘search-form’ ) ? ‘html5’ : ‘xhtml’;

      /**

    • Filters the HTML format of the search form.
      *
    • @since 3.6.0
    • @since 5.5.0 The $args parameter was added.
      *
    • @param string $format The type of markup to use in the search form.
    • Accepts ‘html5’, ‘xhtml’.
    • @param array $args The array of arguments for building the search form.
      */
      $format = apply_filters( ‘search_form_format’, $format, $args );

      $search_form_template = locate_template( ‘searchform.php’ );

      if ( ‘’ !== $search_form_template ) {
      ob_start();
      require $search_form_template;
      $form = ob_get_clean();
      } else {
      // Build a string containing an aria-label to use for the search form.
      if ( $args[‘aria_label’] ) {

       $aria_label = 'aria-label="' . esc_attr( $args['aria_label'] ) . '" ';
      

      } else {

       /*
        * If there's no custom aria-label, we can set a default here. At the
        * moment it's empty as there's uncertainty about what the default should be.
        */
       $aria_label = '';
      

      }

      if ( ‘html5’ === $format ) {

       $form = '<form role="search" ' . $aria_label . 'method="get" class="search-form" action="' . esc_url( home_url( '/' ) ) . '">
           <label>
               <span class="screen-reader-text">' . _x( 'Search for:', 'label' ) . '</span>
               <input type="search" class="search-field" placeholder="' . esc_attr_x( 'Search &hellip;', 'placeholder' ) . '" value="' . get_search_query() . '" name="s" />
           </label>
           <input type="submit" class="search-submit" value="' . esc_attr_x( 'Search', 'submit button' ) . '" />
       </form>';
      

      } else {

       $form = '<form role="search" ' . $aria_label . 'method="get" id="searchform" class="searchform" action="' . esc_url( home_url( '/' ) ) . '">
           <div>
               <label class="screen-reader-text" for="s">' . _x( 'Search for:', 'label' ) . '</label>
               <input type="text" value="' . get_search_query() . '" name="s" id="s" />
               <input type="submit" id="searchsubmit" value="' . esc_attr_x( 'Search', 'submit button' ) . '" />
           </div>
       </form>';
      

      }
      }

      /**

    • Filters the HTML output of the search form.
      *
    • @since 2.7.0
    • @since 5.5.0 The $args parameter was added.
      *
    • @param string $form The search form HTML output.
    • @param array $args The array of arguments for building the search form.
      */
      $result = apply_filters( ‘get_search_form’, $form, $args );

      if ( null === $result ) {
      $result = $form;
      }

      if ( $args[‘echo’] ) {
      echo $result;
      } else {
      return $result;
      }
      }
      ```

      1. the_ID()
        ```php
        /**
  • 输出当前文章的id
    *
  • @since 0.71
    */
    function the_ID() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
    echo get_the_ID();
    }
    ```
  1. get_post_type()

    /**
    * 获取文章的类型,默认获取当前文章
    *
    * @since 2.1.0
    *
    * @param int|WP_Post|null $post Optional. Post ID or post object. Default is global $post.
    * @return string|false          Post type on success, false on failure.
    */
    function get_post_type( $post = null ) {
    $post = get_post( $post );
    if ( $post ) {
        return $post->post_type;
    }
    
    return false;
    }
    
  2. get_permalink()
    ```php

/**

  • 获取文章的链接,默认获取当前文章,亦可根据文章id获取
    *
  • @since 1.0.0
    *
  • @param int|WP_Post $post Optional. Post ID or post object. Default is the global $post.
  • @param bool $leavename Optional. Whether to keep post name or page name. Default false.
  • @return string|false The permalink URL or false if post does not exist.
    */
    function get_permalink( $post = 0, $leavename = false ) {
    $rewritecode = array(

     '%year%',
     '%monthnum%',
     '%day%',
     '%hour%',
     '%minute%',
     '%second%',
     $leavename ? '' : '%postname%',
     '%post_id%',
     '%category%',
     '%author%',
     $leavename ? '' : '%pagename%',
    

    );

    if ( is_object( $post ) && isset( $post->filter ) && ‘sample’ === $post->filter ) {

     $sample = true;
    

    } else {

     $post   = get_post( $post );
     $sample = false;
    

    }

    if ( empty( $post->ID ) ) {

     return false;
    

    }

    if ( ‘page’ === $post->post_type ) {

     return get_page_link( $post, $leavename, $sample );
    

    } elseif ( ‘attachment’ === $post->post_type ) {

     return get_attachment_link( $post, $leavename );
    

    } elseif ( in_array( $post->post_type, get_post_types( array( ‘_builtin’ => false ) ), true ) ) {

     return get_post_permalink( $post, $leavename, $sample );
    

    }

    $permalink = get_option( ‘permalink_structure’ );

    /**

    • Filters the permalink structure for a post before token replacement occurs.
      *
    • Only applies to posts with post_type of ‘post’.
      *
    • @since 3.0.0
      *
    • @param string $permalink The site’s permalink structure.
    • @param WP_Post $post The post in question.
    • @param bool $leavename Whether to keep the post name.
      */
      $permalink = apply_filters( ‘pre_post_link’, $permalink, $post, $leavename );

      if (
      $permalink &&
      ! in_array( $post->post_status, array( ‘draft’