WordPress get page links and titles related PHP function usage parsing _php tips

Source: Internet
Author: User

Get_permalink () (Get article or page link)
Get_permalink () is used to return a link to an article or page based on a fixed connection. The Get_permalink () function needs to know the ID of the article to get when getting the link, and automatically defaults to the current article if it is in the loop.

Usage

Get_permalink ($id, $leavename);

Parameters

$id

(mixed) (optional) An article or page ID (integer), or an article object.

Default value: Automatic invocation of current article in loop

$leavename

(Boolean) (optionally) convert to a link whether or not to ignore the article alias. If set to True, Http://www.example.com/%postname% is returned instead of Http://www.example.com/my-post-name

Default value: None

return value

(String | Boolean) A link is returned if the link is successfully obtained, and False if it fails.

Example

Get a link to an article or page based on an ID:

<a href= "<?php Echo get_permalink (268);?>" > Get the article or page link with the specified ID </a>

Loop to get the current article link:

<?php echo Get_permalink ();?>

Get page links based on page title:

<a href= "<?php Echo esc_url (get_permalink (' Message board ')) get_page_by_title" > Message board </a>

Other

This function is located at: wp-includes/link-template.php

Wp_title () (Get page title)
Wp_title () is used to get the title of the current page, which is what is inside the title tag.

Wp_title () can automatically generate different titles on different pages (such as the first page is the title of the site, the article page is the title of the article). WordPress's official theme is using this function to generate headers, but it is always overlooked in domestic topics (because this function is not too good for SEO by default).

I still recommend the use of this function to call the title, more in line with the theme development norms, if you want to make him more consistent with the SEO, you can use filter optimization, the end of this article has an optimization method.

Usage

Wp_title ($sep, $display, $seplocation);

Parameters

$sep

(optional) The separator for the caption content, typically set to "|" or "-".

Default value:» (»)

$display

(Boolean) (optional) Whether the caption is printed directly and, if set to False, returns a caption, which can be stored in a variable.

Default value: True (direct print output)

$seplocation

(string) (optional) The location of the separator, to the left or right, or to the right, if you pass "rights", and any other content is left.

Default value: Empty string (left)

return value

(string) If the $display parameter is set to False to return the contents of the header, different content is returned depending on the title, and the content returned by default is this:

    • Article page: article title
    • Date page: Date
    • Category pages: Category headings
    • Author Page: Author name

If you want more detailed understanding, you can read the following function source:


/** * Display or retrieve page title for all areas of blog.  * By default, the page title would display the separator before the page title, * So the blog title would be before The page title. This isn't good for * title display, since the blog title shows up most tabs and not what are * important, which is T
 He page, the user is looking at. * * There are also SEO benefits to have the blog title after or to the ' right ' * or the page title. However, it is mostly common sense to have the blogger title * to "right" with most browsers supporting. You can achieve this with * using the seplocation parameter and setting the value to ' right '.
 This change * was introduced around 2.5.0, in case backwards compatibility the 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 StThe Ring $seplocation Optional.
 Direction to display title, ' right '.
 * @return String|null string on retrieve, or null when displaying.
 
  * * Function wp_title ($sep = '» ', $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 ' 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_obj
    ECT ();
  if ($author) $title = $author->display_name;
  The//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 $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 = Sprint
  F (' 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;
 }

Obviously by default the title is relatively simple, not very friendly to SEO.

Example

<title><?php wp_title (' | ', true, ' right ');?></title>

Other

The title can be customized using the Wp_title filter, which is located at: wp-includes/general-template.php

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.