ページ内目次

カスタム投稿のスラッグからタイトルと本文を表示

頻繁に書き換えるデータを格納しておくために、カスタム投稿を使用する場合があります。

基本的に1データだったりするので、そのIDから設定したタイトルとかカスタムフィールド値を引っ張り出せば良いのですが、無意味な数字よりも(なんとなく)意味のわかるスラッグで呼び出したかったりするのです。

という訳で、カスタム投稿のスラッグ名からデータを読み込んでくる方法。

rankingというカスタム投稿に、rankingdataというスラッグをつけたデータ(投稿)をして、そのタイトルと本文を読み込むコードです。

$pageID= get_page_by_path('rankingdata', OBJECT, 'ranking')->ID;
$post = get_post( $pageID );
$ranking_title = get_the_title( $pageID );
$ranking_data = $post->post_content;

 

This website stores cookies on your computer. These cookies are used to provide a more personalized experience and to track your whereabouts around our website in compliance with the European General Data Protection Regulation. If you decide to to opt-out of any future tracking, a cookie will be setup in your browser to remember this choice for one year.

Accept or Deny

↑上へまいりま〜す