user3547049
user3547049

Reputation: 1

Can someone help me to add a category to this Post?

I'm using this code in different sections of my website and I want to display different posts in each section of the front page according to their category. Can someone please help em to add a category there one is "surf" , I've tried everything.

Or maybe a different way to do it?

Thank you.

    <div class="posts">

        <?php
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $total_post_count = wp_count_posts();
        $published_post_count = $total_post_count->publish;
        $total_pages = ceil( $published_post_count / $posts_per_page );

        if ( "1" < $paged ) : ?>

            <div class="page-title section small-padding">

                <h4 class="section-inner"><?php printf( __('Page %s of %s', 'radcliffe'), $paged, $wp_query->max_num_pages ); ?></h4>

            </div>

            <div class="clear"></div>

        <?php endif; ?>

            <?php while (have_posts()) : the_post(); ?>

                <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

                    <?php get_template_part( 'content', get_post_format() ); ?>


                </div> <!-- /post -->

            <?php endwhile; ?>

        <?php if ( $wp_query->max_num_pages > 1 ) : ?>

            <div class="archive-nav">

                <?php echo get_next_posts_link( '&laquo; ' . __('Posts Antigos', 'radcliffe')); ?>

                <?php echo get_previous_posts_link( __('Posts Recentes', 'radcliffe') . ' &raquo;'); ?>

                <div class="clear"></div>

            </div> <!-- /post-nav archive-nav -->

        <?php endif; ?>

    <?php endif; ?>

</div>     </div>  <!-- /posts -->

Upvotes: 0

Views: 67

Answers (1)

Prabin Parajuli
Prabin Parajuli

Reputation: 559

If you are trying to display category by ID , Then

            global $post;
            $args = array( 'category' => '12' );
            $cat_post= get_posts( $args );
            foreach( $cat_post as $post ) :  setup_postdata($post); ?>
                <li class="testimonial"><?php the_content(); ?></li><br/>
            <?php endforeach; ?>

Note: In $args = array( 'category' => '12' ); 12 is the ID of the category

But if you want to display category by Name, Then

            global $post;
            $args = array( 'category_name' => 'uncatogerized' );
            $cat_post= get_posts( $args );
            foreach( $cat_post as $post ) :  setup_postdata($post); ?>
                <li class="testimonial"><?php the_content(); ?></li><br/>
            <?php endforeach; ?>

Here, uncategorized is a category name

Upvotes: 1

Related Questions