TAGS :Viewed: 13 - Published at: a few seconds ago

[ formatting divs using CSS for Search Results ]

Below is the code for the Search Results. The search separates page results from post results.

<?php
    foreach (array('post','page') as $pt) :
        $search_query = new WP_Query(array(
                'post_type'         => $pt,
                's'                 => $s,
                'posts_per_page'    => 10,
                'paged'             => $paged
            )
        );
    ?>
    <?php if ($pt == 'post') : ?>
        <h2 class="pagetitle">Post Search Results</h2>
    <?php else : ?>
        <h2 class="pagetitle">Page Search Results</h2>
    <?php endif; ?>
    <?php 
        if ($search_query->have_posts()) : while ($search_query->have_posts()) :         $search_query->the_post();
        if ($pt == 'post') :
    ?>
        Post Results Code
    <?php else : ?>
        Page Results Code
    <?php endif; ?>
    <?php endwhile; else : ?>
        No Results
    <?php endif; ?>
<?php endforeach; ?>

Using the current code, if no search results are found for either Post/Page Search Results, the phrase "No Results" is displayed below the Post/Page Results header(s). This needs to be preserved.

I want to position the header "Post Search Results" and the "Post Results Code" in one div and the header "Page Search Results" and the "Page Results Code" in a separate div that is set inside another div. Like this:

<div id="A">
<div id="B">Post Results header & Post Results Code</div>
<div id="C">Page Results header & Page Results Code</div>
</div>

Where do I insert the div tags in the code to accomplish this? Thank you!

Answer 1


This should do what you want:

<div id="A">
<?php
foreach (array('post','page') as $pt) :
    $search_query = new WP_Query(array(
            'post_type'         => $pt,
            's'                 => $s,
            'posts_per_page'    => 10,
            'paged'             => $paged
        )
    );
?>
<?php if ($pt == 'post') : ?>
    <div id="B">
    <h2 class="pagetitle">Post Search Results</h2>
<?php else : ?>
    <div id="C">
    <h2 class="pagetitle">Page Search Results</h2>
<?php endif; ?>
<?php 
    if ($search_query->have_posts()) : while ($search_query->have_posts()) :         $search_query->the_post();
    if ($pt == 'post') :
?>
    Post Results Code
<?php else : ?>
    Page Results Code
<?php endif; ?>
<?php endwhile; else : ?>
    No Results
<?php endif; ?>
</div>
<?php endforeach; ?>
</div>