1

我已经设置了几个类别,并希望显示基于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. ?>
4

2 回答 2

1

你的代码有很多问题。一方面,在循环内不起作用。is_page

其次,不要混淆query_posts什么时候应该使用 WP_Query、query_posts() 和 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...';
    } 
}

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

  • 一个简码,只需调整功能。

  • 使用高级自定义字段显示一个元框,您可以在其中选择一个非常具体的类别(不要依赖页面和类别名称)并仅使用该WP_Query功能在模板中将其打印出来。

于 2013-04-13T02:01:02.180 回答
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 );
    }
于 2014-04-04T15:56:50.670 回答