- 赤色のリンクは、まだ日本語Codexに存在しないページ・画像です。英語版と併せてご覧ください。(詳細)
「テンプレートタグ/get permalink」の版間の差分
(→Pass in a post object instead of an ID) |
(→戻り値) |
||
15行目: | 15行目: | ||
== 戻り値 == | == 戻り値 == | ||
− | ; (string|bool) : | + | ; (string|bool) : パーマリンクURLまたは <tt>false</tt> (ページが存在しない場合)。 |
== 用例 == | == 用例 == |
2014年5月17日 (土) 16:36時点における版
目次
説明
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)
- パーマリンクURLまたは false (ページが存在しない場合)。
用例
<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>
IDの代わりに投稿のオブジェクトを渡す
投稿の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 (最新版との差分)