如何分解php代码以避免回声

时间:2013-04-15 作者:730wavy

我用这段php代码列出了所有带有分类类别名称的帖子(标题),它工作得很好,但我想将其分解,避免在php代码中使用echo,以便插入更多代码。这是我的代码--

<?php
$custom_terms = get_terms(\'videoscategory\');
$other_custom_terms = get_terms(\'product_category\');

foreach ($custom_terms as $custom_term) {
foreach ($other_custom_terms as $other_custom_term) {
    wp_reset_query();
    $args = array(\'post_type\' => \'product\', \'orderby\' => \'title\', 
                \'order\' => \'asc\',
        \'tax_query\' => array(
  \'relation\' => \'AND\',
\'orderby\' => \'title\', 
                \'order\' => \'asc\',
    array(
        \'taxonomy\' => \'product_category\',
        \'field\' => \'slug\',
        \'terms\' => $other_custom_term->slug,
\'orderby\' => \'title\', 
                \'order\' => \'asc\',
    ),
            array(
                \'taxonomy\' => \'videoscategory\',
                \'field\' => \'slug\',
                \'terms\' => $custom_term->slug,
\'orderby\' => \'title\', 
                \'order\' => \'asc\',

            ),
        ),
  );

     $loop = new WP_Query($args);
     if($loop->have_posts()) {
        echo \'<h1 style="margin-top:10px;">\'.$custom_term->name.\'</h1>\';

        while($loop->have_posts()) : $loop->the_post();
            echo \'<h2><a href="\'.get_permalink().\'">\'.get_the_title().\'</a></h2>\';
        endwhile;
     }
}
} ?>
这是我现在所拥有的代码,我已经编辑了这些代码,以尝试并完成我想做的事情--

<?php
$custom_terms = get_terms(\'videoscategory\');
$other_custom_terms = get_terms(\'product_category\');

foreach ($custom_terms as $custom_term) {
foreach ($other_custom_terms as $other_custom_term) {
    wp_reset_query();
    $args = array(\'post_type\' => \'product\', \'orderby\' => \'title\', 
                \'order\' => \'asc\',
        \'tax_query\' => array(
  \'relation\' => \'AND\',
\'orderby\' => \'title\', 
                \'order\' => \'asc\',
    array(
        \'taxonomy\' => \'product_category\',
        \'field\' => \'slug\',
        \'terms\' => $other_custom_term->slug,
\'orderby\' => \'title\', 
                \'order\' => \'asc\',
    ),
            array(
                \'taxonomy\' => \'videoscategory\',
                \'field\' => \'slug\',
                \'terms\' => $custom_term->slug,
\'orderby\' => \'title\', 
                \'order\' => \'asc\',

            ),
        ),
     );
}
}
}
     $loop = new WP_Query($args);
     if($loop->have_posts()) : while($loop->have_posts()) : $loop->the_post(); ?>
<div class="box <?php echo $custom_term->slug; ?>">
        <h1 style="font-weight:bold;font-size:18px;margin-top:10px;"><?php echo $custom_term->name ?></h1>
<h2><a href="<?php get_permalink(); ?>"><?php get_the_title(); ?></a></h2>
</div>
       <?php endwhile; ?>
基本上,我想做的是将术语名称或slug作为一个类分配给我的div,这样我就可以使用同位素过滤器按类别过滤帖子列表。然而,我收到了解析/语法错误,而且一切都不正常。我做错了什么?非常感谢您的帮助,谢谢。

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

您有一个额外的“}”,每个循环必须有两个这样的大括号。您还需要有endif;endwhile之后的end语句。分析错误可能是由于额外的“}”。请尝试下面的代码,并让我们知道它是否工作。

 <?php
$custom_terms = get_terms(\'videoscategory\');
$other_custom_terms = get_terms(\'product_category\');

foreach ($custom_terms as $custom_term) {
foreach ($other_custom_terms as $other_custom_term) {
    wp_reset_query();
    $args = array(\'post_type\' => \'product\', \'orderby\' => \'title\', 
                \'order\' => \'asc\',
        \'tax_query\' => array(
  \'relation\' => \'AND\',
\'orderby\' => \'title\', 
                \'order\' => \'asc\',
    array(
        \'taxonomy\' => \'product_category\',
        \'field\' => \'slug\',
        \'terms\' => $other_custom_term->slug,
\'orderby\' => \'title\', 
                \'order\' => \'asc\',
    ),
            array(
                \'taxonomy\' => \'videoscategory\',
                \'field\' => \'slug\',
                \'terms\' => $custom_term->slug,
\'orderby\' => \'title\', 
                \'order\' => \'asc\',

            ),
        ),
  );

     $loop = new WP_Query($args);
     if($loop->have_posts()) 
     {
        ?>
        <div class="box <?php echo $custom_term->slug; ?>">
        <h1 style="margin-top:10px;"><?php echo $custom_term->name;?></h1>

      <?php       
      while($loop->have_posts()) : $loop->the_post(); ?>
            <h2><a href="<?php echo get_permalink() ?>"><?php echo get_the_title(); ?></a></h2>
      <?php  endwhile; ?>
      </div>
     <?php   
     }
}
} ?>

结束

相关推荐

根据情况使用不同的Single.php文件

我正在自定义WordPress网站的主题,我需要使用不同的主题。基于条件的php文件。这是我的用户案例,我需要设计一个竞赛页面,在那里我需要显示人们提交的参赛作品。条目可以属于不同的类别,因此不可能基于类别对其进行跟踪。我已经创建了一个标签(竞赛),每个属于竞赛的参赛作品都会被贴上这个标签,我可以根据这个标签轻松挑选所有参赛作品。在主导航栏上,我放置了一个链接\"Contest\" 这将把用户带到竞赛页面,在此页面上,我显示所有提交的条目,并提供简短描述和链接,以转到故事页面。这是我在自定义竞赛页面上显示