top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

How to display all posts based on category in Wordpress?

0 votes
30 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 Google+ Share Button LinkedIn Share Button Multiple Social 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?

0 votes

I am currently working on my wordpress site and have started to use a timeline. There are next/previous buttons in the post, but I want them to display the post title instead of Next/Previous. I have looked over the wordpress code for this, but the dev is using non-standard code to achieve it.

Could somebody take a look at the code and tell me what I need to change.

<div class="clearfix"></div>
            </div>
            <div class="timeline-info">
                <div class="timeline-content">
                    <?php 
                    $content =  preg_replace ('#<embed(.*?)>(.*)#is', ' ', get_the_content(),1);
                    $content =  preg_replace ('@<iframe[^>]*?>.*?</iframe>@siu', ' ', $content,1);
                    $content =  preg_replace ('/<source\s+(.+?)>/i', ' ', $content,1);
                    $content =  preg_replace ('/\<object(.*)\<\/object\>/is', ' ', $content,1);
                    $content =  preg_replace ('#\[video\s*.*?\]#s', ' ', $content,1);
                    $content =  preg_replace ('#\[audio\s*.*?\]#s', ' ', $content,1);
                    $content =  preg_replace ('#\[/audio]#s', ' ', $content,1);
                    preg_match_all('#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#', $content, $match);
                    foreach ($match[0] as $amatch) {
                        if(strpos($amatch,'soundcloud.com') !== false){
                            $content = str_replace($amatch, '', $content);
                        }elseif(strpos($amatch,'youtube.com') !== false){
                            $content = str_replace($amatch, '', $content);
                        }
                    }
                    $content = preg_replace('%<object.+?</object>%is', '', $content,1);
                    echo apply_filters('the_content',$content);?>
                </div>
            </div>
            <?php 
            $we_sevent_navi = get_option('wpex_navi');
            if($we_sevent_navi!='no'){
                $wpex_navi_order = get_option('wpex_navi_order');
                $preevtrsl = get_option('wpex_text_prev')!='' ? get_option('wpex_text_prev') : esc_html__('Previous article','wp-timeline');
                $nextevtrsl = get_option('wpex_text_next')!='' ? get_option('wpex_text_next') : esc_html__('Next article','wp-timeline');
                if($wpex_navi_order!='ct_order'){ ?>
                    <div class="timeline-navigation defa">
                        <div class="next-timeline">
                            <?php next_post_link('%link', $nextevtrsl) ?>
                        </div>
                        <div class="previous-timeline">
                            <?php previous_post_link('%link', $preevtrsl) ?>
                        </div>
                    </div>
                    <?php 
                }else{
                    wpex_next_previous_timeline($preevtrsl,$nextevtrsl);
                }
            }?>
            <div class="clearfix"></div>
        </div>
  </div>
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...