top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to display all posts based on category in Wordpress?

0 votes
429 views

I have 4 categories. For each category I will show 5 recent post. I will have category such as breakfast, dessert, lunch and savory food. There will be a "See All" link for each category, so when the user click on "see All" link, can see all post of the particular category. Currently my code looks like this, but I am stuck with the "See All" link. I don't know how to link it to main category.

<?php 
get_header();
?> 
<!-- recipe -->
<section class="recipe-wrap">
    <?php
    /*
     * Loop through Categories and Display Posts within
     */
    $post_type = 'recipe';
    $category_link = get_category_link($cat->cat_ID);

    // Get all the taxonomies for this post type
    $taxonomies = get_object_taxonomies( array( 'post_type' => $post_type ) );

    foreach( $taxonomies as $taxonomy ) :

        // Gets every "category" (term) in this taxonomy to get the respective posts
        $terms = get_terms( $taxonomy );

        foreach( $terms as $term ) : ?>

    <div class="recipe-category owl-carousel-slide">
        <div class="row">

            <h2><?php echo $term->name; ?><a href="#">see all</a></h2>

            <div class="recipe-category-carousel owl-carousel owl-theme">

                <?php
                $args = array(
                        'post_type' => $post_type,
                        'posts_per_page' => 10,  //show all posts
                        'tax_query' => array(
                            array(
                                'taxonomy' => $taxonomy,
                                'field' => 'slug',
                                'terms' => $term->slug,
                            )
                        )
                    );
                $posts = new WP_Query($args);

                if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); ?>

                <div class="item recipe-box">
                    <a href="<?php the_permalink(); ?>">
                        <img src="<?php echo(types_render_field('artwork', array('raw' => true) )); ?>">
                        <p><?php the_title(); ?></p>
                    </a>
                </div> 

                <?php endwhile; endif; ?>
                    </div>
                    </section>
                <?php endforeach;
            endforeach; ?>
            </div>
        </div>
    </div>
</section>
<!-- /recipe -->
<?php 
get_footer();
?>
posted Jun 2, 2017 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

You can try this below code. Using this method may you get all post

<div id="mini_stream">
    <ul>
<? $args = array(
    'post_type' => 'post',
    'posts_per_page' => 4,
    'category_name'=>'Product',
);
$loop = new wp_Query($args); 
while($loop->have_posts()) : $loop->the_post();
    echo '<a href="'.get_permalink().'">';
    echo get_the_post_thumbnail($post->ID, 'category-thumb');
    the_title( '<h6>', '</h6>' );
    echo '</a>';
endwhile;
wp_reset_query(); ?>
    </ul>
</div>
answer Jun 20, 2017 by Santosh Nandi
Similar Questions
0 votes

I want to display all the existing categories in my Wordpress page in the bottom of the content and the selected category will be in bold style, as follows:

For example, for an existing post if I selected category2 of 3 existing, then it show like this

category1 category2 category3

<div class="entry-meta">
<span class="term-links">
<?php foreach ( get_the_terms( $post->ID, 'category') as $term ) : 
?>
<a href="<?php echo esc_url( get_term_link( $term->term_id ) ) 
?>"><span class="<?php echo $term->slug ?>"><?php echo $term->name ?>
</span></a>
<?php endforeach; ?>
</span>

<style>
.term-links .category2 {
display: inline-block;
font-weight:bold;
</style>

In the above code only display the selected category, not showing the all category. How can I do this?

0 votes

I want to create a shortcode for category title in wordpress to display the category name, and I found this code:

function categories_list_func( $atts ){
 $categories = get_the_category();

     if($categories) {
        foreach($categories as $category) {
            $output .= '<li class="cat-' . $category->cat_ID . '"><a href="'.get_category_link( $category->term_id ).'" title="' . esc_attr( sprintf( __( "Read more posts from : %s" ), $category->name ) ) . '">'.$category->cat_name.'</a></li>';
        }
        $second_output = trim($output);
      }
      $return_string = '<h4>'.__( "Categories :", "my_site").'</h4><div class="overflow"><ul class="post-categories">' . $second_output . '</ul></div>';

 return $return_string;

} // END Categories
add_shortcode( 'categories-list', 'categories_list_func' );

Can anybody help me how I create this shortcode?

0 votes

I try to get wordpress list categories with count at the end of each category's name by input the parent category as output.

Ex: I have a parent category name "alpha", and its child categories name are, Category A, Category B, Category C, Category D

I want the output display:

-Category A (5)

-Category B (2)

-Category C (6)

-Category D (7)

<?php
    $variable = wp_list_categories( array(
    'show_count' => true,
    'orderby'    => 'name',
    'style'      => 'none'
    ) );
    echo $variable; 
?>

The result of the above code display categories with the count of posts at the end, but it display all categories. Can anybody help me how I fix this problem?

...