Skip to main content

How to Create and Display Custom Post Types and Taxonomies in WordPress for School House Activities

 If you're building a WordPress website to manage and display school house activities, using Custom Post Types (CPTs) and Taxonomies is the best approach. This guide will walk you through the process step by step, from setting up the CPT to displaying activities on your site.


Step 1: Understanding Custom Post Types and Taxonomies

What are Custom Post Types (CPTs)?

Custom post types allow you to create new types of content in WordPress. By default, WordPress includes posts and pages, but you can create a new post type, such as "Activities," to store your school house events.

What are Custom Taxonomies?

Taxonomies are used to categorize and group posts. WordPress has default taxonomies like "Categories" and "Tags," but you can create custom taxonomies, such as "School Houses" or "Activity Categories."


Step 2: Register a Custom Post Type (CPT) for Activities

You need to register a custom post type called activitie. Add the following code to your functions.php file:

function create_activities_cpt() {
    $args = array(
        'label'  => 'Activities',
        'public' => true,
        'menu_icon' => 'dashicons-admin-post',
        'supports' => array('title', 'editor', 'thumbnail', 'excerpt'),
        'rewrite' => array('slug' => 'activities'),
        'has_archive' => true,
    );
    
    register_post_type('activitie', $args);
}
add_action('init', 'create_activities_cpt');

This creates a new post type Activities in the WordPress admin panel.


Step 3: Register a Custom Taxonomy for School Houses

Now, let's create a custom taxonomy called school_house to categorize activities by house (e.g., Red House, Blue House, Green House).

function create_school_house_taxonomy() {
    $args = array(
        'label'  => 'School Houses',
        'rewrite' => array('slug' => 'school-house'),
        'hierarchical' => true,
    );
    
    register_taxonomy('school_house', 'activitie', $args);
}
add_action('init', 'create_school_house_taxonomy');

Now, you can assign activities to different school houses from the WordPress dashboard.


Step 4: Display Activities on the Frontend

1. Archive Page for Activities

Create a file called archive-activitie.php and add:

<?php get_header(); ?>

<h1>All Activities</h1>

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div class="activity-item">
        <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
        <p><?php the_excerpt(); ?></p>
    </div>
<?php endwhile; else : ?>
    <p>No activities found.</p>
<?php endif; ?>

<?php get_footer(); ?>

This will list all activities.

2. Single Activity Page

Create single-activitie.php to display a single activity's details:

<?php get_header(); ?>

<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<p>Category: <?php the_terms(get_the_ID(), 'school_house'); ?></p>

<?php get_footer(); ?>

3. Category Page for School Houses

Create taxonomy-school_house.php to display activities for a specific house:

<?php get_header(); ?>

<h1>Activities in <?php single_term_title(); ?></h1>

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div class="activity-item">
        <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    </div>
<?php endwhile; else : ?>
    <p>No activities available for this house.</p>
<?php endif; ?>

<?php get_footer(); ?>

Conclusion

By following these steps, you now have:

  • A Custom Post Type (CPT) for Activities

  • A Custom Taxonomy for School Houses

  • Templates to display all activities, single activity pages, and activities for a specific house

With this structure, you can easily manage and categorize school house activities in WordPress. 🚀

Comments

Popular posts from this blog

Digital World

In the modern era, we find ourselves immersed in a digital world. Technology has transformed the way we live, work, communicate, and explore. From the convenience of online shopping to the vast knowledge at our fingertips, the digital realm has become an integral part of our daily lives. In this blog, we will explore the various aspects of the digital world, its impact on society, and the opportunities it presents for individuals and businesses alike.   The Power of Connectivity: The digital world has connected us in ways unimaginable just a few decades ago. Social media platforms enable us to bridge distances and connect with friends, family, and even strangers across the globe. Instant messaging and video calls have made communication seamless, fostering relationships and collaborations beyond borders. This connectivity has brought us closer together and expanded our horizons.   Access to Information and Knowledge: The internet has revolutionized access to info...

Little Digital Dreams: Where Learning Meets Fun – The Ultimate YouTube Channel for Kids!

  In today’s digital age, finding safe, educational, and entertaining content for kids can feel like searching for a needle in a haystack. Enter  Little Digital Dreams  â€“ a YouTube channel designed to spark curiosity, nurture creativity, and make learning an adventure for children! If you’re a parent, guardian, or educator looking for wholesome screen time that combines fun with foundational skills, you’ve just found your go-to resource. Let’s dive into why  Little Digital Dreams  is the channel your child will love—and why you’ll love it too! What Makes  Little Digital Dreams  Special? This channel isn’t just another collection of random cartoons. Every video is crafted with care to ensure kids are  learning, laughing, and growing . Here’s what you’ll find: Enchanting Kids’ Cartoon Videos From magical fairy tales to playful animal adventures, our animated stories are packed with vibrant visuals and relatable characters. These cartoons aren’t just...

Bootstrap 5 Carousel Code

  <!-- Carousel --> < div  id ="demo"  class ="carousel slide"  data-bs-ride ="carousel" >    <!-- Indicators/dots -->    < div  class ="carousel-indicators" >      < button  type ="button"  data-bs-target ="#demo"  data-bs-slide-to ="0"  class ="active" > < /button >      < button  type ="button"  data-bs-target ="#demo"  data-bs-slide-to ="1" > < /button >      < button  type ="button"  data-bs-target ="#demo"  data-bs-slide-to ="2" > < /button >    < /div >    <!-- The slideshow/carousel -->    < div  class ="carousel-inner" >      < div  class ="carousel-item active" >        < img  src ="la.jpg"  alt ="Los Angeles"  class ="d-block w-100" ...