Skip to main content
added 593 characters in body
Source Link
Tom J Nowell
  • 60.6k
  • 7
  • 77
  • 147

First, throw away your custom walker.

Post URLs are called permalinks, and as with most things in WordPress, you can filter them! There is a filter called post_link that lets you modify the URL of the post before it's returned in get_permalink(). We can use this, along with the posts slug, and the home_url function to get something similar to this:

function wpse183928_anchor_urls( $url, $post, $leavename ) {
    $url = home_url()."#".$post->post_name;
    return $url;
}
add_filter( 'post_link', 'wpse183928_anchor_urls', 10, 3 );

Secondly, your metabox and post meta you're using for sections is the wrong way to do it. If you want to categorise things, you use a custom taxonomy, e.g. section category, you don't repurpose pages and post meta to implement a custom taxonomy.

So register a custom taxonomy, call it sections, and assign it to pages. Now you can add 'sections' to your nav menu, WordPress will give you a user interface for free, your data will support import/export, and you'll have an admin page dedicated to creating/editing/deleting sections made for you

Some helpful pieces of information:

  • A lot of people use a page template to implement a homepage. There's no need, just use the home.php template, or index.php
  • When I say posts, I'm talking about a post of type post. Pages are posts too ( of type page ). Menu nav items are posts of type nav_menu_item, and images are referenced by post ID, where each image has an associated post of type attachment with post meta connected to it that stores EXIF data, descriptions, etc
  • WP_Query. People will suggest querying using query_posts. These people are not your friends, treat everything they say with suspicion, and never use query_posts
  • If you need to modify which posts WordPress retrieves in the main loop, don't throw away the main query and create your own, use the pre_get_posts filter instead, it's significantly faster

Post URLs are called permalinks, and as with most things in WordPress, you can filter them! There is a filter called post_link that lets you modify the URL of the post before it's returned in get_permalink(). We can use this, along with the posts slug, and the home_url function to get something similar to this:

function wpse183928_anchor_urls( $url, $post, $leavename ) {
    $url = home_url()."#".$post->post_name;
    return $url;
}
add_filter( 'post_link', 'wpse183928_anchor_urls', 10, 3 );

Some helpful pieces of information:

  • A lot of people use a page template to implement a homepage. There's no need, just use the home.php template, or index.php
  • When I say posts, I'm talking about a post of type post. Pages are posts too ( of type page ). Menu nav items are posts of type nav_menu_item, and images are referenced by post ID, where each image has an associated post of type attachment with post meta connected to it that stores EXIF data, descriptions, etc
  • WP_Query. People will suggest querying using query_posts. These people are not your friends, treat everything they say with suspicion, and never use query_posts
  • If you need to modify which posts WordPress retrieves in the main loop, don't throw away the main query and create your own, use the pre_get_posts filter instead, it's significantly faster

First, throw away your custom walker.

Post URLs are called permalinks, and as with most things in WordPress, you can filter them! There is a filter called post_link that lets you modify the URL of the post before it's returned in get_permalink(). We can use this, along with the posts slug, and the home_url function to get something similar to this:

function wpse183928_anchor_urls( $url, $post, $leavename ) {
    $url = home_url()."#".$post->post_name;
    return $url;
}
add_filter( 'post_link', 'wpse183928_anchor_urls', 10, 3 );

Secondly, your metabox and post meta you're using for sections is the wrong way to do it. If you want to categorise things, you use a custom taxonomy, e.g. section category, you don't repurpose pages and post meta to implement a custom taxonomy.

So register a custom taxonomy, call it sections, and assign it to pages. Now you can add 'sections' to your nav menu, WordPress will give you a user interface for free, your data will support import/export, and you'll have an admin page dedicated to creating/editing/deleting sections made for you

Some helpful pieces of information:

  • A lot of people use a page template to implement a homepage. There's no need, just use the home.php template, or index.php
  • When I say posts, I'm talking about a post of type post. Pages are posts too ( of type page ). Menu nav items are posts of type nav_menu_item, and images are referenced by post ID, where each image has an associated post of type attachment with post meta connected to it that stores EXIF data, descriptions, etc
  • WP_Query. People will suggest querying using query_posts. These people are not your friends, treat everything they say with suspicion, and never use query_posts
  • If you need to modify which posts WordPress retrieves in the main loop, don't throw away the main query and create your own, use the pre_get_posts filter instead, it's significantly faster
Source Link
Tom J Nowell
  • 60.6k
  • 7
  • 77
  • 147

Post URLs are called permalinks, and as with most things in WordPress, you can filter them! There is a filter called post_link that lets you modify the URL of the post before it's returned in get_permalink(). We can use this, along with the posts slug, and the home_url function to get something similar to this:

function wpse183928_anchor_urls( $url, $post, $leavename ) {
    $url = home_url()."#".$post->post_name;
    return $url;
}
add_filter( 'post_link', 'wpse183928_anchor_urls', 10, 3 );

Some helpful pieces of information:

  • A lot of people use a page template to implement a homepage. There's no need, just use the home.php template, or index.php
  • When I say posts, I'm talking about a post of type post. Pages are posts too ( of type page ). Menu nav items are posts of type nav_menu_item, and images are referenced by post ID, where each image has an associated post of type attachment with post meta connected to it that stores EXIF data, descriptions, etc
  • WP_Query. People will suggest querying using query_posts. These people are not your friends, treat everything they say with suspicion, and never use query_posts
  • If you need to modify which posts WordPress retrieves in the main loop, don't throw away the main query and create your own, use the pre_get_posts filter instead, it's significantly faster