I am creating a dynamic custom menu that shows all posts links, like the menu widget in the sidebar, of a certain category. It is supposed to be dynamic, meaning that whenever I make a post in that category, the menu should include the post that I made without me having to physically drag & drop a new post in the menu.
Here’s my code: (the category ID of which’s posts I want : 4)
<div class="col-md-4 enigma-sidebar">
<?php if ( is_active_sidebar( 'sidebar-primary' ) )
{ dynamic_sidebar( 'sidebar-primary' ); }
else {
$args = array(
'before_widget' => '<div class="enigma_sidebar_widget">',
'after_widget' => '</div>',
'before_title' => '<div class="enigma_sidebar_widget_title"><h2>',
'after_title' => '</h2></div>' );
the_widget('WP_Widget_Archives', null, $args);
} ?>
<?php /*Menu Loop*/
function menu1_loop() {
global $post;
$args = array(
'type' => 'post',
'orderby' => 'date',
'order' => 'ASC',
'hide_empty' => 1,
'include' => '4',
'number' => '',
'taxonomy' => 'category',
);
$categories = get_categories( $args );
foreach($categories as $category) {
// WP_Query arguments
$args = array (
'category_name' => 'cat-html',
'order' => 'ASC',
'orderby' => 'date',
);
// The Query
$query = new WP_Query( $args );
//Loop
if ( $query->have_posts() ) {
/*echo "<div>"; */
while ( $query->have_posts() ) {
$post.the_permalink();
$post.the_title();
/*echo "<li><a href=".the_permalink().">".the_title()."</a></li>";*/
$query->the_post();
}
/*echo "</div>";*/
}
// Restore Original post data
wp_reset_postdata();
}
} ?>
<!-- # Added by Aryansh Malviya(ARVIS APPS) on Saturday, December 12th, 2015
# Added to make a custom menu for specific task
// begins -->
<?php wp_nav_menu( array( 'theme_location' => 'html-menu', 'container_class' => 'enigma_sidebar_widget' ) /*.menu1_loop()*/ ); ?>
<?php wp_nav_menu( array( 'theme_location' => 'php-menu', 'container_class' => 'enigma_sidebar_widget' ) ); ?>
<!-- // ends -->
</div>
This code is not doing what I think it supposedly should do, here’s a picture showing what this results into:
I am not familiar with either WordPress or PHP so please forgive any silly mistakes.
In functions.php add this function:
Use it like this for your sidebar or wherever you want:
For example:
You can better do this:
In your theme map, create a file named sidebar-custom1.php and copy this code:
Then, in the page you want to call the sidebar, you paste this code:
That will include the sidebar wherever you want it.