Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I needed to use the category name as a class for the H1, and also needed to exclude category 2. So I botched together the PHP code below. It works fine except the post looses it's formatting. I know that get_post_format() is what applies the format, but when I add it, it doesn't seem to do anything. Any help would be appreciated, thanks.

Ok here is the entire Single.php, sorry if i'm not clear on what I was asking I'm a bit of a Wordpress newbie. What I'm trying to achieve is to have the title of the single post to have 2 classes:
1: .category-heading
2: .heading-"the slug of the category" eg .heading-featured

<?php
/**
 * The Template for displaying all single posts.
 *
 * @package WordPress
 * @subpackage WDM
 * @since WDM 1.0
 */

get_header(); ?>


    <div id="primary" class="site-content">
        <div id="content" class="clear" role="main">



            <?php while ( have_posts() ) : the_post();
            $excludedcats = array(2);
            $count = 0;
            $categories = get_the_category();
            foreach($categories as $category) {
                $count++;
                if ( !in_array($category->cat_ID, $excludedcats) ) {
                    echo '<h1 class="category-heading ' . sprintf( __( "heading-%s" ), $category->slug ) . '" >';

                    if( $count != count($categories) ){
                        echo " ";
                    }

                }
                } 
                single_post_title();
                echo '</h1>';
                the_content(); 
                ?>


                <div class="related-posts-div">

                <?php wp_related_posts()?> 
                </div>

                <nav class="nav-single">
                    <h3 class="assistive-text"><?php _e( 'Post navigation', 'wdm' ); ?></h3>
                    <span class="nav-previous"><?php previous_post_link( '%link', '<span class="meta-nav">' . _x( '&larr;', 'Previous post link', 'wdm' ) . '</span> %title' ); ?></span>
                    <span class="nav-next"><?php next_post_link( '%link', '%title <span class="meta-nav">' . _x( '&rarr;', 'Next post link', 'wdm' ) . '</span>' ); ?></span>
                </nav><!-- .nav-single -->



                <?php comments_template( '', true ); ?>

            <?php endwhile; // end of the loop. ?>

        </div><!-- #content -->
    </div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>
share|improve this question
    
what was the code of single.php before your edits? did it have a div with post_class()? –  Michael Mar 14 '13 at 12:00
    
I suspect you have broken markup. Why is the opening <h1> tag inside a foreach and then inside and if, while the only closing h1 is not? –  helgatheviking Mar 14 '13 at 12:31
    
I'm a little confused here. Single.php is for looping through single posts, but you seem to be outputting a title for each category of each post (providing it is not category 2). You are also closing <\h1> outside of the if statement, so you are adding that to your markup whether or not it is needed. Also, you are checking that a category ID is in the $excludedcats array, but you can exclude them when making the $categories object if you wish. Could you perhaps edit your question to explain your needs better? Also, it appears you haven't added all of single.php - please add it all. –  David Gard Mar 14 '13 at 12:49
    
Thanks for the update. Just to clarify - you want posts that are in category 2 included, but just not listed under class=""? –  David Gard Mar 14 '13 at 13:45
    
Yes that's correct, thank you –  Hugo Mar 14 '13 at 13:48
add comment

1 Answer 1

up vote 0 down vote accepted

Ok, try this (untested, so excuse any minor typos/syntax errors).

I have amended so that The Loop first checks that there is Posts to show. I've also removed the need for $count and the addition of ' ' by adding any matches to an array, and then outputting them using join().

Hopefully I've not gone down the incorrect route and it will be suitable for your needs.

<?php
/**
 * The Template for displaying all single posts.
 *
 * @package WordPress
 * @subpackage WDM
 * @since WDM 1.0
 */

get_header();
?>

    <div id="primary" class="site-content">
        <div id="content" class="clear" role="main">
<?php
            if(have_posts()) : while(have_posts()) : the_post();

                    $excludedcats = array(3);
                    $categories = get_the_category();

                    $title_cats = array(); // Initialise the arry to avoid errors when checking it

                    foreach($categories as $category) :

                        /** Check to see if the category needs to be added to the class="" of the title */
                        if(!in_array($category->cat_ID, $excludedcats)) :
                            $title_cats[] = 'heading-'.$category->slug;
                        endif;

                    endforeach;

                    /** Output the title. We are checking that there were actually category headings here as well, so that class="" is properly formatted */
                    echo '<h1 class="category-heading'.
                        sprintf(__('%1$s'),
                            /** %1$s */ (!empty($title_cats)) ? ' '.join(' ', $title_cats) : ''
                        ). // end sprintf()
                    '">'; // end <h1>
                    single_post_title();
                    echo '</h1>';

                    /** Output the content */
                    the_content(); 
?>
                    <div class="related-posts-div">
                        <?php //wp_related_posts()?>
                    </div>

                    <nav class="nav-single">
                        <h3 class="assistive-text"><?php _e( 'Post navigation', 'wdm' ); ?></h3>
                        <span class="nav-previous"><?php previous_post_link( '%link', '<span class="meta-nav">' . _x( '&larr;', 'Previous post link', 'wdm' ) . '</span> %title' ); ?></span>
                        <span class="nav-next"><?php next_post_link( '%link', '%title <span class="meta-nav">' . _x( '&rarr;', 'Next post link', 'wdm' ) . '</span>' ); ?></span>
                    </nav><!-- .nav-single -->

                    <?php comments_template( '', true ); ?>

                <?php endwhile; ?>
            <?php endif; ?>

        </div><!-- #content -->
    </div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>
share|improve this answer
    
I just get a blank screen :( –  Hugo Mar 14 '13 at 14:10
    
No errors? Ok, I have a new blog I'm just setting up, will do some debugging and update... –  David Gard Mar 14 '13 at 14:14
    
Thank you very much for taking time to help me –  Hugo Mar 14 '13 at 14:18
    
Ok, so I've tested and found a missing '"' was the issue. I've also commented out wp_related_posts();, as it's not a native WP function. Presumably you have a plugin for displaying related posts? –  David Gard Mar 14 '13 at 14:25
    
Have also just corrected another small error, hopefully this is what you were after? –  David Gard Mar 14 '13 at 14:35
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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