详解WordPress开发中wp_title()函数的用法,wordpresswp_title
详解WordPress开发中wp_title()函数的用法,wordpresswp_title
wp_title 函数在 WordPress 中是用来显示文章、页面、分类等等等等标题的一个函数,但在首页索引,该函数将不显示任何的东西。该函数在 WordPress 官方主题中一直被使用,但目前很多定制的主题中这个函数总是为忽视。
函数意义详解
wp_title 函数用来显示页面的标题,如在文章页面,则显示文章标题;在分类页面,则显示分类名称,但在首页索引,该函数将不显示任何的东西。
有点像 WordPress 中的 get_the_title 和 single_cat_title()这两个函数的自适应用法(自动判断是页面、文章还是分类、归档、标签)。
函数声明
有点长,希望您能耐心看一遍,哪怕只有那么一遍。
/** * 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 '»'. 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 = '»', $display = true, $seplocation = '') { global $wpdb, $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 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(); $tax = get_taxonomy( $term->taxonomy ); $title = single_term_title( $tax->labels->name . $t_sep, false ); } // If there's an author if ( is_author() ) { $author = get_queried_object(); $title = $author->display_name; } // If there's a post type archive if ( is_post_type_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 ? $t_sep . $my_month : '' ) . ( $my_day ? $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 "; // Determines position of the separator and direction of the breadcrumb if ( 'right' == $seplocation ) { // sep on right, so reverse the order $title_array = explode( $t_sep, $title ); $title_array = array_reverse( $title_array ); $title = implode( " $sep ", $title_array ) . $prefix; } else { $title_array = explode( $t_sep, $title ); $title = $prefix . implode( " $sep ", $title_array ); } $title = apply_filters('wp_title', $title, $sep, $seplocation); // Send it out if ( $display ) echo $title; else return $title; }
用法
<?php wp_title( $sep, $echo, $seplocation ); ?>
参数详解
- $sep:分隔符;
- $echo:是否显示;
- $seplocation:分隔符所在位置(左还是右,只接受'right',如果不是right自动判定为左)
总结
WordPress 中相同功能的函数有很多,都是从基层到高级不断的经过封装最后到达使用层的,当然如果我们需要一些灵活用法的话,我们可以直接用中间那层的函数,如果我们懒的话我们可以直接使用最高级的那层函数,诸如本函数 wp_title ,其实这个函数我们从源代码来看, wp 替我们针对 分类、标签、文章、归档、作者、页面等多种类型的页面进行了判断,并根据不同页面调用不同的标题函数来达到目的。
如果有时间,您可以对下面几个函数进行深入研究一下,一遍更灵活的进行seo
single_post_title 文章页面提取标题的函数
single_term_title tag(标签)、cat(分类)、日期、提取标题的函数 类似于 single_cat_title()函数
get_queried_object 作者页面提取对象的函数(对象中有作者名)
post_type_archive_title()规档等等提取标题的函数
还等什么?
GO GO GO !
您可能感兴趣的文章:
- WordPress开发中用于标题显示的相关函数使用解析
- WordPress中调试缩略图的相关PHP函数使用解析
- 配置解决Nginx服务器中WordPress路径不自动加斜杠问题
- WordPress中用于获取搜索表单的PHP函数使用解析
- 在WordPress中使用wp_count_posts函数来统计文章数量
- 详解WordPress中调用评论模板和循环输出评论的PHP函数
- 详解WordPress中分类函数wp_list_categories的使用
- WordPress中限制非管理员用户在文章后只能评论一次
- 详解WordPress中创建和添加过滤器的相关PHP函数

핫 AI 도구

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

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

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

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

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

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

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

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

뜨거운 주제











PHP와 Flutter는 모바일 개발에 널리 사용되는 기술입니다. Flutter는 크로스 플랫폼 기능, 성능 및 사용자 인터페이스가 뛰어나며 고성능, 크로스 플랫폼 및 맞춤형 UI가 필요한 애플리케이션에 적합합니다. PHP는 성능이 낮고 크로스 플랫폼이 아닌 서버 측 애플리케이션에 적합합니다.

style.css 파일을 편집하여 WordPress 페이지 너비를 쉽게 수정할 수 있습니다. style.css 파일을 편집하고 .site-content { max-width: [원하는 너비] }를 추가하세요. [원하는 너비]를 편집하여 페이지 너비를 설정하세요. 변경 사항을 저장하고 캐시를 지웁니다(선택 사항).

WordPress에서 제품 페이지 만들기: 1. 제품 만들기(이름, 설명, 사진) 2. 페이지 템플릿 사용자 정의(제목, 설명, 사진, 버튼 추가) 3. 제품 정보(재고, 크기, 무게)를 입력합니다. 4. 변형(다양한 색상, 크기)을 만듭니다. 5. 공개 또는 숨김을 설정합니다. 6. 댓글을 활성화/비활성화합니다. 7. 페이지를 미리 보고 게시합니다.

WordPress 게시물은 /wp-content/uploads 폴더에 저장됩니다. 이 폴더는 하위 폴더를 사용하여 연도, 월, 기사 ID별로 정리된 기사를 포함하여 다양한 유형의 업로드를 분류합니다. 기사 파일은 일반 텍스트 형식(.txt)으로 저장되며 파일 이름에는 일반적으로 ID와 제목이 포함됩니다.

WordPress 템플릿 파일은 /wp-content/themes/[테마 이름]/ 디렉터리에 있습니다. 헤더(header.php), 바닥글(footer.php), 기본 템플릿(index.php), 단일 기사(single.php), 페이지(page.php)를 포함하여 웹사이트의 모양과 기능을 결정하는 데 사용됩니다. , 아카이브(archive.php), 카테고리(category.php), 태그(tag.php), 검색(search.php) 및 404 오류 페이지(404.php). 이러한 파일을 편집하고 수정하면 WordPress 웹사이트의 모양을 사용자 정의할 수 있습니다.

WordPress에서 작성자 검색: 1. 관리자 패널에 로그인한 후 게시물 또는 페이지로 이동하여 검색 표시줄을 사용하여 작성자 이름을 입력하고 필터에서 작성자를 선택합니다. 2. 기타 팁: 와일드카드를 사용하여 검색 범위를 넓히고, 연산자를 사용하여 기준을 결합하거나, 저자 ID를 입력하여 기사를 검색하세요.

WordPress는 데이터베이스 상호 작용, 양식 처리, 동적 콘텐츠 생성 및 사용자 요청을 처리하기 위한 핵심 프로그래밍 언어로 PHP 언어를 사용하여 개발되었습니다. PHP는 크로스 플랫폼 호환성, 학습 용이성, 활발한 커뮤니티, 풍부한 라이브러리 및 프레임워크 등의 이유로 선택되었습니다. WordPress는 PHP 외에도 HTML, CSS, JavaScript, SQL 등과 같은 언어를 사용하여 기능을 향상시킵니다.

가장 안정적인 WordPress 버전은 최신 보안 패치, 성능 향상, 새로운 기능 및 개선 사항이 포함된 최신 버전입니다. 최신 버전으로 업데이트하려면 WordPress 대시보드에 로그인하고 업데이트 페이지로 이동하여 지금 업데이트를 클릭하세요.
