WordPress dynamic custom menu not showing correct results

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)

Read More
<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: Menu Problem ARVIS APPS

I am not familiar with either WordPress or PHP so please forgive any silly mistakes.

Related posts

2 comments

  1. In functions.php add this function:

    function getPostsByCategoryID($categoryID)
    {
        $args = array(
        'posts_per_page'   => -1,
        'offset'           => 0,
        'category'         => $categoryID,
        'orderby'          => 'date',
        'order'            => 'ASC',
        'post_type'        => 'post',
        'post_status'      => 'publish',
        );    
    
        $allposts = get_posts( $args );
        foreach ( $allposts as $p ):
            echo '<li><a href="'. get_permalink($p->ID) . '">' . get_the_title($p->ID) . '</a></li>';
        endforeach;
    }
    

    Use it like this for your sidebar or wherever you want:

    <?php getPostsByCategoryID(HERE_THE_CATEGORY_ID); ?>
    

    For example:

    <?php getPostsByCategoryID(4); ?>
    
  2. You can better do this:

    In your theme map, create a file named sidebar-custom1.php and copy this code:

    <div class="col-md-4 enigma-sidebar">
        <div class="enigma_sidebar_widget">
            <div class="enigma_sidebar_widget_title">
                <h2>Sidebar title</h2>
                <?php getPostsByCategoryID(4); ?>
            </div>
        </div>
    </div>
    

    Then, in the page you want to call the sidebar, you paste this code:

    <?php get_sidebar('custom1'); ?>
    

    That will include the sidebar wherever you want it.

Comments are closed.