WordPress Snippets at WPcustoms

Display page list when no menu is assigned

Use this function to automatically add wp_list_pages to an empty menu location. You only need to call this function where you are calling wp_nav_menu and add the fallback: ‘fallback_cb’ => ‘wpc_default_menu’,


/**
 * Snippet Name: Display page list when no menu is assigned
 * Snippet URL: https://wpcustoms.net/snippets/display-page-list-menu-assigned/
 */
  function wpc_default_menu( $args = array() ) {
  $defaults = array('sort_column' => 'menu_order, post_title', 'menu_class' => 'menu', 'echo' => true, 'link_before' => '', 'link_after' => '');
  $args = wp_parse_args( $args, $defaults );
  $args = apply_filters( 'faq_default_menu_args', $args );

  $menu = '';

  $list_args = $args;

  // Show Home in the menu
  if ( ! empty($args['show_home']) ) {
    if ( true === $args['show_home'] || '1' === $args['show_home'] || 1 === $args['show_home'] )
      $text = __('Home', 'omega');
    else
      $text = $args['show_home'];
    $class = '';
    if ( is_front_page() && !is_paged() )
      $class = 'class="current_page_item"';
    $menu .= '
  • ' . $args['link_before'] . $text . $args['link_after'] . '
  • '; // If the front page is a page, add it to the exclude list if (get_option('show_on_front') == 'page') { if ( !empty( $list_args['exclude'] ) ) { $list_args['exclude'] .= ','; } else { $list_args['exclude'] = ''; } $list_args['exclude'] .= get_option('page_on_front'); } } $list_args['echo'] = false; $list_args['title_li'] = ''; $menu .= str_replace( array( "\r", "\n", "\t" ), '', wp_list_pages($list_args) ); if ( $menu ) $menu = '
      ' . $menu . '
    '; $menu = apply_filters( 'wpc_default_menu', $menu, $args ); if ( $args['echo'] ) echo $menu; else return $menu; }