每个类别显示最新 Post 的下拉导航菜单

Drop Down Navigation Menu That Shows Latest Post per Category

所以我正在尝试在我的 wordpress 网站上处理我的导航菜单。我正在尝试从 hongkiat.com(如图所示)复制导航菜单。

在 Hongkiat 的导航菜单中,您会看到五 (5) 个父类别(设计/开发、技术、灵感、社交商务和交易)。一旦用户将鼠标悬停在父类别上,它将在父类别下显示最近的 posts。

无论如何,我设法编写了显示父类别及其子类别的下拉菜单。现在的困境在于如何在用户悬停的父类别下显示最新的 post (至少 3 posts)

无论如何,这是我的代码:

HTML/PHP

<ul>
    <?php 

        global $post;
        $myposts = get_posts('numberposts=3&offset=1');
        foreach($myposts as $post) ;


        $args = array(
        'show_option_all'    => '',
        'hide_empty'         => '0',
        'orderby'            => 'name',
        'order'              => 'ASC',
        'style'              => 'list',
        'use_desc_for_title' => 1,
        'child_of'           => 0,
        'hierarchical'       => 1,
        'title_li'           => (''),
        'show_option_none'   => __( '' ),
        'number'             => null,
        'echo'               => 1,
        'depth'              => 2,
        'current_category'   => 0,
        'pad_counts'         => 0,
        'taxonomy'           => 'category',
        'walker'             => null
        );
        wp_list_categories( $args ); 
    ?>
</ul>

CSS

.navone {
    display:inline-block;
    height:35px;
    vertical-align:middle;
    margin:0px auto;
    font-family: "Raleway",san-serif;
    font-feature-settings: normal;
    font-kerning: auto;
    font-language-override: normal;
    font-size: 12px;
    font-size-adjust: none;
    font-stretch: normal;
    font-style: normal;
    font-synthesis: weight style;
    font-variant: normal;
    font-weight: 600 !important;
    letter-spacing: 0.03em;
    text-transform:uppercase;
}

.navone ul {
    margin:0;
    padding:0;
}

.navone ul ul {
    display: none;

}

.navone ul li:hover > ul {
    display: block;
    -webkit-transition: all .25s ease;
    -moz-transition: all .25s ease;
    -ms-transition: all .25s ease;
    -o-transition: all .25s ease;
    transition: all .25s ease;
}

.navone ul {
    list-style: none;
    position: relative;
    display: inline-table;
}

.navone ul:after {
    content: ""; clear: both; display: block;
}

.navone ul li {
    float: left;
    cursor:pointer;
    padding:10px 20px;
}

.navone ul li:hover {
    background:#000;
}

.navone ul li:hover a {
    color: #fff;
}
    
.navone ul li a {
    display: block;
    color: #FFF;
    text-decoration: none;
}

.navone ul li ul {
    width:200px;
    z-index:9;
}

.navone ul ul {
    background: #000;
    padding: 0;
    position: absolute;
    top:100%;
    left:0;
}

.navone ul ul li {
    float: none; 
    position: relative;
    padding:5px 10px;
}

.navone ul ul li a {
    color: #fff;
}

.navone ul ul li a:hover {
    -webkit-transition: all .25s ease;
    -moz-transition: all .25s ease;
    -ms-transition: all .25s ease;
    -o-transition: all .25s ease;
    transition: all .25s ease;
}

.navone ul ul ul {
    position: absolute; left: 100%; top:0;
}

.navone ul li ul li {
    padding:8px 10px;
    -webkit-transition: all .25s ease;
    -moz-transition: all .25s ease;
    -ms-transition: all .25s ease;
    -o-transition: all .25s ease;
    transition: all .25s ease;
}
.navone ul li ul li:hover {
    border-left:5px solid #F52100;
    padding-left:20px;
    -webkit-transition: all .25s ease;
    -moz-transition: all .25s ease;
    -ms-transition: all .25s ease;
    -o-transition: all .25s ease;
    transition: all .25s ease;
}

.navtwo {
    display:inline;
}

如果有人可以帮助我或指出我缺少的东西,因为我的代码不起作用。我的代码没有错误,但我没有实现我想要完成的目标。

谁能伸出援助之手

你可以试试这个:

<ul>
<?php $args = array(
        'type'                     => 'post',
        'child_of'                 => 0,
        'parent'                   => '',
        'orderby'                  => 'name',
        'order'                    => 'ASC',
        'hide_empty'               => 1,
        'hierarchical'             => 1,
        'exclude'                  => '',
        'include'                  => '',
        'number'                   => '',
        'taxonomy'                 => 'category',
        'pad_counts'               => false

);

$categories = get_categories( $args );

foreach($categories as $cat): ?>
    <li>
        <?php echo $cat->$name; $cat_id = $cat->$term_id;?>
        <?php $post_args = array(
            'post_type'=>'post',
            'posts_per_page' => 3,
            'cat' => $cat_id
        );
        $the_query = new WP_Query($post_args);
        if($the_query->have_posts()): ?>
        <ul>
            <?php while($the_query->have_posts()): $the_query->the_post(); ?>
                    <li>
                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                    </li>
            <?php endwhile; ?>
        </ul>
        <?php endif; wp_reset_query(); ?>
    </li>
<?php endforeach; ?>
</ul>

尚未测试,但应该可以。

<ul>
<?php $args = array(
        'type'                     => 'post',
        'child_of'                 => 0,
        'parent'                   => '',
        'orderby'                  => 'name',
        'order'                    => 'ASC',
        'hide_empty'               => 1,
        'hierarchical'             => 1,
        'exclude'                  => '',
        'include'                  => '',
        'number'                   => '',
        'taxonomy'                 => 'category',
        'pad_counts'               => false

);

$categories = get_categories( $args );

foreach($categories as $cat)
{ 
    if ($cat->category_parent == 0) {

?>
    <li>
        <?php echo $cat->name; $cat_id = $cat->term_id;?>
        <?php $post_args = array(
            'post_type'=>'post',
            'posts_per_page' => 3,
            'cat' => $cat_id
        );
        $the_query = new WP_Query($post_args);
        if($the_query->have_posts()): ?>
        <ul>
            <?php while($the_query->have_posts()): $the_query->the_post(); ?>
                    <li>
                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                    </li>
            <?php endwhile; ?>
        </ul>
        <?php endif; wp_reset_query(); ?>
    </li>
<?php } }?>
</ul>

您在上面的代码中遗漏了一些东西。

让我们尽量简化。

第1步 : 将其下的所有父类别和子类别放入一个名为$total_categories

的数组中

你可以看到下面的代码来获取所有父类别和子类别的数组

<?php 
$parent_args = array(
        'type'                     => 'post',
        'parent'                   => '0',
        'orderby'                  => 'name',
        'order'                    => 'ASC',
        'hide_empty'               => 0,
        'hierarchical'             => 1,
        'taxonomy'                 => 'category',
        'pad_counts'               => false
);
$parent_categories = get_categories( $parent_args );

foreach ($parent_categories as $parent_category) {
    $child_args = array(
        'type'                     => 'post',
        'parent'                   => $parent_category->term_id,
        'orderby'                  => 'name',
        'order'                    => 'ASC',
        'hide_empty'               => 0,
        'hierarchical'             => 1,
        'taxonomy'                 => 'category',
        'pad_counts'               => false
    );  
    $cats_arr = array(); 
    $child_categories = get_categories( $child_args ); 
    $cats_arr[] = $parent_category->term_id;
    foreach ($child_categories as $child_category) {
        $cats_arr[] = $child_category->term_id;
    }

    $total_categories[$parent_category->term_id] = $cats_arr;
}
?>

Output Provided was for my case

上面的数组看起来是这样的,其中 18 是父类别 ID,子类别是 19 和 20 另外我们还包括数组中的父类别 ID (18) 以及数组的索引。

Array
(
    [18] => Array
        (
            [0] => 18
            [1] => 19
            [2] => 20
        )

    [15] => Array
        (
            [0] => 15
            [1] => 16
            [2] => 17
        )

    [1] => Array
        (
            [0] => 1
        )

)

现在只需遍历数组 $total_categories 即可获取所有父类别和该类别下的 前 3 个帖子

<ul>
<?php foreach($total_categories as $total_category_k=>$total_category_v): ?>
        <li>

            <a href=""><?php echo get_the_category_by_ID($total_category_k);?></a>
        <?php $post_args = array(
            'post_type'=>'post',
            'posts_per_page' => 3,
            'orderby'=>'ID',
            'order'=>'DESC',
            'tax_query' => array(
                                array(
                                    'taxonomy' => 'category',
                                    'field'    => 'term_id',
                                    'terms'    => $total_category_v,
                                    'operator' => 'IN'
                                ),
                            ),
            );

        $the_query = new WP_Query($post_args);
        if($the_query->have_posts()): ?>
        <ul>
            <?php while($the_query->have_posts()): $the_query->the_post(); ?>
                    <li>
                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                    </li>
            <?php endwhile; ?>
        </ul>
        <?php endif; wp_reset_query(); ?>
    </li>
<?php endforeach; ?>
</ul>

Output Provided was for my case

And it will finally give you the desired HTML with required content output as below :

<ul>
        <li>

            <a href="">menu</a>
            </li>
        <li>

            <a href="">test</a>
            </li>
        <li>

            <a href="">Uncategorized</a>
                <ul>
                                <li>
                        <a href="http://localhost/wp/clark_atlanta_university/students-unlock-the-mysteries-of-the-brain-with-nih-scientists/">Students unlock the mysteries of the brain with NIH scientists</a>
                    </li>
                                <li>
                        <a href="http://localhost/wp/clark_atlanta_university/contacts-better-than-permanent-lenses-for-babies-after-cataract-surgery/">Contacts better than permanent lenses for babies after cataract surgery</a>
                    </li>
                                <li>
                        <a href="http://localhost/wp/clark_atlanta_university/nih-announces-recruitment-for-clinical-trial-to-test-new-tinnitus-treatment-device/">NIH announces recruitment for clinical trial to test new tinnitus treatment device</a>
                    </li>
                    </ul>
            </li>
</ul>

您缺少的代码是:

'orderby'=>'ID',
'order'=>'DESC',

To get the top 3 posts you will have to get the posts in descending order by ID and of course posts_per_page will get you 3 records only

'tax_query' => array(
                                    array(
                                        'taxonomy' => 'category',
                                        'field'    => 'term_id',
                                        'terms'    => $total_category_v,
                                        'operator' => 'IN'
                                    ),
                                ),
                );

And to get posts related to categories (whether the parent or child ) so we have passed the array containing the parent as well as child categories