Wordpress - 点击类别链接后按类别列出的帖子列表

时间:2014-05-22 09:38:30

标签: php wordpress-theming wordpress

在我的wordpress网站上,我有一个页面,使用以下代码列出所有类别和子类别。

<ul>
<?php 
$parents = get_categories(array('parent' => 0, 'exclude' => '1,7','hide_empty' => 0));
if(!empty($parents)){
    foreach($parents as $parent){
        ?>
        <li>
            <div class="catImg">
                <span><img src="<?php echo z_taxonomy_image_url($parent->term_id); ?>" /></span>
            </div>
            <h2><?php echo $parent->name; ?></h2>
            <ul class="models">
                <?php wp_list_categories(array('hierarchical' => false, 'hide_empty' => 0, 'title_li' => __( '' ), 'show_option_none'   => __( '' ), 'child_of' => $parent->term_id)); ?>
            </ul>
        </li>
        <?php
    }
} else { 
    ?>
    <li>No Categories</li>
    <?php } ?>
</ul>

上面的代码会生成多个块,如下图所示。在照片中,'团队Losi Racing'是父类别,'8ight 3.0'和'8ight EU'是Team Losi Racing的子类别。

enter image description here

当我点击ul.models列表中的子类别时,我希望转到列出该类别中所有帖子的页面。

由于我是wordpress的新手,这是我的第一个主题,我想知道是否有人可能指出我正确的方向来实现上述结果。

提前致谢

1 个答案:

答案 0 :(得分:0)

答案

正如我的理解。你可以在下面这样做。试试吧。如果您有任何疑问或疑虑,请随时通过搜索yeshansachithak与我联系。

<强> CODE

<ul>
    <?php 
    $parents = get_categories(array('parent' => 0, 'exclude' => '1,7','hide_empty' => 0));
    if(!empty($parents)){
        foreach($parents as $parent){
            ?>
            <li>
                <div class="catImg">
                    <span><img src="<?php echo z_taxonomy_image_url($parent->term_id); ?>" /></span>
                </div>
                <h2><?php echo $parent->name; ?></h2>
                <ul class="models">
                    <?php 
                      $categories = get_categories('child_of'=>$parent->ID); //Pass the Parent category id here
                      foreach ($categories as $category) {
                        echo '<li>'.$category->cat_name.'</li>';//Child cat list
                        //Show links by using css after click above sub-cat name
                        ?>
                        <ul class="sub-cat-post-links">
                        <?php
                            $args = array( 'offset'=> 1, 'category' => $category->cat_ID );
                            $myposts = get_posts( $args );
                            foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
                                <li>
                                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                </li>
                            <?php endforeach; 
                            wp_reset_postdata();
                        ?>
                        </ul>
                        <?php
                      }
                     ?>
                </ul>
            </li>
            <?php
        }
    } else { 
        ?>
        <li>No Categories</li>
        <?php } ?>
    </ul>

<强>解释

从您的代码中,您可以获得父类别。然后我们可以得到父母的孩子类别。在子类别列表中,我们可以列出属于子和父的帖子。单击子猫名称后将显示该列表。你还必须做一些css。当我们点击帖子名称/标题时。它将把我们带到帖子。single.php

感谢。这太快了。抱歉我的英文不好

简要介绍

在我的回答中,关于你的que形象。 Team Losi RacingXRAY是父类别名称。 8ight 3.08ight EU是子类别名称。当您单击子猫名称时。它会在child category name下方显示所属帖子。喜欢下拉。点击帖子链接后。它将带您到帖子内容。

根据我们的讨论,请参阅下面的第二个答案。我不想删除第一个答案。它也对某人有所帮助。

答案

这是代码。你可以在下面这样做。试试吧。如果您有任何疑问或疑虑,请随时通过搜索yeshansachithak与我联系。

<ul>
    <?php 
    $parents = get_categories(array('parent' => 0, 'exclude' => '1,7','hide_empty' => 0));
    if(!empty($parents)){
        foreach($parents as $parent){
            ?>
            <li>
                <div class="catImg">
                    <span><img src="<?php echo z_taxonomy_image_url($parent->term_id); ?>" /></span>
                </div>
                <h2><?php echo $parent->name; ?></h2>
                <ul class="models">
                    <?php 
                      $categories = get_categories('child_of'=>$parent->ID); //Pass the Parent category id here
                      foreach ($categories as $category) {
                        echo '<li>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </li> ';
                      }
                     ?>
                </ul>
            </li>
            <?php
        }
    } else { 
        ?>
        <li>No Categories</li>
        <?php } ?>
    </ul>

简要介绍

在上面一个。我们正在从父母猫那里获取这些儿童类别。之后,我们将显示该子类别的链接。点击链接后,我们会转到类别帖子列表archive.php页面。在那里,你可以喜欢你的风格。

非常感谢

相关问题