백엔드 개발 PHP 튜토리얼 WordPress_php 팁에서 페이지 링크 및 제목을 얻기 위한 관련 PHP 함수의 사용 분석

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

May 16, 2016 pm 08:02 PM
php wordpress 제목 링크

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에 있습니다.

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

CakePHP 프로젝트 구성 CakePHP 프로젝트 구성 Sep 10, 2024 pm 05:25 PM

이번 장에서는 CakePHP의 환경 변수, 일반 구성, 데이터베이스 구성, 이메일 구성에 대해 알아봅니다.

Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드 Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드 Dec 24, 2024 pm 04:42 PM

PHP 8.4는 상당한 양의 기능 중단 및 제거를 통해 몇 가지 새로운 기능, 보안 개선 및 성능 개선을 제공합니다. 이 가이드에서는 Ubuntu, Debian 또는 해당 파생 제품에서 PHP 8.4를 설치하거나 PHP 8.4로 업그레이드하는 방법을 설명합니다.

CakePHP 날짜 및 시간 CakePHP 날짜 및 시간 Sep 10, 2024 pm 05:27 PM

cakephp4에서 날짜와 시간을 다루기 위해 사용 가능한 FrozenTime 클래스를 활용하겠습니다.

CakePHP 파일 업로드 CakePHP 파일 업로드 Sep 10, 2024 pm 05:27 PM

파일 업로드 작업을 위해 양식 도우미를 사용할 것입니다. 다음은 파일 업로드의 예입니다.

CakePHP 라우팅 CakePHP 라우팅 Sep 10, 2024 pm 05:25 PM

이번 장에서는 라우팅과 관련된 다음과 같은 주제를 학습하겠습니다.

CakePHP 토론 CakePHP 토론 Sep 10, 2024 pm 05:28 PM

CakePHP는 PHP용 오픈 소스 프레임워크입니다. 이는 애플리케이션을 훨씬 쉽게 개발, 배포 및 유지 관리할 수 있도록 하기 위한 것입니다. CakePHP는 강력하고 이해하기 쉬운 MVC와 유사한 아키텍처를 기반으로 합니다. 모델, 뷰 및 컨트롤러 gu

PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법 PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법 Dec 20, 2024 am 11:31 AM

VS Code라고도 알려진 Visual Studio Code는 모든 주요 운영 체제에서 사용할 수 있는 무료 소스 코드 편집기 또는 통합 개발 환경(IDE)입니다. 다양한 프로그래밍 언어에 대한 대규모 확장 모음을 통해 VS Code는

CakePHP 유효성 검사기 만들기 CakePHP 유효성 검사기 만들기 Sep 10, 2024 pm 05:26 PM

컨트롤러에 다음 두 줄을 추가하면 유효성 검사기를 만들 수 있습니다.

See all articles