具有不同输出的不同帖子类别

时间:2016-01-09 17:44:29

标签: php wordpress

我在这里遇到了一些问题。到目前为止,我有两个帖子类别:category_name = projects和category _name = blog。所以我希望每个类别的帖子都有不同的风格和不同的输出。 所以我创建了两个文件projects.php和content.php,然后我将下面的代码放在single.php中

<?php get_header(); ?>

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

            <?php if ( 'category_name=projects' ) : ?>

                <?php get_template_part( 'template-parts/project', get_post_format() );
        the_post_navigation();
        ?>
                    <?php else : ?>
                        <?php get_template_part( 'template-parts/content', get_post_format() );
        the_post_navigation();
        if ( comments_open() || get_comments_number() ) :
        comments_template();
        endif;
        ?>
                            <?php endif; ?>
                                <?php endwhile; else : ?>
                                    <p>
                                        <?php _e( 'Sorry, no posts matched your criteria.' ); ?>
                                    </p>
                                    <?php endif; ?>
    </main>
    <!-- #main -->
</div>
<!-- #primary -->

<?php

get_sidebar(); get_footer();

一切正常但是当我开始编辑项目和单个php时,它不会分类。 以下代码来自projects.php

<article id="post-<?php the_ID(); ?>" <?php post_class( ''); ?>>
    <?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );?>
        <header class="entry-header project-header" style="background-image: url('<?php echo $thumb['0'];?>'); height: 80vh;
background-size: cover; background-repeat: no-repeat; background-position: center;">
        </header>
        <div class="entry-content">
            <section class="quater">
                <div class="container-fluid">
                    <h2><?php the_title(); ?></h2>
                    <h3>Published: <?php the_time('jS F Y') ?></h3>
                </div>
            </section>
            <section class="project-content">
                <?php the_content () ?>
            </section>
        </div>
        <footer class="entry-footer">
            <?php fish_entry_footer(); ?>
        </footer>
</article>

所以问题是 - 如何为每个文件引用某个类别?

3 个答案:

答案 0 :(得分:1)

您的PHP if语句看起来有点奇怪。您没有使用任何比较运算符。您只是调用if ('category_name = projects'),它只是在字符串上调用if(由于字符串&#39;值&#39;是&gt; 0,因此应返回true) 。

你有一个存储类别的变量吗?如果您这样做,则需要执行strcmp()==以查看要显示的类别,然后显示相应的类别。

答案 1 :(得分:1)

您可以尝试使用wp查询 - 喜欢 -

$query = new WP_Query( array( 'category_name' => 'staff' ) );

然后循环尝试 -

<?php while ( $query->have_posts() ) : $query->the_post(); ?>

答案 2 :(得分:0)

我想我已经解决了。 所以...我将single.php重新编辑为默认值,并将当前代码添加到functions.php

homestead.rb

现在,当我浏览该类别时,WP会调用projects.php,默认情况下所有其他类别都会保留。 现在将测试它。

相关问题