0

For example, I have post named "My book number 1", that is in "Book" custom post type group. And is another post named "My book number 2", that is too in "Book" custom post type.

Question: How I can set specific design template for "My book number 1" post?

3 Answers 3

0

You can just create a template...call it something like book-one-template.php, the first line after <?php will need to be /* Template Name: Book One Template */. Then, using that and the custom-post-template plugin you can set that as the template for any post you like.

1
  • ru.forums.wordpress.org/topic/… in custom-post-templates.php 71 line $post_types = apply_filters( 'cpt_post_types', array( 'post' ) ); we must add 'book' $post_types = apply_filters( 'cpt_post_types', array( 'post','book' ) );
    – Vitalij
    Jan 31, 2012 at 11:19
1

One way of doing it would be as follows: using the name of the post to determine how it displays.

Firstly, open page.php and "Save As" it to single-book.php.

Secondly, find "the loop", i.e. where your page.php has its while... statement.

You can then use code similar to the following to differentiate styles between different books:

<?php while (have_posts()) : the_post(); ?>

    <?php global $post; ?>

    <?php if ( stristr($post->post_name, "Book 1") ) : ?>
    <?php /* do something for your first book here */

        <div class="post" id="post-<?php the_ID(); ?>">

        <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>

        <div class="entry">

        <?php the_content('Continue reading &raquo;'); ?>

        </div>

    <?php else if ( stristr($post->post_name, "Book 2") ) :  ?>
    <?php /* do something different for your second book */

        <div class="post" id="post-<?php the_ID(); ?>">

        <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>

        <div class="entry">

        <?php the_content('Continue reading &raquo;'); ?>

        </div>

    <?php else : ?>
    <?php /* do something different for the rest of the books... */

        <div class="post" id="post-<?php the_ID(); ?>">

        <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>

        <div class="entry">

        <?php the_content('Continue reading &raquo;'); ?>

        </div>

    <?php endif; ?>

<?php endwhile; ?>

The stristr PHP function checks to see if the second parameter (here string "Book 1") exists in the first parameter (here string $post->post_name). It also differentiates for "Book 2". Obviously you can add as many of those as you like and change the text accordingly.

1
  • Ok, thanks for this snippet of code ;) Not bad solution, at this time it is the best ;)
    – Vitalij
    Jan 30, 2012 at 15:42
0

Maybe this answer is helpful to you:

Created custom post type but selected template not staying selected

notice that you'll have to set foo_resources inside of the custom_template_redirect function to your custom post type

5
  • This not solving my problem, because it applying template for whole custom post group.
    – Vitalij
    Jan 30, 2012 at 14:26
  • I need apply template only for specific post of some custom post type.
    – Vitalij
    Jan 30, 2012 at 14:27
  • Actually it gives you the ability to chose which template to use on the post edit screen..like you may know it from standard "page" post type. You are aware of the way custom page templates work in WordPress? Maybe I just don't understand what you try to archive. Jan 30, 2012 at 15:14
  • Code snippet of "dunc" user, giving me ability to set specific template for "specific post " of some custom post type. (Not for all posts, but for individual one) ;)
    – Vitalij
    Jan 30, 2012 at 16:14
  • When Im trying to work with your code, it applies page template for all posts that are in "Book" custom post type.
    – Vitalij
    Jan 30, 2012 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.