web-dev-qa-db-ja.com

カテゴリアーカイブ内のページコンテンツを表示する(archive.php)

私はwordpressでテンプレート編集をするのが初めてなので、自分のサイトに表示するページの説明を取得しようとしていますが、表示されません。説明を表示するために取得しようとしているURLは次のとおりです。

http://fourwallsla.com/category/the-neighborhood/east-side/

そして私はワードプレス内のページの説明のスクリーンショットを添付しました。アーカイブテンプレートを使用しているようです(なぜページテンプレートを使用していないのですか?)。

アーカイブテンプレートのコードは次のとおりです。

<?php get_header(); ?>
<div class="main">
    <!--<?php iinclude_page(17); ?>-->
    <?php
        $category = get_the_category(); 
        $catName = $category[0]->cat_name;
        $catName = strtolower($catName);

    ?>

    <?php if (have_posts()) : $first = true; ?>
        <?php $post = $posts[0]; // Hack. Set $post so that the_date() works. ?>
        <?php /* If this is a category archive */ if (is_category()) { ?>
          <h1><?php single_cat_title(); ?></h1>
        <?php /* If this is a tag archive */ } elseif( is_tag() ) { ?>
          <h1><?php single_tag_title(); ?></h1>
        <?php /* If this is a daily archive */ } elseif (is_day()) { ?>
          <h1><?php echo get_the_time('F jS, Y'); ?></h1>
        <?php /* If this is a monthly archive */ } elseif (is_month()) { ?>
          <h1><?php echo get_the_time('F, Y'); ?></h1>
        <?php /* If this is a yearly archive */ } elseif (is_year()) { ?>
          <h1><?php echo get_the_time('Y'); ?></h1>
        <?php /* If this is an author archive */ } elseif (is_author()) { ?>
          <h1><?php _e( 'Author Archive', 'gray_white_black' ); ?></h1>
        <?php /* If this is a paged archive */ } elseif (isset($_GET['paged']) && !empty($_GET['paged'])) { ?>
          <h1><?php _e( 'Blog Archives', 'gray_white_black' ); ?></h1>
        <?php } ?>

        <div>
        <?php
        switch ($catName)
        {
            case "east side":
                iinclude_page(17);
                break;
            case "downtown":
                iinclude_page(20);
                break;
            case "mid-city":
                iinclude_page(15);
                break;
            case "west side":
                iinclude_page(23);
                break;
            case "coastal":
                iinclude_page(27);
                break;
            case "valley":
                iinclude_page(25);
                break;
            default:
                iinclude_page(6);
        }
        ?>
    </div>

        <ul class="post-list">
        <?php while (have_posts()) : the_post();
            if($first) $class = "first-in-row";
            else $class="";
            $first = !$first;
            ?>
            <!-- Start: Post -->
            <li <?php post_class($class); ?>>
                <?php the_post_thumbnail(); ?>
                <p class="categories"><?php the_category(", "); ?></p>
                <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a> <?php edit_post_link(__('Edit', 'gray_white_black'), '', ''); ?></h2>

                <?php the_excerpt(); ?>
                <p class="more"><a href="<?php the_permalink() ?>"><?php _e( '&raquo;&raquo; ', 'gray_white_black' );?></a></p>
                <?php if(has_tag()): ?><p class="tags"><span><?php the_tags(""); ?></span></p><?php endif; ?>
            </li>
            <!-- End: Post -->
        <?php endwhile; ?>
        </ul>

        <p class="pagination">
            <span class="prev"><?php next_posts_link(__('&laquo; Previous Posts', 'gray_white_black')) ?></span>
            <span class="next"><?php previous_posts_link(__('Next posts &raquo;', 'gray_white_black')) ?></span>
        </p>

    <?php else : ?>
        <h2><?php _e( 'Not found', 'gray_white_black' ); ?></h2>
        <p><?php _e( 'Sorry, but you are looking for something that isn\'t here.', 'gray_white_black' ); ?></p>
        <?php get_search_form(); ?>
    <?php endif; ?>
</div>
<?php get_sidebar(); ?>

<?php get_footer(); ?>

上記のコードで、the_post()は、ページに表示されているカテゴリ内のホームリスティングのコンテンツを適切に指しています。しかし、スクリーンショットに示すように、ページ自体のthe_post()を取得するにはどうすればよいですか。ありがとう。

enter image description here

1
mheavers

あなたがページIDを知っているならば、最も簡単な答えはそのページの内容を得ることでしょう。

// This retrieves the content from the post or page where ID = $page_id.
// You can either set this above here, or call get_the_content(12). Your choice.
$parent_page_content = get_the_content($page_id)

// Clean it up and make it a bit more legible:
$parent_page_content = str_replace(']]>', ']]&gt;', $parent_page_content);
$parent_page_content = apply_filters('the_content', $parent_page_content);

// Now echo out the content in place! <p> tags won't be necessary here, btw.
echo $parent_page_content;

このメソッドはループにまったく干渉しないので、これを実行した後でwp_reset_query()を使用する必要はありません。また、the_contentフィルタをテキストに適用しているので、段落タグと改行タグが自動的に追加されます。

お役に立てれば。

1
Alex Lane

しかし、どうすればページ自体のthe_post()を取得できますか?

簡単な答え:あなたはそうではない

archive.phpファイルは WordPressテンプレート階層の一部 で、単なるtemplateファイルです。投稿内容はありません。現在のクエリに一致する投稿の内容をループして出力するだけです。

特定の説明を出力したい場合は、archive.phpファイルを直接編集して、出力したい内容を伝える必要があります。

編集する

カテゴリ名がページ名と同じであるカテゴリテンプレートにページコンテンツを出力したい。

あなたはこれを行うことができますが、それは確かにWordPressに固有のものではありません。静的ページとブログ投稿は、まったく異なる2つのタイプのコンテンツ(つまり、まったく異なる2つの投稿タイプ)であり、両者の間に相関関係はありません。 WordPressは、特定のカテゴリ分類用語を持つblog postsを、その分類用語と同じスラッグを持つ静的ページに関連付けないでください。

とは言っても、あなたはcan2つを関連付けることができます。ただし、このプロセスはやや脆弱です。

<?php
if ( is_category() ) {
    // Get current category slug
    $cat = get_query_var( 'cat' );
    $cat_object = get_category( $cat );
    $category_slug = $cat_object->slug;

    // Get the ID of the static page with the same slub
    $category_static_page_id = get_page_by_path( $category_slug );

    // If such a page exists, get it
    if ( isset( $category_static_page_id ) ) {
        // Get the static page object
        $category_static_page = get_page( $category_static_page_id );

        // Output page content
        // Apply the usual the_content filters,
        // for usual "Nice" formatting
        echo apply_filters( 'the_content', $category_static_page->post_content );
    }
}
?>
0
Chip Bennett