Single.php失去了格式化功能

时间:2013-03-14 作者:Hugo

我需要将类别名称用作H1的类,还需要排除类别2。所以我把下面的PHP代码弄糟了。它工作得很好,只是帖子的格式松动了。我知道get_post_format() 是应用格式的,但当我添加它时,它似乎没有任何作用。任何帮助都将不胜感激,谢谢。

好的,这是整个单曲。php,很抱歉,如果我不清楚我在问什么,我是Wordpress的新手。我试图实现的是将单个帖子的标题设为两个类:
1:。类别标题2:。标题-“该类别的鼻涕虫”例如。标题特色

<?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(); ?>

1 个回复
最合适的回答,由SO网友:David Gard 整理而成

好的,试试这个(未经测试,请原谅任何小的打字错误/语法错误)。

我已经修改,以便循环首先检查是否有要显示的帖子。我也不再需要$count 以及添加\' \' 通过向数组中添加任何匹配项,然后使用join().

希望我没有走错路,它会适合你的需要。

<?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(); ?>

结束

相关推荐

Query_Posts和自定义分类发布顺序

我有一个为“项目”定义的自定义分类法和一个显示所有项目的页面。在这个页面中有一个jquery过滤菜单(同位素)。最初,页面必须显示所有项目,但按时间顺序(“按时间顺序”是一个类别,1970、1980、1990等是项目元素的子类别)按DESC模式排序。现在,最初在页面中显示项目的查询是query_posts(\'post_type=portfolio&project-type=\'.get_post_meta($post->ID, \'portfolio_page_cats\', true).