Prusakov
Prusakov

Reputation: 347

Different post category with different output

I have got a little problem here. I have got two post categories so far: category_name=projects and category _name=blog. So I wanted to have different style and different output for posts in each category. So I have created two files projects.php and content.php and then I have put following code in single.php

<?php get_header(); ?>

<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">
        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

            <?php if ( 'category_name=projects' ) : ?>

                <?php get_template_part( 'template-parts/project', get_post_format() );
        the_post_navigation();
        ?>
                    <?php else : ?>
                        <?php get_template_part( 'template-parts/content', get_post_format() );
        the_post_navigation();
        if ( comments_open() || get_comments_number() ) :
        comments_template();
        endif;
        ?>
                            <?php endif; ?>
                                <?php endwhile; else : ?>
                                    <p>
                                        <?php _e( 'Sorry, no posts matched your criteria.' ); ?>
                                    </p>
                                    <?php endif; ?>
    </main>
    <!-- #main -->
</div>
<!-- #primary -->

<?php

get_sidebar(); get_footer();

And everything works fine BUT it does not separate categories when I started to edit projects and single php's. The following code is from projects.php

<article id="post-<?php the_ID(); ?>" <?php post_class( ''); ?>>
    <?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );?>
        <header class="entry-header project-header" style="background-image: url('<?php echo $thumb['0'];?>'); height: 80vh;
background-size: cover; background-repeat: no-repeat; background-position: center;">
        </header>
        <div class="entry-content">
            <section class="quater">
                <div class="container-fluid">
                    <h2><?php the_title(); ?></h2>
                    <h3>Published: <?php the_time('jS F Y') ?></h3>
                </div>
            </section>
            <section class="project-content">
                <?php the_content () ?>
            </section>
        </div>
        <footer class="entry-footer">
            <?php fish_entry_footer(); ?>
        </footer>
</article>

So question is - How do I quote certain category for each file?

Upvotes: 1

Views: 67

Answers (3)

Prusakov
Prusakov

Reputation: 347

I think I have worked it out. So... I re-edit single.php to its default and added current code to functions.php

function get_custom_cat_template($single_template) {
     global $post;

       if ( in_category( 'projects' )) {
          $single_template = dirname( __FILE__ ) . '/projects.php';
     }
     return $single_template;
}

add_filter( "single_template", "get_custom_cat_template" ) ;

So now WP calls for projects.php when I browse that category and all the rest category stays by default. Will test it for now.

Upvotes: 0

Jimbo
Jimbo

Reputation: 81

You could try something with wp query- like-

$query = new WP_Query( array( 'category_name' => 'staff' ) );

then for the loop try-

<?php while ( $query->have_posts() ) : $query->the_post(); ?>

Upvotes: 1

Robert Calove
Robert Calove

Reputation: 449

Your PHP if statement looks a bit odd. You aren't using any comparison operators. You're just calling if ('category_name = projects'), which is just calling if on a string (which should return true since the string's 'value' is > 0).

Do you have a variable somewhere that stores the category? If you do, then you need to either do a strcmp() or a == to see which category to display, then display the respective category.

Upvotes: 1

Related Questions