Replacing Wordpress Category with Static Page? - wordpress

I'm rehashing a Wordpress question from 4 years ago that had an approved answer, but that solution no longer works in the current version of Wordpress. The original question: Make Wordpress use the page instead of category
Background (copied from originally approved answer):
example.com is the domain
I have a WP page called "foobar" with content
I have a WP post category called "foobar"
I have a WP post entitled "fun things to do with foobars", and the category is set to "foobar"
Expectations
When I go to example.com/foobar, I want to see the page about foobars, not a WP category page that shows all blog posts with that category.
When I go to the blog post about fun things, the URL is example.com/foobar/fun-things-to-do-with-foobars/
The solution at the time
There appeared to be a work-around with a custom permalink structure and using "." (no quotes) as the category base. That solution does not work with the current version of Wordpress.
There were also a couple of answers that suggested various plugins. I'm not overtly opposed to that route, but the suggested plugins at the time seem to have been affected by the same update that negated the accepted answer.

To load page with the same slug as of category name you could use following code snippet that replace category with Page.
function wpa_alter_cat_links( $termlink, $term, $taxonomy ){
if( 'category' != $taxonomy ) return $termlink;
return str_replace( '/category', '', $termlink );
}
add_filter( 'term_link', 'wpa_alter_cat_links', 10, 3 );
Be aware with the code. You should have pages for all your categories otherwise you will get 404 for those categories which have not created pages.
After adding above code into your activated theme's functons.php file please flush the permalink. You can do that with visiting settings -> permalinks -> click save changes
Hope it works for you!

Related

Replace Blog Post Author link with Team Member link (CPT + ACF)

I'm building a website that has "Team Members" section and "Blog" section. Team members are a custom post type.
The client requested that the links to authors in blog posts should point to associated Team Member page instead of the default author page in WordPress (basically they don't want to have default author pages at all).
I've found this solution offered by #Damocles - Use "Team Members" custom post type instead of Author for blog posts
Basically the solution proposed by him was simple and exactly as I thought initially of tackling this issue:
Create a "Post Object" ACF field and set it to filter through "Team Member" custom post types
Attach this field to User accounts
Go to user profile and choose the correct Team Member from drop down menu
Then use a filter in functions.php to automatically replace the author link everywhere with associated Team Member url
Makes sense but unfortunately, it doesn't want to work on my website. I even used the same name for ACF field as he did and used the exact same code in functions.php:
add_filter( 'author_link', 'team_author_link', 10, 3 );
function team_author_link( $link, $author_id, $author_nicename ) {
$team_post_id = get_field('team_post', $author_id);
// if the team post is set, get the permalink to the team post:
$team_link = get_permalink($team_post_id);
$link = ($team_link !== false) ? $team_link : $link;
return $link;
}
The author link DOES change, BUT instead of pointing to the associated Team Member page, all author links point to the currently opened blog post URL. I don't know, maybe my theme is overwriting the query or something, so the URL to the custom post type cannot be achieved from blog post view?
Can someone help me achieve it, please? I want to attach a Team Member (custom post type) to user account in WordPress and replace the author link through functions.php to the associated Team Member page url.
It's because you are missing a prefix, to tell ACF that it should be looking on a user.. - try this
$team_post_id = get_field('team_post', 'user_'.$author_id);
Docs are here : https://www.advancedcustomfields.com/resources/how-to-get-values-from-a-user/

How to add a child page to a Custom Post Type "Post"

Friends, Hello.
I have an issue, i'm searching for an answer for 2 days now. I think I have checked all the 10 firsts google pages of 20 different requests by now.
So, this is it.
I have a custom post type named "Footballer".
I have created 2 "Posts" of that CPT, which are "Messi" and "Ronaldo".
My permalinks for these 2 footballers are "mysite.com/footballer/messi/" and "mysite.com/footballer/ronaldo/". It is OK for me.
Now, I want to add a child page "about" (or "stats", or anything) to my footballers and I want this page to be available by the link "mysite.com/footballer/messi/about" (just add the child page slug after the cpt post link).
Obviously, I want to create a single page "about", not one for every footballer I have, and in this page I would retrieve data for the specific footballer (by functions/shortcodes).
Nota: the cpt and values in this post are fictionnals, I know I would display the about/stats directly in the footballer cpt post. I am just trying to explain what I need -> a generic child page for a cpt, available by "mysite.com/cpt/cptpost/childpage".
How can I do it ? I s it even possible ?
Thanks for your help.
EDIT ;
As it is not possible with Divi, can I try to rewrite the URL of my subpage ? If my subpage url is "mysite/subpage/?foot=messi", can I make it appear "mysite/footballer/messi/subpage".
I tried this code, without success
function my_rewrite_url() {
add_rewrite_tag( '%foot%','([^&]+)' );
add_rewrite_rule(
'footballer/([^/]+)/subpage',
'index.php?pagename=subpage&foot=$matches[1]',
'top'
);
}
add_action( 'init', 'my_rewrite_url' );
You are looking for custom taxonomies on the custom post type.

Why are my Woocommerce permalinks conflicting with main website pages?

Was wondering if someone could point me in the right direction.
My current Woocommerce Permalink settings are as follows:
SHOP
www.mywebsite.com/shop
SHOP WITH SUB CATEGORY
www.mywebsite.com/seasons/winter
Unfortunately, the above SHOP WITH SUB CATEGORY link also works without the product category - i.e. www.mywebsite.com/winter - for some reason.
Which is annoying as I also have a main page in my website called Winter. (i.e. mywebsite.com/winter).
I have tried various versions of the COMMON, OPTIONAL and PRODUCT permalinks, but no matter what I type, www.mywebsite.com/winter always points to the Woocommerce Shop page and not the normal website page.
Any ideas?
Huge thank you in advance :)
Steven
What happens when you use this? This code stops WordPress from guessing where to go.
function no_redirect_guess_404_permalink( $header ){
global $wp_query;
if( is_404() )
unset( $wp_query->query_vars['name'] );
return $header;
}
add_filter( 'status_header', 'no_redirect_guess_404_permalink' );
Original answer

Show custom post archive when custom post not specified

I have a custom post type called produce set up in WordPress and a custom taxonomy called produce_category.
The URL format for posts in this custom post type is in this format http://mywebsite/produce/%produce_category%/%postname%/. Of course %produce_category% and %postname% get substituted accordingly, a working example url would be http://mywebsite/produce/fruits-and-vegetables/avocado.
What I would like to do is show the produce_category custom taxonomy archive if a user visits http://mywebsite/produce/%produce_category%, without specifying post name at the end, e.g http://mywebsite/produce/fruits-and-vegetables to show all produce in fruits-and-vegetables produce_category.
Besides that when a user visits http://mywebsite/produce/ I would like to show all the produce archive. EDIT: I have this working now.
I know how to create the archive pages totally fine and have no problem with that. I am stuck at creating permalinks. When I visit http://mywebsite/produce/%produce_category% I get a 404 error.
I'm looking for advise on the best way to implement this. Currently I am using Custom Post Type Permalinks and CPTUI.
The CPTUI custom taxonomy settings interface does not allow me to have a blank in the custom rewrite slug. It defaults to the custom taxonomy slug, produce_category, when I don't fill in anything.
This gives the front-side produce_category taxonomy archive url as http://mywebsite/produce/produce_category/%produce_category%/ e.g. http://mywebsite/produce/produce_category/fish-and-seafood/ when what I want for the archive is http://mywebsite/produce/fish-and-seafood/.
Please help with suggestion on the best way I can achieve the custom taxonomy url.
Thank you all.
Try this code. It will help you to achieve your url structure... Make sure you update permalinks after saving it to functions.php
function custom_produce_category_link( $link, $term, $taxonomy )
{
if ( $taxonomy !== 'produce_category' )
return $link;
return str_replace( 'produce_category/', '', $link );
}
add_filter( 'term_link', 'custom_produce_category_link', 10, 3 );

How to replace old external links with new ones in all Wordpress posts automatically?

How to replace old external links with new ones in all Wordpress posts automatically?
The links are in [embed] codes in the posts.
http://old-232.external.link.com/folder/etc.mp4
with
http://newexternallink.com/~newfolder/folder/etc.mp4
Note: the new external link is just an IP address.
Note2: the external links are in some of the posts.
If you take a backup of your database before trying, you may try a search and replace, for example by using a tool plugin like Better Search Replace
I guess there are several other options, but this is one of the plugins which gives you a preview on what is going to change, before doing the actual replacement.
If you add more specific details on what your replacing, you may get more spesific answers as well.
You can do this pretty easy with a simple plugin. Use the WordPress filter the_content
function my_content_filter($content){
//only add text before WordPress posts
if(is_single() && is_main_query()){
//reply all occurances of the old URL with the new URL
$content = str_replace([OLD_LINK], [NEW_LINK],$content);
}
return $content;
}
add_filter( 'the_content', 'my_content_filter' );
For more on this check out my post on how to Add Text Before And After Content In WordPress . Part 5 is about replacing content in all your posts or pages.

Resources