1

尝试在 wordpress 循环中实现此 HTML 结构时遇到一些问题。

<ul class="dropdown">
    <li>Normal Text Without Child</li>
    <li>Link Text Without Child</li>
    <li class="has-child">
            <a href="#">Link Text With Child</a>
            <ul>
                    <li><a href="#">Child Link Text</a></li>
                    <li><a href="#">Child Link Text</a></li>
                    <li><a href="#">Child Link Text</a></li>
                    <li>Child Normal Text</li>
                    <li>Child Normal Text</li>
                    <li>Child Normal Text</li>
            </ul>
    </li>
     <li class="has-child">
            Normal Text With Child
            <ul>
                    <li><a href="#">Child Link Text</a></li>
                    <li><a href="#">Child Link Text</a></li>
                    <li><a href="#">Child Link Text</a></li>
                    <li>Child Normal Text</li>
                    <li>Child Normal Text</li>
                    <li>Child Normal Text</li>
            </ul>
    </li>
</ul>

该代码负责显示我的自定义帖子类型调用Product,它具有pagehierarchical属性,因此我可以将帖子设置为具有父级。我把这段代码写在taxonomy-product_category.php. 这是我的尝试:

<?php
            global $post;
            $child = false;
            $type   = get_term_by('slug',get_query_var('term'),get_query_var('taxonomy'));
            $loop = new WP_Query(
            array(
                    'post_type' => 'product',
                    'orderby' => 'title',
                    'order' => 'ASC',
                    'posts_per_page' => -1,
                    'tax_query' => array(
                            array(
                                    'taxonomy'  => 'product_category',
                                    'field'     => 'slug',
                                    'terms'     => $type->slug
                            )
                    ),
            )
            );?>

            <?php if($loop->have_posts()){while($loop->have_posts()):$loop->the_post();
                    $post;
                    $detail = sunterCustomField('detail'); //metabox that will check if this post able to show link or not
                    $text = ($detail == 1) ? '<a href="'.get_permalink().'">'.get_the_title().'</a>' : get_the_title();
            ?>
              <li>
                   <?php echo $text;?>
                    <?php if($post->post_parent <> 0 && $child == false):?>
                    <ul>
                  <li><?php echo $text;?></li>
                    <?php
                    $child = true;
                    endif;?>
                    <?php if($post->post_parent == 0 && $child == true):?>
                    </ul>
                    <?php
                    $child = false;
                    endif;?>
                            </li>
            <?php endwhile;} ?>
            <?php wp_reset_query(); ?>

注意:我不能使用wp_list_pages()因为不是每个帖子都有一个链接,我只需要查询同一术语中的帖子类型(这就是我在 taxonomy-product_category.php 中写它的原因)。这里是上面代码的结果。

        <ul class="dropdown">
      <li>
            <a href="#">A product Chemical 1</a>
            <li>
                    A product Chemical 6 child
            <ul>
                    <li>A product Chemical 6 child</li>
                    <li>A product Chemical 7 child </li>
                     <li>B product Chemical 2 </li>
            </ul>
            </li>
             <li><a href="#">C product Chemical 3</a>
    </ul>

看,它没有正确显示。我想不通其中的逻辑。

解决方案 !!

好吧,经过几个小时的谷歌搜索后,我终于可以解决它了。所以,基本思想是条件语句,它检测当前帖子是否有子帖子并检查当前帖子是否是顶级帖子。感谢我借用的这个很棒的片段并对其进行一点编辑,使其适合我的问题。我正在将帖子类型参数添加到产品帖子类型的功能中

function has_children() {
global $post;
$args = array(
    'child_of'  => $post->ID,
    'post_type' => 'product'
);
$pages = get_pages($args);
return count($pages);
}

function is_top_level() {
global $post, $wpdb;
$current_page = $wpdb->get_var("SELECT post_parent FROM $wpdb->posts WHERE ID = " . $post->ID);
return $current_page;
}

是的,上面的代码添加在functions.php中。这里是taxonomy-product_catgeory.php中的代码

<?php
    $loop = new WP_Query(
    array(
            'post_type' => 'product',
            'orderby' => 'title',
            'order' => 'ASC',
            'posts_per_page' => -1,
            'tax_query' => array(
                    array(
                            'taxonomy'  => 'product_category',
                            'field'     => 'slug',
                            'terms'     => $type->slug
                    )
            ),
    )
    );?>
    <?php if($loop->have_posts()){while($loop->have_posts()):$loop->the_post();
            $detail = sunterCustomField('detail');
            $text = ($detail == 1) ? '<a href="'.get_permalink().'">'.get_the_title().'</a>' : get_the_title();
    ?>
    <?php

    if (has_children()) {
            ?>
            <li class="has-child">
                    <?php echo $text;?>
                    <ul>
                            <?php
                            $args = array(
                                    'child_of' => $post->ID,
                                    'parent' => $post->ID,
                                    'post_type'     => 'product',
                                     'hierarchical' => 0
                            );
                            $pages = get_pages($args);
                            foreach ($pages as $page) {
                                    $subdetail =  get_post_meta( $page->ID, 'detail', true );
                                    $subtitle = ($subdetail == 1) ? '<a href="'.get_permalink($page->ID).'">'.$page->post_title.'</a>' : $page->post_title;
                                    echo '<li>'.$subtitle.'</li>';
                            }
                            ?>
                    </ul>
            </li>

            <?php
    }
    if(!is_top_level() && !has_children()){
            ?>
            <li><?php echo $text;?></li>
            <?php
    }

    ?>
    <?php endwhile;} ?>
    <?php wp_reset_query(); ?>
4

1 回答 1

0

您需要添加一个结束列表项标签......</li>在您的第一个<li>和您缺少无序列表的结束标签之后。在A品Chemical 6子之后你需要</ul>在打开另一个之前添加<ul>

于 2013-11-02T02:02:07.150 回答