- 赤色のリンクは、まだ日本語Codexに存在しないページ・画像です。英語版と併せてご覧ください。(詳細)
「テンプレートタグ/get permalink」の版間の差分
(→デフォルトの使い方) |
(→特定の投稿へのリンク) |
||
28行目: | 28行目: | ||
<?php echo get_permalink(); ?> | <?php echo get_permalink(); ?> | ||
− | === | + | === 特定の投稿へのリンク === |
− | + | インフォメーションリストの中のハイパーリンクとして、2つの特定の投稿(post IDが1と10)のパーマリンクを返します。上記のようにこのタグはパーマリンクを出力しないので PHP の echo コマンドを使用しています。 | |
<ul> | <ul> |
2014年5月17日 (土) 16:29時点における版
目次
説明
Returns the permalink to a post or page for use in PHP. It does NOT display the permalink and can be used outside of The Loop. On failure returns false
.
Note that when used outside The Loop on a posts page (index, archive, etc.) without the ID parameter, it will return the URL of the last post in The Loop, not the permalink for the current page. See: http://core.trac.wordpress.org/ticket/9963
使い方
<?php $permalink = get_permalink( $id ); ?>
引数
- $id
- (mixed) (optional) The integer ID for a post or page, or a post object
- 初期値: The current post ID, when used in The Loop.
- $leavename
- (bool) (optional) Whether to keep the post name or page name. When set to true, a structural link will be returned, rather than the actual URI. Example: http://www.example.com/%postname% instead of http://www.example.com/my-post
- 初期値: false
戻り値
- (string|bool)
- The permalink URL, or false on failure (if the page doesn't exist).
用例
<a href="<?php echo get_permalink( 268 ); ?>">My link to a post or page</a>
デフォルトの使い方
(ループ内で使用される)現在のポストのパーマリンク。このタグはパーマリンクを出力しないので、この例ではPHPのechoコマンドを使用しています。
Permalink for this post: <?php echo get_permalink(); ?>
特定の投稿へのリンク
インフォメーションリストの中のハイパーリンクとして、2つの特定の投稿(post IDが1と10)のパーマリンクを返します。上記のようにこのタグはパーマリンクを出力しないので PHP の echo コマンドを使用しています。
<ul> <li>MyBlog info: <ul> <li><a href="<?php echo get_permalink(1); ?>">About MyBlog</a></li> <li><a href="<?php echo get_permalink(10); ?>">About the owner</a></li> </ul> </li> </ul>
Pass in a post object instead of an ID
This shows how you can you can get the permalink with the page title instead of the ID.
<a href="<?php echo esc_url( get_permalink( get_page_by_title( 'Monthly Events' ) ) ); ?>">Monthly Events</a>
変更履歴
Since: 1.0.0
ソースファイル
get_permalink() is located in wp-includes/link-template.php
.
関連項目
get_permalink(), the_permalink(), post_permalink(), permalink_anchor(), permalink_single_rss()
最新英語版: WordPress Codex » Function Reference/get_permalink (最新版との差分)