0

こんにちは私がやろうとしているのは、page.phpファイルで、各ページに一連のifステートメントを設定することです。たとえば、page==aboutusはaboutusコンテンツなどを表示します。現時点では、どのページを表示していても、page.phpファイルのすべてが表示されているので、自分が書いた方法とまったく同じであると確信しています。誰かが私が間違っていることを知っていますか?

これが私が今まで持っているものです

<?php get_head(); ?>

<?php is_page($page); ?>

<?php is_page('about-us'); ?> {

    <div id="container">
    <?php get_header(); ?>
    <?php get_banner(); ?>

        <div class=" center content" role="main">
            <div id="posts">
                <span class="title">About Us</span>     

                <div class="msghead">
                <?php query_posts('cat=7&showposts=1&orderby=date'); while (have_posts()) : the_post(); the_content(); 
                endwhile;?>
                </div>



             </div>

                   <div id="sidebardiv">
                <?php get_sidebar(); ?> </div>
                <div class="clear"> </div>

          </div>

       </div>
    <?php get_footer(); ?>

    </div>

}

<?php is_page('classes'); ?> {

    <div id="container">
    <?php get_header(); ?>
    <?php get_banner(); ?>

        <div class=" center content" role="main">
            <div id="posts">
                <span class="title">Classes</span>     

                <div class="msghead">
                <?php query_posts('cat=7&showposts=1&orderby=date'); while (have_posts()) : the_post(); the_content(); 
                endwhile;?>
                </div>



             </div>

                   <div id="sidebardiv">
                <?php get_sidebar(); ?> </div>
                <div class="clear"> </div>

          </div>

       </div>
    <?php get_footer(); ?>

    </div>

}
4

3 に答える 3

2

より簡単なアプローチはis_page('about-us')、現在のページを確認するために使用することです。

コーデックス:http://codex.wordpress.org/Function_Reference/is_page

于 2012-11-20T11:16:13.450 に答える
2

実際にページをチェックしているのではなく、is_page()関数を実行してコンテンツを表示しているだけです。

さらに、The Loopを使用する場合は、エラーを回避するために、最初に表示する投稿があることを確認する必要があります。

ヒントとして、ページの唯一の違いが表示される投稿である場合は、ループの前に引数を設定できます。これにより、コードのかなりの部分を切り取ることができます。以下は、そのメソッドの例を含むPastebinへのリンクです。さらに、テンプレートがpag​​e.phpの場合、表示しているページはすでにループに入っているので、それを利用してページのタイトルを表示できます。これはテストされていませんが、開始する必要があります。

<?php get_head(); ?>

<?php if(is_page('about-us')) : ?>

    <div id="container">
    <?php get_header(); ?>
    <?php get_banner(); ?>

        <div class=" center content" role="main">
            <div id="posts">
                <span class="title">About Us</span>     

                <div class="msghead">
                <?php
                query_posts('cat=7&showposts=1&orderby=date');
                if(have_posts()) : while (have_posts()) : the_post();
                        the_content(); 
                    endwhile;
                endif;
                ?>
                </div>



             </div>

                   <div id="sidebardiv">
                <?php get_sidebar(); ?> </div>
                <div class="clear"> </div>

          </div>

       </div>
    <?php get_footer(); ?>

    </div>

<?php elseif(is_page('classes')) : ?>

    <div id="container">
    <?php get_header(); ?>
    <?php get_banner(); ?>

        <div class=" center content" role="main">
            <div id="posts">
                <span class="title">Classes</span>     

                <div class="msghead">
                <?php
                query_posts('cat=7&showposts=1&orderby=date');
                if(have_posts()) : while (have_posts()) : the_post();
                        the_content(); 
                    endwhile;
                endif;
                ?>
                </div>



             </div>

                   <div id="sidebardiv">
                <?php get_sidebar(); ?> </div>
                <div class="clear"> </div>

          </div>

       </div>
    <?php get_footer(); ?>

    </div>

<?php endif; ?>
于 2012-11-20T11:53:25.590 に答える
1

ifステートメントがありません。

例えば

<?php if( is_page('about-us') ): ?>
  <p>about-us page here</p>
<?php elseif( is_page('classes') ): ?>
  <p>classes page here</p>
<?php endif; ?>

より簡単な方法は、page-about-us.phpおよびの新しいテンプレートを作成することです。page-classes.php

于 2012-11-20T11:58:46.363 に答える