PHP if else and while rules for categories

#1

I’ve got a grid of blog posts and I want to style posts from a certain category in a different way to the rest of the posts.

My current archive.php is pulling the same template_part for all the posts in the grid, so i thought if i alter it making a rule so that if posts have the category ‘guest blog’ they pull a different template, then this would style the category differently in the grid, but it hasn’t worked. The code I’ve tried is below but it’s breaking the blog page entirely, could anyone please help?

<?php get_header(); ?>
<main role="main" class="site-page">
    <div class="wrap">
        <article role="contentinfo" class="site-content">
            <?php echo '<h1>' . single_cat_title("", false) . '</h1>'; ?>
            <?php if (have_posts()) : ?>
                <div class="row">
                    <div class="col-6 ta-l"><?php getSearchResultsFromTo(); ?></div>
                    <div class="col-6 ta-r"><?php if (is_paginated){ paginate(); } ?></div>
                </div>
                <div class="resource_grid">
                    <?php while (have_posts()) : the_post(); ?>
                    <?php if ( in_category( 'guest-blog' ) ) : ?>
                        <?php get_template_part('_template-parts/content', 'search2'); ?>
                      <?php else : ?>
                     <?php get_template_part('_template-parts/content', 'search'); ?>
                   <?php endwhile; ?>
                </div>
            <?php else : ?>
                <?php get_template_part('_template-parts/content', 'none'); ?>
            <?php endif; ?>
        </article>
    </div>
</main>
<?php get_footer(); ?>
#2

You need to start with getting rid of the jumping in and out of Php for nothing nonsense.

By the way, who/where taught you to do such a thing?

#3

Looks like WordPress’s wonderful code style.

1 Like
#4

Sorry I’m still very new to coding, our developer coded this but is no longer in the company so I’m trying to alter it to get the new styling that I need, whilst we are trying to find a new developer. When you say jumping in and out of php do you mean going between

tags and tags? If you could give me any example of how it should be tweaked that would be greatly appreciated.
#5

That’s exactly what I mean. If you need a new developer, I am available. PM me the details.