In order to make a one-page wordpress theme, I used this code in functions.php:

function new_nav_menu_items($items){ 

$items = "";

$args = array("post_type" => "page", "order" => "ASC", "orderby" => "menu_order");
$the_query = new WP_Query($args);

if($the_query->have_posts()): 
    while($the_query->have_posts()):
        $the_query->the_post(); 
            $items .= '<li><a href="#post-'. get_the_ID() .'">' . get_the_title() . '</a></li>';            
    endwhile;
else: 
    echo "";
endif;
return $items;
}


add_filter("wp_nav_menu_items", "new_nav_menu_items");

However, when I put that code in my functions.php, I can’t view the custom links anymore in the nav. I need to use this code because it makes my theme just one-page.

In header.php:

 <?php wp_nav_menu( array('theme_location' => 'primary' ) ); ?>

and the other functions I used in my functions.php are the following:

 register_nav_menus(array(
    'primary' => esc_html__('Header','nxp-site'),
    'secondary' => esc_html__('Social','nxp-site'),));


 function modify_nav_menu_args( $args ) {
    if( 'primary' == $args['theme_location'] ) {
    $args['menu_class'] = 'menu genesis-nav-menu menu-primary nav-menu special-class';
    }
 return $args;
 }

 if(get_page_by_title("Home") == null)
 {
     $post = array(
    "post_title" => "Home",
    "post_status" => "publish",
    "post_type" => "page",
    "menu_order" => "-100",
    "page_template" => "single-page-theme.php"
    );

    wp_insert_post($post);

    $home_page = get_page_by_title("Home");
    update_option("page_on_front",$home_page->ID);
    update_option("show_on_front","page");
 }

My idea is that I’ll be selecting ‘primary’ so that I can only use new_nav_menu_items when I use ‘primary’. But if it is possible, how can I code it?

Read more here: How to add custom links in my menu nav? (One-page theme in WordPress)


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: