doujiu1447 2014-05-27 18:40
浏览 24
已采纳

当前自定义帖子类型的Wordpress菜单

I am looking to automatically create a Wordpress Menu for the 'current' Custom Post Type. I found a useful snippet that outputs the current custom post type here:-

 $post_type = get_post_type( $post->ID );
 echo $post_type;

But am struggling to translate this (or alternate method) into a dynamically created menu for the current custom post type - list all posts in the custom post type. I can't do this on an individual custom post type basis as I'm using a master template to display a series of custom post types.

Thanks

Glennyboy

  • 写回答

1条回答 默认 最新

  • douqiao2008 2014-05-27 19:36
    关注

    Here's a super simple way to do it. just create a loop that lists all the page titles wrapped in a link.

    <?php 
        $obj = get_post_type_object(get_post_type($post->ID));
        echo '<h2>' .  $obj->labels->name . '</h2>';
    ?>
    <ul>
        <?php 
            $query = new WP_Query(array('post_type' => get_post_type($post->ID), 'posts_per_page' => -1, 'order' => 'DESC', 'orderby' => 'date',));
            while ( $query->have_posts() ) : $query->the_post();    
        ?>
    
            <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>  
    
        <?php endwhile; wp_reset_postdata(); ?>
    </ul>
    

    This will loop through all the current post's post type and create an unordered list with all the posts titles, wrapped in links to the posts. You could obviously change the HTML to suit your needs.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)