【问题标题】:Different post category with different output不同的帖子类别具有不同的输出
【发布时间】:2016-01-09 17:44:29
【问题描述】:

这里有个小问题。到目前为止,我有两个帖子类别: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>

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

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    您的 PHP if 语句看起来有点奇怪。您没有使用任何比较运算符。你只是在调用if ('category_name = projects'),它只是在一个字符串上调用if(它应该返回true,因为字符串的'值'> 0)。

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

    【讨论】:

    • 没有存储任何变量。所以我需要写不同的single.php :(
    【解决方案2】:

    您可以尝试使用 wp query- 喜欢-

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

    然后for循环尝试-

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

    【讨论】:

      【解决方案3】:

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

      function get_custom_cat_template($single_template) {
           global $post;
      
             if ( in_category( 'projects' )) {
                $single_template = dirname( __FILE__ ) . '/projects.php';
           }
           return $single_template;
      }
      
      add_filter( "single_template", "get_custom_cat_template" ) ;
      

      所以现在 WP 在我浏览该类别时调用 projects.php,而所有其余类别都默认保留。 现在将对其进行测试。

      【讨论】:

        猜你喜欢
        • 2015-12-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多