Community Forums › Forums › Archived Forums › Design Tips and Tricks › How to add custom post type different styling on single posts?
Tagged: child theme, custom design, post format design, Post Formats
- This topic has 4 replies, 2 voices, and was last updated 11 years ago by Henrik Blomgren.
-
AuthorPosts
-
January 5, 2014 at 11:27 pm #83515Henrik BlomgrenMember
Hello again. I´ve dabbled in some more wp coding. When I previously did my wordpress themes I separated how my different post types would appear on the front page and in the archives. It is simple, all I´ve placed for code in my index.php has been the following:
<?php if ( have_posts() ) : ?> <?php /* The loop */ ?> <?php while ( have_posts() ) : the_post(); ?> <?php get_template_part( 'content', get_post_format() ); ?> <?php endwhile; ?> <?php hb4_paging_nav(); ?> <?php else : ?> <?php get_template_part( 'content', 'none' ); ?> <?php endif; ?>
This makes it easy to customize how every post type is displayed. You can see how I´ve styled it at the two projects that use them at http://deculture.se and http://henrikblomgren.se. Now I want to transfer this into Genesis and I´m a bit lost at how to do this. I tried doing it like this as I found a piece of interesting code here:
https://gist.github.com/billerickson/3218052But instead of using it as it is I modified it into
function be_custom_loop() { global $post; // arguments, adjust as needed $args = array( 'post_type' => 'post', 'paged' => get_query_var( 'paged' ) ); /* Overwrite $wp_query with our new query. The only reason we're doing this is so the pagination functions work, since they use $wp_query. If pagination wasn't an issue, use: https://gist.github.com/3218106 */ global $wp_query; $wp_query = new WP_Query( $args ); if ( have_posts() ) : while ( have_posts() ) : the_post(); get_template_part( 'content', get_post_format() ); endwhile; do_action( 'genesis_after_endwhile' ); endif; wp_reset_query(); } add_action( 'genesis_loop', 'be_custom_loop' ); remove_action( 'genesis_loop', 'genesis_do_loop' );
Can I do this for single posts as well? Do I just create a single.php file and type in just about the same or how do I fix this? Since I have some posts that are of the format image and I do not want to show a thumbnail on these posts when people are visiting the single post of a post for example http://henrikblomgren.se/dagens-bild-iris-heart/ when used with my current coding of a child theme to genesis it will look like this:
http://demo.henrikblomgren.se/dagens-bild-iris-heart/ for example. The image chosen as the featured image there is not cropped or anything, just a random image I choose to demonstrate how weirdly it looks.Which is kinda bleh if you ask me. Is there any easy way for me to remove the featured image on single posts when the post itself is from the post format "image"? Or do I have to make some complicated function?
My own knowledge is telling me to make it simple. Make a single.php, grab the get_template_part( 'single-content', get_post_format() ); and have a single-content-image.php which says no, no featured image is shown here.
Is it possible to do something like this on Genesis? I have tried google and I have tried searching the documentation and all but sadly I haven´t found anything of this sort.
Sorry for coming here as a new user and only asking weird questions.
January 6, 2014 at 8:10 am #83577Brad DaltonParticipantHi
To answer your question about styling single custom post types, you can add a custom body class to the single.php file or conditionally from your child themes functions file
Looks like this:
//* Add portfolio body class to the head add_filter( 'body_class', 'executive_add_portfolio_body_class' ); function executive_add_portfolio_body_class( $classes ) { $classes[] = 'executive-pro-portfolio'; return $classes; }
So you filter the default single class and generate your own class which only effects the template.
Otherwise, you add a conditional statement after the function to generate the new class from your functions file.
More http://codex.wordpress.org/Function_Reference/body_class
<?php /** * This file adds the custom portfolio post type single post template to the Executive Pro Theme. * * @author StudioPress * @package Executive Pro * @subpackage Customizations */ add_filter( 'body_class', 'add_body_class' ); function executive_add_body_class( $classes ) { $classes[] = 'single-cpt-class'; return $classes; } //* Remove the post image (requires HTML5 theme support) remove_action( 'genesis_entry_content', 'genesis_do_post_image', 8 ); genesis();
Use the code snippets to modify your single cpt template file http://my.studiopress.com/snippets/entry-content/
Or you can conditionally remove them from your functions file if using a native single template.
Name the file single-your-cpt.php
January 6, 2014 at 8:38 am #83583Henrik BlomgrenMemberThank you Brad for answering. When I get around adding my portfolio custom post types I will use the info you´ve written now.
It does however not answer my question.
As it is Genesis only have 1 way of displaying single posts regardless of post format. I write standard, video, image and asides on my personal blog. When people visit let´s say the link I mentioned above(http://henrikblomgren.se/dagens-bild-iris-heart/) I have styled it as such that the single view of the post format "image" does not have a fetured image shown at the top. Genesis on the other hand if I place code to display the featured image on single posts will always show a featured image, regardless of if the post format is a standard, aside, video or image.
My question was: Is it possible to make Genesis load a different template file for displaying how different post formats look when viewing them at the single post view?
If you take notice on how this link looks: http://henrikblomgren.se/wordpress-3-8-parker-ute-nu/ this is a post of the post format "aside". As you see it does not show a post title or featured image. This is thanks to me loading a special template file called content-aside which describes how a post being in the post format "aside" should look.
I want this functionality in Genesis. Is this possible or is it impossible to have the single.php load different templates depending on what the post format is?
I noticed I posted this as "custom post type" in the title of this topic. I beg for your forgivness as it should have said "post format" and not "custom post type". I will post another subject of the featured image as your code is not what I´m looking for.
January 6, 2014 at 11:42 am #83610Brad DaltonParticipantYou can conditionally style post different posts by post formats by filtering the body class but really, the post format body class can be used as well if you can see it in the source code.
What theme are you using?
January 6, 2014 at 2:13 pm #83640Henrik BlomgrenMemberHello Brad, thanks for answering.
I´m not using any theme since I´m creating my own child theme since there really wasn´t one theme that had all the functions and looked the way I wanted it to so I only got the Genesis framework and then started on a own child theme.
You can see how far I´ve gotten here: http://demo.henrikblomgren.se
So I wanted to see how far I could get without asking. But using hooks at this degree is hard when it´s the first time you´re doing it. Creating themes and sending people to different template files to display content based on format or category or whatever is easy when doing it normally, but with hooks and Genesis I have no real clue.
Yes I can see what format that the post is by checking in the body class. single-format-aside is the body class for the aside format for example. When it´s an image it outputs single-format-image
So is there any easy way of doing this with Genesis based on the information you have received? What hook/function have I missed for using this?
Thank you once again for the help.
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.