How to change custom WordPress permalinks? - wordpress

I have a custom theme whereas the format of custom post is http://www.example.com/coupon/store-name/title-name
Is there a way to overwrite in whole site pages and change to http://www.example.com/deals/title-name
I tried Permalinks and Custom Post Type Permalinks and it didn't work.

A custom post type is registered with the register_post_type function and its URL slug is set with its rewrite arg. The good news is that you can use the register_post_type function to modify an existing post type.
You are going to need to:
get the args which were used to register this post type. You can use the get_post_type_object function for this
modify these as needed, that is, add your rewrite rules
call register_post_type with modified args

Related

Want to redirect a Wordpress URL to a specific page template and pass name of post

I've got a Wordpress site set up with a custom post type and custom taxonomy and custom URLs. However when I go to:
/custom-post-type/custom-taxonomy/custom-taxonomy-sub/postname
It 404s. I've tried everything and just want to create a redirect that grabs the postname and passes it to single-custom-taxonomy.php without changing the URL.
Some sort of redirect with a regex like /custom-post-type/(.*)/(.*)/(.*) and pass the third match to single-custom-taxonomy.php as a variable so it can get the post page.
How do I do this?
Figured it out:
function custom_rewrite_basic() {
add_rewrite_rule('^knowledge-center/(.*)/(.*)/(.*)', 'index.php?pagename=post_type=questions&questions=$matches[3]', 'top');
}
add_action('init', 'custom_rewrite_basic');

How to update update existing custom post type with register_post_type permanently?

I want to perform quick rewrite slug update for my custom post type on the fly. So I use code like in my theme's functions.php:
$no = get_post_type_object($pt_slug);
$no -> rewrite['with_front'] = false;
$no -> rewrite['slug'] = $slug;
register_post_type($pt_slug, $no );
Which is hooked to 'init' add_action('init', 'check_post_type_rewrite_url');
There is large code so I write only issue part in here.
register_post_type returns object with updated slug. I understand it means my custom post type was updated, but it was not. Existing post type still has its old rewrite slug. Should I add something special for rewrite rules to save changes and make it work? Or there is specific way to register/update post types with rewrite slug so it worked?
The way you solved the issue - by using flush_rewrite_rules - is technically correct but not recommended. As stated in the doc:
This function is useful when used with custom post types as it allows for automatic flushing of the WordPress rewrite rules (usually needs to be done manually for new custom post types). However, this is an expensive operation so it should only be used when absolutely necessary.
So you shouldn't keep your flush_rewrite_rules() call in your init hook, as it will regenerate all the rewrite rules at every page load, which is really badly ressource consuming.
Usually it's enough to just visit the Permalinks Settings page to flush the rules - so if you change again your CPT slug in the future, just visit the page once.
If your slug could change dynamically, then you can make use of flush_rewrite_rules(), but you need to use it carefully - it shouldn't be called on every page load, but could be used by a periodic cron job, or on plugin activation / deactivation, depends of the case.

add custom field template to theme admin page

in WP admin how to add the custom field template plugin to a theme page?
it automatically shows in posts and pages but i want this in the theme page. the theme am using is from iwak "creations" portfolio page.
what files do i need to modify to add this?
It's very hard to say what you need to modify without being able to look at the code. Being a premium theme, we can't just download it and take a look.
Having said that, the theme may use the WordPress custom post type functionality. Search the code for a call to the register_post_type function. If it's used, you may be in luck. Either
add 'custom-fields' to the supports argument in that call, or
call add_post_type_support after the post type is registered. The $post_type parameter will be the first value passed to the register_post_type function, and the $supports parameter will be 'custom-fields'.
Daniel, are you using this Custom Post Type Plugin - http://wordpress.org/extend/plugins/custom-field-template/screenshots/? I've used it before, and the guy who created it is Japanese, so his personal page isn't very useful as far as support for english goes.
I had some trouble with this at first. But what I think you're trying to do is add the custom fields to your new pages you've created, correct?
It's not very straightforward, but once it works it's pretty awesome.
Basically, when you set up the plugin you create these different "Custom fields," right? Well part of that should look like this:
[Custom Field Name]
type = textarea
rows = 4
cols = 40
tinyMCE = true
htmlEditor = true
Ok, so once you've created those "Custom fields" keep note of the part in brackets. You'll add this to your template pages:
<?php getCustomField('Custom Field Name'); ?>
Now when you enter the info in the pages or posts, the content should appear as you've entered it.

How to rewrite URI of custom post type?

The site that I am working on uses the following "pretty" permalink structure:
http://example.com/blog/my-special-post
But for a custom post type my client would like to avoid having a "pretty" slug:
http://example.com/product/142
How can the post ID be used in place of the slug?
I believe that this might be possible using WP_Rewrite, but I do not know where to begin.
I posted the same question on WordPress stack exchange site and received a good solution:
WordPress StackExchange - How to rewrite URI of custom post type?
All of WP's posts/pages, regardless of their type, can be reached via http://example.com/?p=foo, where foo is the post ID.
By default, redirection to a "pretty permalink" takes place though.
On the WP Codex entry for register_post_type() (I assume that's what you're using to set up your CPT, right?), the rewrite argument is described:
rewrite
(boolean or array) (optional) Rewrite permalinks with this format. False to prevent rewrite.
Default: true and use post type as slug
So, using
$args = array(
'rewrite' => false,
// ... whatever else there is
);
register_post_type('product', $args);
should yield what you're looking for.
In the WP admin you can chane the display of the permalinks by going to 'Settings->Permalinks'. To achieve your desired results, set the custom structure to /%post_id%/. Unless you manually edit .htaccess and the WP rewrite rules, I don't think you can achieve a structure where you have the post slug in the URI for one post type, but the ID for another.

custom post type template

I have a custom post type "recipes". I created it using the custom post type UI plug in.
the name is ic_recipes, the label is Recipes and the rewrite slug is recipes
the url is formed as mysite.com/recipes/lentil-soup/
Here is the question...
I wanted to create the template file for this custom posts types to use..
but no matter what i tried, wordpress ended up using the single.php.
I've supplied the following files to the twentyten theme folder;
recipes.php
ic_recipes.php
archive-recipes.php
and finally archive-ic_recipes.php
none kicked in. single.php was the one every single time.
I checked the codex. codex has the the following to say ( I copy/pasted for you at the bottom of this question) about the the template naming!
Please tell me what is it that I'm missing..
http://codex.wordpress.org/Template_Hierarchy#Custom_Post_Types_display
Custom Post Types display
archive-{post_type}.php - If the post type were product, WordPress would look for archive-product.php.
archive.php
index.php
archive-xxx.php is for archives. You want to use single-xxx.php as an alternate to single.php.

Resources