get_permalink() WordPress関数 テンプレートタグ

get_permalink()

参照:WordPress Codex-get_permalink()

構文

get_permalink(①$id[オプション], ②$leavename[オプション]);

get_permalink()の詳細

 動 作 

投稿・固定ページのパーマリンクを取得する。

パーマリンクを出力しない。

 

 返り値 

パーマリンクのURL。

失敗するとfalse。

 

 引 数 

①$id[オプション]

(複合)投稿または固定ページの整数型のID。またはオブジェクト。

(デフォルト)ループ内で使用した場合は、表示中のID。

②$leavename[オプション]

(真偽値)投稿名・固定ページ名を保持するかどうか。

true:実際のURIではなく構造的なリンクを返す。

https://nawate.jp/%postname% : 構造的なリンク

https://nawate.jp/wp-get_permalink : 実際のURI

(デフォルト)false

 

get_permalink()のコード

参照:wp-includes/link-template.php

<?php
110	/**
111	 * Retrieves the full permalink for the current post or post ID.
112	 *
113	 * @since 1.0.0
114	 *
115	 * @param int|WP_Post $post      Optional. Post ID or post object. Default is the global `$post`.
116	 * @param bool        $leavename Optional. Whether to keep post name or page name. Default false.
117	 * @return string|false The permalink URL or false if post does not exist.
118	 */
119	function get_permalink( $post = 0, $leavename = false ) {
120	        $rewritecode = array(
121	                '%year%',
122	                '%monthnum%',
123	                '%day%',
124	                '%hour%',
125	                '%minute%',
126	                '%second%',
127	                $leavename ? '' : '%postname%',
128	                '%post_id%',
129	                '%category%',
130	                '%author%',
131	                $leavename ? '' : '%pagename%',
132	        );
133	
134	        if ( is_object( $post ) && isset( $post->filter ) && 'sample' == $post->filter ) {
135	                $sample = true;
136	        } else {
137	                $post   = get_post( $post );
138	                $sample = false;
139	        }
140	
141	        if ( empty( $post->ID ) ) {
142	                return false;
143	        }
144	
145	        if ( $post->post_type == 'page' ) {
146	                return get_page_link( $post, $leavename, $sample );
147	        } elseif ( $post->post_type == 'attachment' ) {
148	                return get_attachment_link( $post, $leavename );
149	        } elseif ( in_array( $post->post_type, get_post_types( array( '_builtin' => false ) ) ) ) {
150	                return get_post_permalink( $post, $leavename, $sample );
151	        }
152	
153	        $permalink = get_option( 'permalink_structure' );
154	
155	        /**
156	         * Filters the permalink structure for a post before token replacement occurs.
157	         *
158	         * Only applies to posts with post_type of 'post'.
159	         *
160	         * @since 3.0.0
161	         *
162	         * @param string  $permalink The site's permalink structure.
163	         * @param WP_Post $post      The post in question.
164	         * @param bool    $leavename Whether to keep the post name.
165	         */
166	        $permalink = apply_filters( 'pre_post_link', $permalink, $post, $leavename );
167	
168	        if ( '' != $permalink && ! in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft', 'future' ) ) ) {
169	                $unixtime = strtotime( $post->post_date );
170	
171	                $category = '';
172	                if ( strpos( $permalink, '%category%' ) !== false ) {
173	                        $cats = get_the_category( $post->ID );
174	                        if ( $cats ) {
175	                                $cats = wp_list_sort(
176	                                        $cats,
177	                                        array(
178	                                                'term_id' => 'ASC',
179	                                        )
180	                                );
181	
182	                                /**
183	                                 * Filters the category that gets used in the %category% permalink token.
184	                                 *
185	                                 * @since 3.5.0
186	                                 *
187	                                 * @param WP_Term  $cat  The category to use in the permalink.
188	                                 * @param array    $cats Array of all categories (WP_Term objects) associated with the post.
189	                                 * @param WP_Post  $post The post in question.
190	                                 */
191	                                $category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post );
192	
193	                                $category_object = get_term( $category_object, 'category' );
194	                                $category        = $category_object->slug;
195	                                if ( $category_object->parent ) {
196	                                        $category = get_category_parents( $category_object->parent, false, '/', true ) . $category;
197	                                }
198	                        }
199	                        // show default category in permalinks, without
200	                        // having to assign it explicitly
201	                        if ( empty( $category ) ) {
202	                                $default_category = get_term( get_option( 'default_category' ), 'category' );
203	                                if ( $default_category && ! is_wp_error( $default_category ) ) {
204	                                        $category = $default_category->slug;
205	                                }
206	                        }
207	                }
208	
209	                $author = '';
210	                if ( strpos( $permalink, '%author%' ) !== false ) {
211	                        $authordata = get_userdata( $post->post_author );
212	                        $author     = $authordata->user_nicename;
213	                }
214	
215	                $date           = explode( ' ', gmdate( 'Y m d H i s', $unixtime ) );
216	                $rewritereplace =
217	                array(
218	                        $date[0],
219	                        $date[1],
220	                        $date[2],
221	                        $date[3],
222	                        $date[4],
223	                        $date[5],
224	                        $post->post_name,
225	                        $post->ID,
226	                        $category,
227	                        $author,
228	                        $post->post_name,
229	                );
230	                $permalink      = home_url( str_replace( $rewritecode, $rewritereplace, $permalink ) );
231	                $permalink      = user_trailingslashit( $permalink, 'single' );
232	        } else { // if they're not using the fancy permalink option
233	                $permalink = home_url( '?p=' . $post->ID );
234	        }
235	
236	        /**
237	         * Filters the permalink for a post.
238	         *
239	         * Only applies to posts with post_type of 'post'.
240	         *
241	         * @since 1.5.0
242	         *
243	         * @param string  $permalink The post's permalink.
244	         * @param WP_Post $post      The post in question.
245	         * @param bool    $leavename Whether to keep the post name.
246	         */
247	        return apply_filters( 'post_link', $permalink, $post, $leavename );
248	}
?>


WordPressのおすすめ-カスタマイズ編

おすすめ

 

スポンサードリンク

スポンサードリンク