WordPress Custom Posts Auto Menu for Current Post Type

Glenn Glenn's Blog 0 Comments

Here is a great snippet that automatically creates a menu for your current WordPress Custom Post Type:-



<?php if(condition){ ?>
<div class="widget widget_nav_menu">
<?php
$obj = get_post_type_object(get_post_type($post->ID));
echo '<h2>' . $obj->labels->name . '...</h2>';
?>
<ul class="menu">
<?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>
</div>
<?php }?>

Simply drop the code into your custom post archive/template and a menu will be automatically created for you – no need to input your custom post type.

Credit to Andy Warren

About the Author
Wordpress Custom Posts Auto Menu for Current Post Type WordPress Custom Posts Auto Menu for Current Post Type 0da77002febbfe28d15144a57bbcc2c9 s 180 d mm r g

Glenn

A highly experienced WordPress Web Developer, Front-end and Back-end Developer & New Media Specialist with extensive knowledge of a wide spectrum of technologies in the Development and Creative Industries built up over a number of years.

Leave a Reply

Your email address will not be published. Required fields are marked *