What i want to make:
I want to make a navigation menu where every dropdownbar is its own post type.
What i've made so far:
I'm not the best at php yet, but i tried to work something out:
echo "<ul class="menu">";
$post_type = get_post_types( array('Movie', 'Book', 'Game') );
foreach( $post_type as $type ) {
$args = array(
'post_type' => $type
);
echo "<li>".$type."<ul class="dropdown">";
$posts = get_posts( $args );
if( $posts ) {
foreach( $posts as $post ) {
echo "<li>".get_the_title( $post->ID, 'title' )."</li>";
}
echo "</ul></li>";
}
}
echo "</ul>";
Question:
Is there a smarter way to make the dropdownmenu? or what can i do to make it work?
There's nothing wrong with that approach, except that you shouldn't use get_post_types()
- just an array of the post type names will do.
As it stands at the moment, 'post_type' => $type
will pass an array to post_type, when it should be a string.
Also, echo "<li>".$type."<ul class="dropdown">";
should be inside your if( $posts ) {
before the foreach.