0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Wordpressを下層ディレクトリに設置した際に、ホームページのパスをショートコードで取得して表示する方法

Posted at

Wordpressを下層ディレクトリに設置し、下層ディレクトリのみでWordpessを運用することがあります。
その際にホームページへのパスを予め定義しておくと便利です。

サイトの構造
■ホームページ(index.html) ←取得したいホームページはここ
├ hogehoge.html(普通のページ)
|
└ /hoge(Wordpress)
    └ 記事や固定ページなど

※WordPress アドレス (URL)と、サイトアドレス (URL)はいずれも【Wordpress設置ディレクトリ】を設定しています。

function.phpに以下を書く

function sitehome(){
	$wp_top = get_bloginfo('url');
	$sitehome = str_replace ('/hoge', '',$wp_top);
	return $sitehome;
}
add_shortcode('sitehome', 'sitehome');

// hoge はWordpress設置ディレクトリ
// http://www.○○○○○.com/hoge ←この部分

テンプレートファイルでの使用例

//例
<a href="<?php echo do_shortcode('[sitehome]'); ?>">ホーム</a>
<img src="<?php echo do_shortcode('[sitehome]'); ?>/images/img.png" alt="画像">

記事・固定ページの投稿画面での使用例

<a href="http://[sitehome]">ホーム</a>
<img src="http://[sitehome]/images/img.png" alt="画像">

相対パスではなく絶対パスで取得しています。
ショートコードに設定しておけば投稿画面でも使えるので便利です。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?