I have created a custom post type post_event_type and have set rewrite to true when registering.
Within this post type, there are categories under the taxonomy of event_Category with rewrite set to true
The url then resolves for this post to URL/post_event_type/postname
There are three categories under the taxonomy event_Category such as cinema, dance, music.
I want the url to resolve to the page as such URL/dance/postname so 'dance' would be the taxonomy category and post name would be the post residing underneath this category
If I change the register post type rewrite to 'events' they would all resolve to URL/events/postname
I have then tried the following coding to try and seperate these out. The URLs work in the way I wan them to, however they then go to a 404 page.
add_filter('rewrite_rules_array', 'mmp_rewrite_rules');
function mmp_rewrite_rules($rules) {
$newRules = array();
$newRules['events/(.+)/(.+)/(.+)/(.+)/?$'] = 'index.php?vc_guide_creator=$matches[4]';
$newRules['events/(.+)/?$'] = 'index.php?event_Category=$matches[1]';
return array_merge($newRules, $rules);
}
global $wp_rewrite;
$structure = '/%events%/';
$wp_rewrite->add_rewrite_tag("%events%", '([^/]+)', "post_event_type=");
$wp_rewrite->add_permastruct('post_event_type', $structure, false);
function filter_post_type_link($link, $post)
{
if ($post->post_type != 'post_event_type')
return $link;
if ($cats = get_the_terms($post->ID, 'event_Category')){
$link = str_replace('post_event_type', array_pop($cats)->slug, $link);
}
return $link;
}
add_filter('post_type_link', 'filter_post_type_link', 10, 2);
Can anyone help please?
Have you tried to update your permalinks.
Go to Settings-> Permalinks and click Save changes button in the bottom without changing any of the settings, this will simply update the permalinks table.
I'm using wordpress and my page has the URL http://proservicescontractors.com/services/
But when I go to the page in my dashboard with the above URL, any change I make does not show on the front end. I tried simply duplicating my content and that change did not show on the front end.
Not sure what to do, this has me completely baffled.
Any ideas?
Since they're custom post types, by default, they're not actually loaded into a page per se. You should read up on WordPress's template hierarchy. To give you a rough idea of what's happening:
WP looks at your URL, and since it recognises it as a custom post type archive, it will look for a template to use...
It will first look for archive-$post_type.php, or in your case, archive-services.php
If it can't find that, it will look for archive.php
If it can't find that, it will use index.php
The important thing to note is that archive pages don't actually show up in the admin area, since they simply gather up and display custom posts, so there's nothing for you to edit.
Now, if you really want to edit some content on the Services archive, you have two options:
Edit archive-services.php in a text editor.
This is the quick and dirty option; the downside is that it defies the point of a CMS.
Create a page template with it's own loop
Create a new page template called page-services.php and insert a loop in there to display your custom posts. To get you started:
<?php get_header(); ?>
<?php // The main loop
if (have_posts()) {
while (have_posts()) {
the_post();
}
} else {
echo 'No posts';
}
?>
<?php // Now for the services loop
// WP_Query arguments
// For additional options, see: https://codex.wordpress.org/Class_Reference/WP_Query#Parameters
$args = array (
'post_type' => array( 'services' ),
);
// The Query itself
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// Do something with the post
// In your case look at archive-services.php and see what
// that template does inside the loop
}
} else {
// no posts found
}
// Restore original Post Data
// Don't forget this, it's important
wp_reset_postdata();
?>
<?php get_footer();?>
You should then be able to apply that page template to your Services page; it should then display your posts below the page content. One thing to look out for is that WordPress will continue to load archive-services.php whenever you go to http://proservicescontractors.com/services/. While there are ways around this, the easiest fix would be to simply give your new page a different url, such as http://proservicescontractors.com/all-services/
Thanks for your help. I'm using yoast and I wanted to change the title and description. When you pointed out that it was a custom post type archive and not a page, I went back through yoast and found where I could change them under "Titles and Metas" > "Custom Post Type Archives" > "Services"
I would like to remove the category & tag base from WordPress URL. I have come across other posts and solutions which used plugins. I would like to stay away from plugins and have a solution from within functions.php. This would prevent any future plugin updates or WordPress default files from being changed.
Any help would be appreciated. Thanks!
I have tried these solutions so far:
This htaccess solution did not work: http://mikepayne.co/2011/remove-category-base-from-url/
These methods also failed: http://www.askapache.com/wordpress/remove-category-wordpress-urls.html
If you want to remove /category/ from the url, follow these two steps:
Go to Settings >> Permalinks and select Custom and enter: /%category%/%postname%/
Next set your Category Base to .
Save it and you’ll see your URL changed to this format:
http://yourblog.com/quotes/
(Source: http://premium.wpmudev.org/blog/daily-tip-quick-trick-to-remove-category-from-wordpress-url/)
If you use Yoast SEO plugin just go to:
Search Appearance > Taxonomies > Category URLs.
And select remove from Strip the category base (usually /category/) from the category URL.
Regarding the tag removal I did not found any solution yet.
Whilst you dismiss it as a solution, the plugin is by far the easiest and most consistent method and they don't change any WordPress default files.
http://wordpress.org/plugins/wp-no-category-base/
It hasn't needed to be updated for a year, so it is not exactly creating any problems with updates.
There is no simple hand rolled solution that will do all of this that does not just replicate what the plugin does from within your own functions.php
Better and logical permalinks like myblog.com/my-category/ and myblog.com/my-category/my-post/.
Simple plugin - barely adds any overhead.
Works out of the box - no setup needed. No need to modify
WordPress files.
Doesn't require other plugins to work.
Compatible with sitemap plugins.
Works with multiple sub-categories.
Works with WordPress Multisite.
Redirects old category permalinks to the new ones (301 redirect, good for SEO).
Plus you get the benefit that if WordPress does change, then the plugin will be updated to work whilst you would then have to figure out how to fix your own code on your own.
Set Custom Structure: /%postname%/
Set Category base: . (dot not /)
Save. 100% work correctly.
instead put this in your functions.php
works fine, no redirect problems.
function fix_slash( $string, $type )
{
global $wp_rewrite;
if ( $wp_rewrite->use_trailing_slashes == false )
{
if ( $type != 'single' && $type != 'category' )
return trailingslashit( $string );
if ( $type == 'single' && ( strpos( $string, '.html/' ) !== false ) )
return trailingslashit( $string );
if ( $type == 'category' && ( strpos( $string, 'category' ) !== false ) )
{
$aa_g = str_replace( "/category/", "/", $string );
return trailingslashit( $aa_g );
}
if ( $type == 'category' )
return trailingslashit( $string );
}
return $string;
}
add_filter( 'user_trailingslashit', 'fix_slash', 55, 2 );
The dot trick will likely ruin your rss feeds and/or pagination. These work, though:
add_filter('category_rewrite_rules', 'no_category_base_rewrite_rules');
function no_category_base_rewrite_rules($category_rewrite) {
$category_rewrite=array();
$categories=get_categories(array('hide_empty'=>false));
foreach($categories as $category) {
$category_nicename = $category->slug;
if ( $category->parent == $category->cat_ID )
$category->parent = 0;
elseif ($category->parent != 0 )
$category_nicename = get_category_parents( $category->parent, false, '/', true ) . $category_nicename;
$category_rewrite['('.$category_nicename.')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?category_name=$matches[1]&feed=$matches[2]';
$category_rewrite['('.$category_nicename.')/page/?([0-9]{1,})/?$'] = 'index.php?category_name=$matches[1]&paged=$matches[2]';
$category_rewrite['('.$category_nicename.')/?$'] = 'index.php?category_name=$matches[1]';
}
global $wp_rewrite;
$old_base = $wp_rewrite->get_category_permastruct();
$old_base = str_replace( '%category%', '(.+)', $old_base );
$old_base = trim($old_base, '/');
$category_rewrite[$old_base.'$'] = 'index.php?category_redirect=$matches[1]';
return $category_rewrite;
}
// remove tag base
add_filter('tag_rewrite_rules', 'no_tag_base_rewrite_rules');
function no_tag_base_rewrite_rules($tag_rewrite) {
$tag_rewrite=array();
$tags=get_tags(array('hide_empty'=>false));
foreach($tags as $tag) {
$tag_nicename = $tag->slug;
if ( $tag->parent == $tag->tag_ID )
$tag->parent = 0;
elseif ($tag->parent != 0 )
$tag_nicename = get_tag_parents( $tag->parent, false, '/', true ) . $tag_nicename;
$tag_rewrite['('.$tag_nicename.')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?tag=$matches[1]&feed=$matches[2]';
$tag_rewrite['('.$tag_nicename.')/page/?([0-9]{1,})/?$'] = 'index.php?tag=$matches[1]&paged=$matches[2]';
$tag_rewrite['('.$tag_nicename.')/?$'] = 'index.php?tag=$matches[1]';
}
global $wp_rewrite;
$old_base = $wp_rewrite->get_tag_permastruct();
$old_base = str_replace( '%tag%', '(.+)', $old_base );
$old_base = trim($old_base, '/');
$tag_rewrite[$old_base.'$'] = 'index.php?tag_redirect=$matches[1]';
return $tag_rewrite;
}
// remove author base
add_filter('author_rewrite_rules', 'no_author_base_rewrite_rules');
function no_author_base_rewrite_rules($author_rewrite) {
global $wpdb;
$author_rewrite = array();
$authors = $wpdb->get_results("SELECT user_nicename AS nicename from $wpdb->users");
foreach($authors as $author) {
$author_rewrite["({$author->nicename})/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$"] = 'index.php?author_name=$matches[1]&feed=$matches[2]';
$author_rewrite["({$author->nicename})/page/?([0-9]+)/?$"] = 'index.php?author_name=$matches[1]&paged=$matches[2]';
$author_rewrite["({$author->nicename})/?$"] = 'index.php?author_name=$matches[1]';
}
return $author_rewrite;}
add_filter('author_link', 'no_author_base', 1000, 2);
function no_author_base($link, $author_id) {
$link_base = trailingslashit(get_option('home'));
$link = preg_replace("|^{$link_base}author/|", '', $link);
return $link_base . $link;
}
The non-category plugin did not work for me.
For Multisite WordPress the following works:
Go to network admin sites;
Open site under \;
Go to settings;
Under permalinks structure type /%category%/%postname%/.
This will display your url as www.domainname.com/categoryname/postname;
Now go to your site dashboard (not network dashboard);
Open settings;
Open permalink. Do not save (the permalink will show uneditable field as yourdoamainname/blog/. Ignore it. If you save now the work you did in step 4 will be overwritten. This step of opening permalink page but not saving in needed to update the database.
If you're still searching for the combination (tags, categories and pages on the url-base), you can do it like I did.
Open the settings for permalinks and set a dot (.) for the category- and tag-base (https://premium.wpmudev.org/blog/removing-category-base-urls-wordpress/)
Install the plugin wp-no-tag-base
Tested using Wordpress 3.9.1
If you have pages, categories or tags having the same name, the system will take:
tag
page
category
https://wordpress.org/plugins/remove-category-url/
Use this plugin it does the job perfectly of hiding the category-base
It does not require any setting just install and activate.
Select Custom Structure in permalinks and add /%category%/%postname%/ after your domain. Adding "/" to the category base doesn't work, you have to add a period/dot. I wrote a tutorial for this here: remove category from URL tutorial
I don´t know how to do it using code, but for those who don't mind using a plugin. This is a great one that works for me:
https://es.wordpress.org/plugins/permalink-manager/
Modifying WP core files doesn't seems to be a solution for removing the category prefix. Also the "." fix via the Permalinks doesn't seems to work.
I believe its better to have this set via Yoast SEO plugin or Rank Math SEO Plugin, hoping almost all WordPress sites have either one of this plugin for SEO purpose.
No complicated steps, just a few mouse clicks and eventually forget about it.
If you are using Yoast SEO Plugin,
Yoast SEO > Search Appearance > Taxonomies
If you are using Rankmath SEO Plugin,
Rankmath > General Settings > Strip Category Base
And here is a dedicated plugin that meets the purpose: https://wordpress.org/plugins/remove-category-url/ , if that helps someone.
updated answer:
other solution:
In wp-includes/rewrite.php file, you'll see the code:
$this->category_structure = $this->front . 'category/';
just copy whole function, put in your functions.php and hook it. just change the above line with:
$this->category_structure = $this->front . '/';
add_action( 'init', 'remove_category_perma' );
function remove_category_perma() {
unset($GLOBALS['wp_rewrite']->extra_permastructs['category']);
}
WordPress 5.0.2:
To remove category slug from existing posts, do this :
Navigate to Settings > Permalinks and change Custom Structure from /%category%/%postname%/ to: /%postname%/
Keep Category and Tag bases empty (which is the default also)
Save
All posts can now be directly accessed via domain.com/%postname%/ and all categories can be accessed via domain.com/category/xyz/. WordPress will automatically add all the 301 redirects for the old urls. So, if someone accesses domain.com/%category%/%postname%/, they will automatically get redirected to domain.com/%postname%/.
Adding "." or "/" won't work if you want a consolidated blog view. Also, I have know idea what that solutions would do for the RSS or XML feeds. I feel better sticking with the WP convention. However, I did come up with a more elegant approach.
First, I name the base category url "blog"
Then I created a category called "all". Finally, I but all my subcategories under "all". So I get a url structure like this.
/blog - 404 - recommend 301 redirect to /blog/all/
/blog/all/ - all posts combined.
/blog/all/category1/ - posts filtered by category1
/blog/all/category2/ - posts filterer by category2
I put a custom label on the menu item called "Blog", but it goes to blog/all. It would be a good idea to 301 redirect /blog to /blog/all in the .htaccess file to avoid the 404 on /blog.
I have created a small wordpress plugin that displays a list of people in a page via shortcode.
When the user clicks on one of the names from the list, a query_var gets set and my plugin catches the $_GET with the specific id of the person the user just clicked. All very well until now.
My problem is that now I want to display a page with the details (for the clicked element) but I dont seem to be able to edit the content or post that gets to the page and it returns me to the page with the list of people.
My question is how do I edit the post? I have tried adding a add_filter('the_content','my_func') to this, but this does not work since this hook is probably already passed.
I can access the post directly via get_content() or get_post(), but I dont seem to be able to make the page populated new data.
In other words... this does nore seem to work
$fid = $_GET['fid'];
global $wpdb;
$sql = "select * from fighters where fighter_id = {$fid} limit 1";
$fighter = $wpdb->get_row($sql);
$html = $this->_getFighterPageLayout($fighter);
$post = get_post();
$post->post_content = $html;
$post->title = 'test';
$post->private = false;
// or even just global $content = $html;
What am I doing wrong and what ways do I have to edit/update the content/post?
You have to use the hooks of Wordpress to update the content. This works with the add_filter function
Try something like this, it should works
function mytheme_content_filter( $content ) {
// Do stuff to $content, which contains the_content()
// Then return it
return $content;
}
add_filter( 'the_content', 'mytheme_content_filter' );
I have tried everything I know how on Drupal to confirm my search results page to show the pager at the bottom. I work at a library and know there are several pages that mention library but only ten will show up on the first page which is expected but there is no option to view anymore after the first page. I've searched on Drupal and looked at the code and it appears to be the same, however, I'm unable to get the pager functionality to work.
Any suggestions?
if you are trying to make query resut as a pager try to do as the following
$query = db_select('node', 'n')->extend('PagerDefault');
->condition('type', 'blog') // you query condition
->fields('n', array('title')) // the fields that you request from database
->limit(10); // the number of result/row per page
$queryresult = $query->execute() ;
$content = '';
foreach ($queryresult as $row) {
$content .= $row->title."<br/>"; // the row view content
}
$content .= theme('pager'); // this will display the pagination content
echo $content;