user1354409
user1354409

Reputation: 33

Wordpress: Individual Styling/Coding for Custom Post Types on Index

I'm really struggling to Google my way out of this issue. I've set-up custom post-types, and I've managed to include those custom types in the index page. But now I really need a way of styling those entries differently to the regular posts. Does anyone know how I can do this? It's not just styling, but also, replacing/adding/removing certain code, as the custom types operate/look differently to regular posts.

Any help would be HUGELY appreciated!

Upvotes: 2

Views: 802

Answers (2)

Alex Lane
Alex Lane

Reputation: 206

As for the CSS, inside your loop, when you're defining the element that contains your post, try doing something like this

<article class="<?php echo $post->post_type; ?>" ...>

This will give you a class you can hook onto within your CSS file.

You can also use WordPress's built-in post_class function to achieve a similar result

<article <?php post_class($post->post_type); ?>>

That will add the post type to the rest of the default post classes, so you can still style those elements differently.

As for the different code for different post types, one of the answers above me mentioned using an if statement based on the post type:

if ('my_post_type' == $post->post_type)
{
    // Code Here
}

I hope this helps you.

Codex Reference for post_class

Upvotes: 2

Oterox
Oterox

Reputation: 658

you can use this code inside the loop:

if ( 'custom_post_type' == get_post_type() ){
   //do whatever
}

or if you need to check some post types you can use a switch like:

switch ( get_post_type() ) {
  case 'post':
    //do whatever;
    break;
  case 'custom_post_type_1':
    //do whatever;
    break;
  case 'custom_post_type_2':
    //do whatever;
    break;
}       

Upvotes: 0

Related Questions