• how can I display a category by divs dynamically? The one’s I’m using right now is manually put the id for each of the categories. Is there a way I can query the category.

    Also I’m having trouble generating a dynamic link for the each section. The code i used is simply for 1 link only I don’t know how to loop the code so it can generate unique link for every category.

    Code I’m using right now is this.

    <?php
        // Get the ID of a given category
    	$category_id1 = get_cat_ID( 'category name 1' );
    
        // Get the URL of this category
        $category_link = get_category_link( $category_id );
    ?>
    
    <a href="<?php echo esc_url( $category_link ); ?>" title="category name 1">
    	<?php echo get_cat_name(6);?>
    	<?php echo category_description(6); ?>
    </a>
    
    <a href="<?php echo esc_url( $category_link ); ?>" title="category name 1">
    	<?php echo get_cat_name(10);?>
    	<?php echo category_description(10); ?>
    </a>

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    Use get_categories to get a list of categories. You can then use a foreach to loop through them and do what you have above.

    https://developer.wordpress.org/reference/functions/get_categories/

    Hello michaelpon12,
    you can use following code to get all categories name and Url :-

    <?php $categories = get_categories();
    foreach ($categories as $cat) {
     $category_link = get_category_link($cat->cat_ID);
     $categoryName = get_cat_name($cat->cat_ID);
     ?>
      '<a href="<?php echo esc_url( $category_link ); ?>" title="Category Name"><?php echo $categoryName; ?></a>';
    
    <?php
    }
    ?>

    Thanks hope this will help you.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘display categories details by divs’ is closed to new replies.