如何在page.php内显示post类别if else语句?

时间:2013-04-12 22:57:33

标签: php wordpress

我设置了几个类别,并希望根据is_page()显示特定类别。

page.php内,我创建了一个if..else语句,用于检查页面名称并打印出特定类别。我现在的问题是,不是只打印the_title,而是打印整个帖子。

我在哪里错了?

<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php if ( is_page( 'Greywater Recycling' ) ) { ?>
<div class="col">
    <?php query_posts( 'category_name=Greywater Recycling&posts_per_page=5'); if (have_posts()) : while (have_posts()) : the_post(); ?>
     <h2><?php the_title(); ?></h2>

    <?php endwhile; endif; ?>
</div>
<?php } else if ( is_page( 'Stormwater Management' ) ) { ?>
<div class="col">
    <?php query_posts( 'category_name=Stormwater Management&posts_per_page=5'); if (have_posts()) : while (have_posts()) : the_post(); ?>
     <h2><?php the_title(); ?></h2>

    <?php endwhile; endif; ?>
</div>
<?php } else if ( is_page( 'Rainwater Harvesting' ) ) { ?>
<div class="col">
    <?php query_posts( 'category_name=Rainwater Harvesting&posts_per_page=5'); if (have_posts()) : while (have_posts()) : the_post(); ?>
     <h2><?php the_title(); ?></h2>

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

2 个答案:

答案 0 :(得分:1)

您的代码存在许多问题。例如,循环中的 is_page does not work

其次,不要混淆query_postsWhen should you use WP_Query vs query_posts() vs get_posts()?。真的,忘掉它用于二次循环。

您的代码可以简化为以下内容。在functions.php中,我们删除一个函数以通过其名称获取类别ID。另一个使用ID进行二次循环。然后,在page.php中对这些函数进行简单调用。

文档:Class_Reference/WP_Query

page.php

请注意,您不需要在每一行打开PHP标记,这使代码难以阅读。
仅用于在PHP和HTML之间交换

<?php 
get_template_part( 'content', 'page' ); 

// This page title
$the_title = get_the_title(); 

// Search for category with the same name of the page
$category = brsfl_get_category_id( $the_title );

// Category found, go to the secondary loop
if( $category ) {
    brsfl_check_print_categories( $category );
}

functions.php

始终在功能名称前加上一些独特的内容,以避免可能导致网站崩溃的冲突。

/**
 * Get the category ID based on its name
 */ 
function brsfl_get_category_id( $cat_name )
{
    $term = get_term_by( 'name', $cat_name, 'category' );

    // term found, return its ID
    if( $term ) {
        return $term->term_id;
    }

    // not found
    return false;
}

/**
 * Print a loop based on a category ID
 */     
function brsfl_check_print_categories( $cat_id )
{
    $args = array(
        'post_type' => 'post',
        'cat' => $cat_id,
        'posts_per_page' => 5
    );
    $the_query = new WP_Query( $args );

    if ( $the_query->have_posts() ) 
    { 
        while ( $the_query->have_posts() ) :
            $the_query->the_post();
            echo '<h2>' . get_the_title() . '</h2>';
        endwhile;
    } 
    else
    {
        echo 'No posts found...';
    } 
}

虽然我已在建议的范围内回答,但我认为有更好的解决方案,例如:

  • a shortcode,只需调整功能即可。

  • 使用Advanced Custom Fields显示一个元框,您可以在其中选择一个非常特定的类别(不依赖于页面和类别名称)并仅使用WP_Query函数将其打印出来在模板中。

答案 1 :(得分:0)

我认为通过slug名称获取类别更好。 我已经尝试过你的解决方案,它工作正常,但在我的类别中有一个名称带有特殊的html字符,如撇号或引号,但它没有。

这是从您的解决方案编辑的代码片段:

你的functions.php中的

    function brsfl_get_category_id($cat_name){
      $term = get_term_by( 'slug', $cat_name, 'category' );
      if( $term ) {
        return $term->term_id;
      }
      return false;
    }
你的page.php中的

    $the_slug = get_post(get_the_ID())->post_name;
    $category = brsfl_get_category_id( $the_slug );
    if( $category ) {
      brsfl_check_print_categories( $category );
    }
相关问题