• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

Adding a class to element in page template?

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › General Discussion › Adding a class to element in page template?

This topic is: not resolved

Tagged: class, page template, php

  • This topic has 3 replies, 2 voices, and was last updated 12 years, 11 months ago by rfmeier.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • June 7, 2013 at 1:58 am #44478
    hadhops
    Member

    Hello,

    I have very little knowledge of php and need to add a class to an element so I can style it.

    Basically I want the category name to appear at the top of the page before the content-sidebar div. I've managed to piece together bits and peices to achieve this, however there I need a class on the category.

    Here is my page template code (I've bolded the relevant function and action)-

    <?php
    /*Template Name Posts: Faculty Post*/
    ?>

    <?php

    add_action( 'genesis_meta', 'faculty_post_genesis_meta' );
    /**
    * Add widget support for this template. If no widgets are active, display the default Genesis loop.
    *
    */
    function faculty_post_genesis_meta() {

    if ( is_active_sidebar( 'Sidebar' ) ) {

    remove_action( 'genesis_loop', 'genesis_do_loop' );
    add_action( 'genesis_before_content_sidebar_wrap', 'ducere_the_category' );
    add_action( 'genesis_loop', 'genesis_do_loop' );
    add_filter( 'body_class', 'add_body_class' );

    function add_body_class( $classes ) {
    $classes[] = 'faculty_post';
    return $classes;
    }

    function ducere_the_category() {
    $category = get_the_category();
    echo $category[0]->cat_name;

    }
    }

    }

    genesis();

     

    Thank you for any help 🙂

    http://martinhadley.com/ducere/?p=164
    June 7, 2013 at 5:39 am #44493
    rfmeier
    Member

    Hello,

    I tried a quick example for a single post within my development environment. You can try the code below within your template file.

    Since you are using a template, WordPress should automatically insert a css class into the body element that is a slug of your template.

    Let me know how it turns out.


    Ryan Meier – Twitter

    June 7, 2013 at 5:59 am #44500
    hadhops
    Member

    You nailed it! Thank you so much.

    June 7, 2013 at 6:01 am #44503
    rfmeier
    Member

    You are welcome. Glad I could help.


    Ryan Meier – Twitter

  • Author
    Posts
Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘General Discussion’ is closed to new topics and replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2026 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble