Jc John Jc John - 2 months ago 6
PHP Question

wordpress Post a different post assigned inside the category of html

Hello there good programmers, I have 3 different sections in my webpage. I am using a wordpress loop to post all the post in wordpress. my problem is, i want to assign every post in the other sections or something conditions ?, for example.

For example i have 3 different sections
i have a section id named offer, i want to display the post that is limited only for my section offer page.

<section id = "offer">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if ( in_category( '3' ) ) : ?>
<div class="post-cat-three">
<?php else : ?>
<div class="post">
<?php endif; ?>
</section>


next section is the section id named projects, i want to display a post that is intended for my projects sections only. and put in inside my section named

<section id="projects">

</section>


The last section is the contact section where i put my contact details and other informations, and i want it to be assigned in contact section.

<section id = "contact">

</section>

Answer

You can do it as @Omar Faruque says or do one single query, then store every section content on a variable and then do the rest of the markup, like this:

<?php

$args = array(

//Category Parameters
'category__in'     => array(1, 2, 3), //Query only the posts on the categories with IDs 1, 2, 3

//Type & Status Parameters
'post_type'   => 'post',

);

$query = new WP_Query( $args );
$category1_posts = "";
$category2_posts = "";
$category3_posts = "";
if($query->have_post()) {   
    while($query->have_posts()) {
        $query->the_post();
        $thumbnail_url = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ) );
        if(in_category(1)) {
            $category1_posts .= '<div class="category1_post">';
            $category1_posts .= '<h2>' . get_the_title() . '</h2>':
            $category1_posts .= '<img src="' . $thumbnail_url . '">';
            //Same for other elements you want here, the_excerpt for example
            $category1_posts .= '</div>';
        } else if (in_category(2)) {
            $category2_posts .= '<div class="category2_post">';
            $category2_posts .= '<h2>' . get_the_title() . '</h2>':
            $category2_posts .= '<img src="' . $thumbnail_url . '">';
            //Same for other elements you want here, the_excerpt for example
            $category2_posts .= '</div>';
        } else {
            $category3_posts .= '<div class="category3_post">';
            $category3_posts .= '<h2>' . get_the_title() . '</h2>':
            $category3_posts .= '<img src="' . $thumbnail_url . '">';
            //Same for other elements you want here, the_excerpt for example
            $category3_posts .= '</div>';
        }
    }
}

wp_reset_query();
//Then just place the sections and echo the posts you have in your variables

?>

<section id="category1">
    <?php echo $category1_posts; ?>
</section>

<section id="category2">
    <?php echo $category2_posts; ?>
</section>

<section id="category3">
    <?php echo $category3_posts; ?>
</section>
Comments