30

The has_archive parameter for register_post_type() does exactly what you expect it to do. However, it modifies rewrite rules when set. So, if you change it to false, you'll need to flush your rewrite rules in order for the archive index display to be removed entirely.


28

You can use a built in function of the menu editor for this. Go to your Custom Post Type in the Menu editor, click the "view all" tab and the first item you see is a link to the CPT archive page. In case your CPT does not appear in the Menu editor please check whether your CPT has archives enabled ('has_archive' => true in the register_post_type-...


20

Navigate to Settings -> permalinks Change the permalink structure to Default Save settings Change to custom structure or post name (or any other structure) Save Settings This will re-write the htaccess file and then the re-write should work. If the above solution doesn't work - it should be related to server configuration. Aapache2 Run: a2enmod rewrite &...


19

To get the current post type use get_post_type(). Then ask get_post_type_object() for all the data you need, for example the slug: $post_type = get_post_type(); if ( $post_type ) { $post_type_data = get_post_type_object( $post_type ); $post_type_slug = $post_type_data->rewrite['slug']; echo $post_type_slug; }


12

What you need is hooking template_include filter and selectively load your template inside plugin. As a good practice, if you plan to distribute your plugin, you should check if archive-my_plugin_lesson.php (or maybe myplugin/archive-lesson.php) exists in theme, if not use the plugin version. In this way is easy for users replace the template via theme (or ...


11

Given the standard registration, you should have the following: A post type with the name 'recipe' A recipe post archive at example.com/recipe/ Recipe posts with urls that take the form example.com/recipe/helloworldrecipe/ A template archive-recipe.php A template single-recipe.php However, I see this in your registration code: 'rewrite' => array('slug' =...


10

Since WordPress version 4.4 the hook 'theme_page_templates' allows to set arbitrary page templates. It means that it's possible to show arbitrary values in the "Page Template" menu of the page edit screen, and when selected, the value will be stored in the page template meta for the page. This means that you can "automagically" create a page template for ...


10

You have multiple options here. 1. Define post type archive slug on post type registration By setting 'has_archive' => 'galleries' you can define custom post type archive slug. Check documentation. Then you can delete your page "galleries" then add & customize the archive-post_type.php 2. Disable default archive for post type Disable the archive ...


8

I found a solution here: http://gabrieleromanato.name/wordpress-fix-the-404-error-on-custom-post-types/ Here's what you need to do to get it to work (quoted from site above): Go to Settings → Permalinks and change your current structure to: /%category%/%postname% Save changes. Restore your original permalink settings. Save changes.


8

Have you tried refreshing permalinks? Settings -> Permalinks (don't have to change anything) Then try again?


8

Pass the desired archive slug via has_archive: $args = array( 'rewrite' => array( 'slug' => 'case-studies' ), 'has_archive' => 'testimonials-case-studies', );


7

Here's an example using add_rewrite_rule to handle years and months for a custom post type where news is the slug. Visit the Settings > Permalinks page in admin to flush rewrite rules after this is added. You could also put this in a plugin and flush rewrite rules on plugin activation. function wpa83797_news_rewrite_rules(){ add_rewrite_rule( ...


7

You can pass a string for has_archive. That string will be used as archive URL. So in your registration code use: 'has_archive' => 'jobs' From the register_post_type() declaration in WordPress core: if ( $args->has_archive ) { $archive_slug = $args->has_archive === true ? $args->rewrite['slug'] : $args->has_archive; if ( $args->...


7

You can use the archive_template hook to process the content of a theme's archive template, by using the scheme below, but obviously you'll only ever be able to process a fraction of the themes out there, given that a template can basically contain any old thing. The scheme is to load the template into a string ($tpl_str) in the archive_template filter, ...


7

EDIT I get the idea that I might have misread your question Just a few notes here If archive-projects.php is a page template, rename it. You should not use archive as a prefix for a page template, or for that matter any other reserved template name. Page templates should be named page-{$name}.php or any other name with prefixes used by the template ...


6

Re-name your CPT archive to home.php Then use pre_get_posts to alter the home page query so only CPT's display function wpsites_home_page_cpt_filter($query) { if ( !is_admin() && $query->is_main_query() && is_home() ) { $query->set('post_type', array( 'your-cpt' ) ); } } add_action('pre_get_posts','wpsites_home_page_cpt_filter')...


6

I know this is old but I have this problem too and I found a rather clean way to handle it is to use a custom menu walker class KB_Custom_Menu_Walker extends Walker_Nav_Menu { protected static $custom_post_types = array(); public function start_el(&$output, $item, $depth=0, $args=array(), $id=0) { if (isset( self::$custom_post_types[ $item->...


6

I'm using this outside of the loop on the archive.php template to get which custom post archive I'm on. It's a combo of the methods that both @toscho and @Rarst recommended: $post_type = get_queried_object(); echo $post_type->rewrite['slug']; Update: @majick pointed out that this only works if you've set the rewrite slug for your CPT. Rewrite slug is ...


6

Get all custom post types: $post_types = get_post_types( array ( '_builtin' => FALSE ), 'objects' ); Sort them by their name: uasort( $post_types, 'sort_cpts_by_label' ); /** * Sort post types by their display label. * * @param object $cpt1 * @param object $cpt2 * @return int */ function sort_cpts_by_label( $cpt1, $cpt2 ) { return ...


6

I'm not sure if I understand you correctly, but maybe this will help you. If you're on CPT archive, then there is no "Edit" link, because it's archive and not a page... Archive is list of posts, so there is no single post to edit... Another example of archive page is when you're viewing search results or all posts from given year - there is nothing to edit ...


5

EDIT -> while this answer still works for < WP4.4, since 4.4 support for Custom Post Types is now included in wp_get_archives() There finally is a simple, quick and easy solution for date based archives of Custom Post Types in WordPress! This has been a long standing issue that is recorded here in the WP Core Trac. It has yet to be solved but one of the ...


5

as determined in the comments, the page URI is ?post_type=photos&genre=boudoir... The reason why "archive-{$post_type}.php" isn't used in this case is because the page is a taxonomy archive and not a custom post type archive. WordPress is looking for taxonomy-genre.php instead of archive-photos.php, and not finding it, is defaulting back to good old ...


5

Include your template files in your plugin. I stick mine in /plugin/templates. You need to hook into template location for that template: add_filter('archive_template', 'yourplugin_get_custom_archive_template'); function yourplugin_get_custom_archive_template($template) { global $wp_query; if (is_post_type_archive('yourCPT')) { $templates[]...


5

Updated approach The first thought/suggestion I made actually - like you said - doesn't work as I understood it. At least I tried it and couldn't figure it out. That said, what you want is still achievable, but not by using a endpoint or at least not by making use of add_rewrite_endpoint(). However I figured it should be possible via a "ordinary" rewrite ...


5

I don't think you need to use the hook pre_get_posts on this. Remove that part from functions.php. Then just put the archive-technology.php in themes root directory. Then go to you Permalinks settings page and hit Save Changes button. WordPress will need to regenerate the .htaccess file to use new archive template.


4

This should work for you: // grab all public post types $post_types = get_post_types( array('public' => true), 'names' ); // loops through each post type foreach( $post_types as $type ) { // setup the query $query_args = array( 'post_type' => $type, 'posts_per_page' => 1 ); // perform the query $items = ...


4

Hook into '404_template'. (Example) Fetch all public custom post types where has_archive is not FALSE. Find the post type’s has_archive string and see if it is part of the current request’s url. Try get_page_by_title() with the last part of the requests. wp_redirect() to the found post’s permalink. exit;.


4

The answers get confusing. And maybe Im as well but the headline question is: Get custom post type slug for an archive page If you mean post type archive landing-page, and when is_post_type_archive() returns true, you want the slug that responing to current viewing archive: /* returns /products/ */ $responding_name = str_replace(get_home_url(), '', ...


4

Why don't you try using a home.php file and place your desired loop/query within that file or alternatively use get_template_part to retrieve a file that contains your loop. Can you please show us your code? What I frequently do is structure my index.php like so, <?php get_header(); ?> <?php if ( is_home() ) { ...


4

If you see doc for wp_list_categories in codex, you'll se that it accepts a include param, that should contain a comma separed list of categorys ids. So if you change your function get_terms_by_post_type creating the function get_terms_id_by_post_type to retrieve only the term ids, just like: function get_terms_id_by_post_type( $taxonomies, $post_types ) { ...


Only top voted, non community-wiki answers of a minimum length are eligible