WordPress_php 팁에서 페이지 링크 및 제목을 얻기 위한 관련 PHP 함수의 사용 분석

WBOY
풀어 주다: 2016-05-16 20:02:49
원래의
1092명이 탐색했습니다.

get_permalink()(기사 또는 페이지 링크 가져오기)
get_permalink()는 고정된 링크를 기반으로 기사나 페이지의 링크를 반환하는 데 사용됩니다. 링크를 가져올 때 get_permalink() 함수는 가져올 기사의 ID를 알아야 합니다. 루프에 있는 경우 자동으로 현재 기사가 기본값이 됩니다.

사용방법

get_permalink( $id, $leavename );
로그인 후 복사

매개변수

$id

(혼합) (선택 사항) 게시물 또는 페이지의 ID(정수)도 게시물 개체일 수 있습니다.

기본값: 루프에서 현재 기사를 자동으로 호출

$leavename

(부울) (선택 사항) 링크로 변환할 때 기사 별칭을 무시할지 여부입니다. True로 설정하면 http://www.example.com/my-post-name

대신 http://www.example.com/%postname%이 반환됩니다.

기본값: 없음

반환값

(문자열 | 부울) 링크가 성공적으로 획득되면 링크를 반환하고, 그렇지 않으면 False를 반환합니다.

ID를 기반으로 기사나 페이지 링크 가져오기:

<a href="<&#63;php echo get_permalink( 268 ); &#63;>">获取指定 ID 的文章或页面链接</a>
로그인 후 복사

루프에서 현재 기사의 링크 가져오기:

<&#63;php echo get_permalink(); &#63;>
로그인 후 복사

페이지 제목을 기준으로 페이지 링크 가져오기:

<a href="<&#63;php echo esc_url( get_permalink( get_page_by_title( '留言板' ) ) ); &#63;>">留言板</a>
로그인 후 복사

기타

이 기능은 wp-includes/link-template.php에 있습니다

wp_title() (웹페이지 제목 가져오기)
wp_title()은 제목 태그의 내용인 현재 웹페이지의 제목을 가져오는 데 사용됩니다.

wp_title()은 다양한 페이지에 다양한 제목을 자동으로 생성할 수 있습니다. 예를 들어 홈페이지는 웹사이트 제목이고 기사 페이지는 기사 제목입니다. 모든 공식 워드프레스 테마에서는 이 기능을 사용하여 제목을 생성하지만, 국내 테마에서는 항상 무시됩니다(이 기능은 기본적으로 SEO에 별로 좋지 않기 때문입니다).

테마 개발 사양에 더 부합하는 제목 호출에 이 기능을 사용하는 것이 좋습니다. SEO에 더 부합하도록 하려면 필터를 사용하여 최적화할 수 있습니다. 이 기사의 끝.

사용방법

wp_title( $sep, $display, $seplocation );
로그인 후 복사

매개변수

$9월

(문자열) (선택 사항) 제목 내용의 구분 기호로, 일반적으로 "|" 또는 "-"로 설정됩니다.

기본값: »(»)

$디스플레이

(Boolean) (선택) 제목을 직접 출력할지 여부. False로 설정하면 제목이 반환되어 변수에 저장될 수 있습니다.

기본값: True(직접 인쇄)

$seplocation

(문자열) (선택 사항) 구분 기호의 위치는 왼쪽 또는 오른쪽입니다. "right"가 전달되면 오른쪽이 되고 다른 모든 항목은 왼쪽이 됩니다.

기본값: 빈 문자열(왼쪽)

반환값

(문자열) $display 매개변수를 False로 설정하면 제목의 내용이 반환될 수 있습니다. 기본적으로 반환되는 내용은 다음과 같습니다.

    기사 페이지: 기사 제목
  • 날짜 페이지: 날짜
  • 카테고리 페이지: 카테고리 제목
  • 작가 페이지: 작가 이름
자세히 알고 싶다면 아래 함수 소스코드를 읽어보세요.


/**
 * Display or retrieve page title for all areas of blog.
 *
 * By default, the page title will display the separator before the page title,
 * so that the blog title will be before the page title. This is not good for
 * title display, since the blog title shows up on most tabs and not what is
 * important, which is the page that the user is looking at.
 *
 * There are also SEO benefits to having the blog title after or to the 'right'
 * or the page title. However, it is mostly common sense to have the blog title
 * to the right with most browsers supporting tabs. You can achieve this by
 * using the seplocation parameter and setting the value to 'right'. This change
 * was introduced around 2.5.0, in case backwards compatibility of themes is
 * important.
 *
 * @since 1.0.0
 *
 * @param string $sep Optional, default is '&raquo;'. How to separate the various items within the page title.
 * @param bool $display Optional, default is true. Whether to display or retrieve title.
 * @param string $seplocation Optional. Direction to display title, 'right'.
 * @return string|null String on retrieve, null when displaying.
 */
function wp_title($sep = '&raquo;', $display = true, $seplocation = '') {
  global $wp_locale;
 
  $m = get_query_var('m');
  $year = get_query_var('year');
  $monthnum = get_query_var('monthnum');
  $day = get_query_var('day');
  $search = get_query_var('s');
  $title = '';
 
  $t_sep = '%WP_TITILE_SEP%'; // Temporary separator, for accurate flipping, if necessary
 
  // If there is a post
  if ( is_single() || ( is_home() && !is_front_page() ) || ( is_page() && !is_front_page() ) ) {
    $title = single_post_title( '', false );
  }
 
  // If there's a post type archive
  if ( is_post_type_archive() ) {
    $post_type = get_query_var( 'post_type' );
    if ( is_array( $post_type ) )
      $post_type = reset( $post_type );
    $post_type_object = get_post_type_object( $post_type );
    if ( ! $post_type_object->has_archive )
      $title = post_type_archive_title( '', false );
  }
 
  // If there's a category or tag
  if ( is_category() || is_tag() ) {
    $title = single_term_title( '', false );
  }
 
  // If there's a taxonomy
  if ( is_tax() ) {
    $term = get_queried_object();
    if ( $term ) {
      $tax = get_taxonomy( $term->taxonomy );
      $title = single_term_title( $tax->labels->name . $t_sep, false );
    }
  }
 
  // If there's an author
  if ( is_author() && ! is_post_type_archive() ) {
    $author = get_queried_object();
    if ( $author )
      $title = $author->display_name;
  }
 
  // Post type archives with has_archive should override terms.
  if ( is_post_type_archive() && $post_type_object->has_archive )
    $title = post_type_archive_title( '', false );
 
  // If there's a month
  if ( is_archive() && !empty($m) ) {
    $my_year = substr($m, 0, 4);
    $my_month = $wp_locale->get_month(substr($m, 4, 2));
    $my_day = intval(substr($m, 6, 2));
    $title = $my_year . ( $my_month &#63; $t_sep . $my_month : '' ) . ( $my_day &#63; $t_sep . $my_day : '' );
  }
 
  // If there's a year
  if ( is_archive() && !empty($year) ) {
    $title = $year;
    if ( !empty($monthnum) )
      $title .= $t_sep . $wp_locale->get_month($monthnum);
    if ( !empty($day) )
      $title .= $t_sep . zeroise($day, 2);
  }
 
  // If it's a search
  if ( is_search() ) {
    /* translators: 1: separator, 2: search phrase */
    $title = sprintf(__('Search Results %1$s %2$s'), $t_sep, strip_tags($search));
  }
 
  // If it's a 404 page
  if ( is_404() ) {
    $title = __('Page not found');
  }
 
  $prefix = '';
  if ( !empty($title) )
    $prefix = " $sep ";
 
  /**
   * Filter the parts of the page title.
   *
   * @since 4.0.0
   *
   * @param array $title_array Parts of the page title.
   */
  $title_array = apply_filters( 'wp_title_parts', explode( $t_sep, $title ) );
 
   // Determines position of the separator and direction of the breadcrumb
  if ( 'right' == $seplocation ) { // sep on right, so reverse the order
    $title_array = array_reverse( $title_array );
    $title = implode( " $sep ", $title_array ) . $prefix;
  } else {
    $title = $prefix . implode( " $sep ", $title_array );
  }
 
  /**
   * Filter the text of the page title.
   *
   * @since 2.0.0
   *
   * @param string $title    Page title.
   * @param string $sep     Title separator.
   * @param string $seplocation Location of the separator (left or right).
   */
  $title = apply_filters( 'wp_title', $title, $sep, $seplocation );
 
  // Send it out
  if ( $display )
    echo $title;
  else
    return $title;
 
}
로그인 후 복사
분명히 제목은 기본적으로 상대적으로 단순하고 SEO에 그다지 친숙하지 않습니다.

<title><&#63;php wp_title( '|', true, 'right' ); &#63;></title>
로그인 후 복사
기타

제목은 wp_title 필터를 사용하여 맞춤 설정할 수 있습니다. 이 기능은 wp-includes/general-template.php에 있습니다.

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!