YouTube

いお;

以下のPHPコードは、投稿ページに所属する親カテゴリーの小カテゴリーリストを表示します。このコードを投稿ページのテンプレートファイルに追加したり出来る。。

<?php
 // 現在の投稿のIDを取得
$post_id = get_the_ID();

// 現在の投稿が属するカテゴリーのIDを取得
$categories = get_the_category($post_id);

// 親カテゴリーのIDを取得
$parent_category_id = 0;
if (!empty($categories)) {
    foreach ($categories as $category) {
        if ($category->parent == 0) {
            $parent_category_id = $category->term_id;
            break;
        }
    }
}

// 親カテゴリーがある場合に小カテゴリーリストを表示
if ($parent_category_id != 0) {
    $args = array(
        'child_of' => $parent_category_id, // 親カテゴリーのIDを指定
        'hide_empty' => false, // 空のカテゴリーを表示
    );
    $child_categories = get_categories($args);
    if (!empty($child_categories)) {
        ?>
<div id="text-7" class="widget widget_text"><h4 class="widgettitle gf"><span>他のカテゴリー</span></h4>
        <div id="menu-list" class="l-wrap2 bg-black">
            <div class="l-inner">
                <ul class="l-content menu-column">
                    <?php foreach ($child_categories as $child_category) : ?>
                        <li class="column-item">
                            <a href="<?php echo esc_url(get_category_link($child_category->term_id)); ?>" class="btn-item">
                                <span><?php echo esc_html($child_category->name); ?></span>
                            </a>
                        </li>
                    <?php endforeach; ?>
                </ul>
            </div>
        </div>
 </div>
        <?php
    }
}
?>

このコードは、現在の投稿が所属する親カテゴリーを取得し、その親カテゴリーに属する小カテゴリーのリストを表示します。リスト内の各小カテゴリーは、リンクとして表示されます。

Stork19のサイドバーに表記する場合

やり方

いじるファイル: ./wp-content/themes/jstork19/parts/main-parts.php

main-parts.phpのなか #562 下の記述があるので

if (is_active_sidebar('sidebar1') || is_active_sidebar('side-fixed') || is_active_sidebar('sp-contentfoot')) {
		echo '<div id="sidebar1" class="sidebar" role="complementary">';

		if (is_active_sidebar('sp-contentfoot') && is_mobile()) {
			dynamic_sidebar('sp-contentfoot');
		}

		if (is_active_sidebar('sidebar1')) {
			if (!is_mobile() || (is_mobile() && !is_active_sidebar('sp-contentfoot'))) {
				dynamic_sidebar('sidebar1');
			}
		}

		if (is_active_sidebar('side-fixed') && !is_mobile()) {
			echo '<div id="scrollfix" class="scrollfix">';
			dynamic_sidebar('side-fixed');
			echo '</div>';
		}

		echo '</div>';
	}

この記述の任意の場所に挿入してください。このコードは、サイドバーのウィジェット領域を表示するループ内にあり、条件に基づいて表示されるサイドバーのウィジェットが制御されます。

if (is_active_sidebar('sidebar1') || is_active_sidebar('side-fixed') || is_active_sidebar('sp-contentfoot')) {
    echo '<div id="sidebar1" class="sidebar" role="complementary">';

    # ここに上記のコードを挿入
    # 上記のコードを挿入した後、次のコードに続けてください
    
    if (is_active_sidebar('sp-contentfoot') && is_mobile()) {
        dynamic_sidebar('sp-contentfoot');
    }

    if (is_active_sidebar('sidebar1')) {
        if (!is_mobile() || (is_mobile() && !is_active_sidebar('sp-contentfoot'))) {
            dynamic_sidebar('sidebar1');
        }
    }

   

    if (is_active_sidebar('side-fixed') && !is_mobile()) {
        echo '<div id="scrollfix" class="scrollfix">';
        dynamic_sidebar('side-fixed');
        echo '</div>';
    }

    echo '</div>';
}

結果

	if (is_active_sidebar('sidebar1') || is_active_sidebar('side-fixed') || is_active_sidebar('sp-contentfoot')) {
    echo '<div id="sidebar1" class="sidebar" role="complementary">';



    // 現在の投稿のIDを取得
    $post_id = get_the_ID();

    // 現在の投稿が属するカテゴリーのIDを取得
    $categories = get_the_category($post_id);

    // 親カテゴリーのIDを取得
    $parent_category_id = 0;
    if (!empty($categories)) {
        foreach ($categories as $category) {
            if ($category->parent == 0) {
                $parent_category_id = $category->term_id;
                break;
            }
        }
    }

    // 親カテゴリーがある場合に小カテゴリーリストを表示
    if ($parent_category_id != 0) {
        $args = array(
            'child_of' => $parent_category_id, // 親カテゴリーのIDを指定
            'hide_empty' => false, // 空のカテゴリーを表示
        );
        $child_categories = get_categories($args);
        if (!empty($child_categories)) {
            ?>
            <div id="text-7" class="widget widget_text"><h4 class="widgettitle gf"><span>カテゴリー絞り込み</span></h4>	
            <div id="menu-list" class="l-wrap2 bg-black">
                <div class="l-inner">
                    <ul class="l-content menu-column">
                        <?php foreach ($child_categories as $child_category) : ?>
                            <li class="column-item">
                                <a href="<?php echo esc_url(get_category_link($child_category->term_id)); ?>" class="btn-item">
                                    <span><?php echo esc_html($child_category->name); ?></span>
                                </a>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                </div>
            </div>
						</div>
            <?php
        }
    }

    if (is_active_sidebar('sp-contentfoot') && wp_is_mobile()) {
        dynamic_sidebar('sp-contentfoot');
    }

    if (is_active_sidebar('sidebar1')) {
        if (!wp_is_mobile() || (wp_is_mobile() && !is_active_sidebar('sp-contentfoot'))) {
            dynamic_sidebar('sidebar1');
        }
    }


    if (is_active_sidebar('side-fixed') && !wp_is_mobile()) {
        echo '<div id="scrollfix" class="scrollfix">';
        dynamic_sidebar('side-fixed');
        echo '</div>';
    }

    echo '</div>';
 }

コードをテンプレート化

ただ、中に書いたコードが多くなるとややこしくなるのでテンプレート化して呼び出すことにしました。

./wp-content/themes/jstork19/parts/main-parts.php の元コード

if (is_active_sidebar('sidebar1') || is_active_sidebar('side-fixed') || is_active_sidebar('sp-contentfoot')) {
		echo '<div id="sidebar1" class="sidebar" role="complementary">';

		if (is_active_sidebar('sp-contentfoot') && is_mobile()) {
			dynamic_sidebar('sp-contentfoot');
		}

		if (is_active_sidebar('sidebar1')) {
			if (!is_mobile() || (is_mobile() && !is_active_sidebar('sp-contentfoot'))) {
				dynamic_sidebar('sidebar1');
			}
		}

		if (is_active_sidebar('side-fixed') && !is_mobile()) {
			echo '<div id="scrollfix" class="scrollfix">';
			dynamic_sidebar('side-fixed');
			echo '</div>';
		}

		echo '</div>';
	}
これを下に変更

この行を追加: include(‘path/to/your/file.php’);

if (is_active_sidebar('sidebar1') || is_active_sidebar('side-fixed') || is_active_sidebar('sp-contentfoot')) {
    echo '<div id="sidebar1" class="sidebar" role="complementary">';

    // 他のPHPファイルを読み込む
    include('path/to/your/file.php'); // ファイルパスは実際のファイルの場所に合わせてください

    if (is_active_sidebar('sp-contentfoot') && is_mobile()) {
        dynamic_sidebar('sp-contentfoot');
    }

    if (is_active_sidebar('sidebar1')) {
        if (!is_mobile() || (is_mobile() && !is_active_sidebar('sp-contentfoot'))) {
            dynamic_sidebar('sidebar1');
        }
    }

    if (is_active_sidebar('side-fixed') && !is_mobile()) {
        echo '<div id="scrollfix" class="scrollfix">';
        dynamic_sidebar('side-fixed');
        echo '</div>';
    }

    echo '</div>';
}
解説

テーマに追加した他のPHPファイルを読み込むには、PHPの include もしくは require 関数を使用します。これらの関数は、指定したファイルを現在のファイルに含めることができます。上記は、その方法の例です。

上記のコードでは、include 関数を使用して、他のPHPファイルを読み込んでいます。path/to/your/file.php の部分は、実際のファイルのパスに置き換える必要があります。また、include 関数の代わりに require 関数を使用することもできます。これらの関数の違いは、ファイルが見つからなかった場合にエラーを発生させるかどうかです。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

ド "place_id" の件数: 40