• Hallo everyone,

    I’m working on a website to wich I want to display three of the categories on the frontpage.

    This is the code I’m using:

    
    					<?php 
                            $categories = get_categories( array(
    							'orderby' => 'name',
    							'hide_empty' => false,
                                'parent' => 0
                            ) );
                        ?>
                        
    					<?php foreach ( $categories as $category ) : ?>
    						<div class="grid-3">
    							<div class="intro_linkToCategory">
                                    <a href="<?php echo esc_url( get_category_link( $category -> term_id ) ); ?>">
                                        <img src="<?php echo z_taxonomy_image_url( $category -> term_id ); ?>" />
                                        <h2><?php echo $category->name; ?></h2>
                                    </a>
    							</div><!-- end of intro_linkToCategory -->                            	
    						</div><!--  end of grid -->
    					<?php endforeach; ?>
    

    This code, however, displays all the avaliable categories. I want to limit get_categories() to show a limited amount of categories by category ID. I might have searched on the wrong places but I couldn’t find a solution to do this. I don’t want to show the posts of a category but just list the category name and image (using a plugin for the image) and link it to the correct category page.

    I’d like a point into the right direction or an example of working code that will point me on the right direction. I have a deadline of April 1st ( not a joke ) on this one and I’m starting to get nervous…

    Thanks in advance,
    Kind regards.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hello
    please check this code

    
    				<?php 
                            $categories = get_categories( array(
    							'orderby' => 'name',
    							'hide_empty' => false,
                                'parent' => 0
                            ) );
                        ?>
                        
    					<?php
                        $limit=3; // Set limit here
                        $counter=0;
                        foreach ( $categories as $category ) {
                        if($counter<$limit){?>
    						<div class="grid-3">
    							<div class="intro_linkToCategory">
                                    <a href="<?php echo esc_url( get_category_link( $category -> term_id ) ); ?>">
                                        <img src="<?php echo z_taxonomy_image_url( $category -> term_id ); ?>" />
                                        <h2><?php echo $category->name; ?></h2>
                                    </a>
    							</div><!-- end of intro_linkToCategory -->                            	
    						</div><!--  end of grid -->
    					<?php $counter++;
                        }
                       } ?>
    
    Thread Starter vascofmdc

    (@vascof)

    Hi Sajid

    Thank you for your answer. I have checked your code example. I’m looking for a way to be able to input the category id of the categories I want to display. Normaly with posts one can add the post id’s into the arguments array i’m looking for a simmilar thing, maybe with a different code approach.

    Thank you for taking time to answer my question nonetheless.

    kind regards,
    Vasco.

    Thread Starter vascofmdc

    (@vascof)

    Hi Michael,

    thank you for your imput. It solved my problem. Funny thing is I had scanned trhough that page before but missed the point completely. This is my endcode for posterity:

    
    <?php 
        $categories = get_categories( array(
            'include'=> array(78, 72, 76),
            'orderby' => 'name',
    	'hide_empty' => false,
            'parent' => 0
        ) );
    ?>
                        
    <?php foreach ( $categories as $category ) : ?>
        <div class="grid">
    	<div class="intro_linkToCategory">
                <a href="<?php echo esc_url( get_category_link( $category -> term_id ) ); ?>">
                    <img src="<?php echo z_taxonomy_image_url( $category -> term_id ); ?>" />
                    <h2><?php echo $category->name; ?></h2>
                </a>
            </div><!-- end of intro_linkToCategory -->                            	
        </div><!--  end of grid -->
    <?php endforeach; ?>
    

    the grid reference is part of amazium grid system wich I use for responsiveness.

    once again, thank you all for the imput.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘limit get_categories() to only three categories’ is closed to new replies.