How to add multiple categories in function WordPress - php

Wordpress - functions.php
I'm using this function to automatically add custom structure in url for posts in a specific categorie.
But now I would like to know how I could add multiple categories without having to copy this function over and over.
Other solution could be to have this function work for the parent post category and all child categories.
// url structure
add_filter( 'post_link', 'custom_permalink', 10, 3 );
function custom_permalink( $permalink, $post, $leavename ) {
// Get the category for the post
$category = get_the_category($post->ID);
if ( !empty($category) && $category[0]->cat_name == "b" ) {
$cat_name = strtolower($category[0]->cat_name);
$permalink = trailingslashit( home_url('/questions/' . $post->post_name .'/' ) );
}
return $permalink;
}
add_action( 'init', 'custom_rewrite_rules' );
function custom_rewrite_rules() {
add_rewrite_rule(
'b/([^/]+)(?:/([0-9]+))?/?$',
'index.php?category_name=b&name=$matches[1]&page=$matches[2]',
'top' // The rule position; either 'top' or 'bottom' (default).
);
}

Related

Remove cpt slug and add custom taxonomy in url

I need to change the custom post type url, this code works it but is also affecting other cpts causing 404 in all other cpt, how can I make it only for the one I need.
Here is my code
// Rewrite urls of resources and put category on url
function change_custom_post_link( $post_link, $id = 0 ){
$post = get_post($id);
if ( is_object( $post ) ){
$terms = get_the_terms( $post->ID, 'resource_type' );
if(get_post_type($post->ID) == 'resource' ) {
if( $terms ){
return home_url( "/".$terms[0]->slug."/".$post->post_name );
}else{
return home_url( "/resource/".$post->post_name );
}
}
}
return $post_link;
}
add_filter( 'post_type_link', 'change_custom_post_link', 1, 3 );
and this is the rewrite
function resource_rewrite_rules() {
add_rewrite_rule(
'^(.*)/(.*)/?$',
'index.php?post_type=resource&name=$matches[2]',
'top'
);
}
add_action( 'init', 'resource_rewrite_rules' );
Why are you doing this? That's too tricky.
You may use this plugin to create and modify CPT: CPT plugin WordPress
Or refer to docs: https://developer.wordpress.org/reference/functions/register_post_type/

How to rename all action and titles of WooCommerce product post type

Im using WooCommerce Plugin, and instead of the product I want to rename all titles and actions to Donations.
this is what i have tried :
function debug_admin_menus() {
global $menu, $submenu, $pagenow;
if ( current_user_can('manage_options') ) {
if( $pagenow == 'index.php' ) { // print on dashboard
echo '<pre>', print_r( $menu, 1 ), '</pre>'; // top level menus
echo '<pre>', print_r( $submenu, 1 ), '</pre>'; // submenus
}
}
}
add_action( 'admin_notices', 'debug_admin_menus' );
// Change label
function custom_change_admin_label() {
global $menu, $submenu;
$menu[11][0] = 'Donation'; //instead Products
$submenu['edit.php?post_type=product'][5][0] = 'All Donation'; // instead all products
}
add_action( 'admin_menu', 'custom_change_admin_label' );
I feel this is a very Clumsy and long way and maybe there is a better way or a plugin to rename/translate?
add_filter('gettext', 'translate_text');
add_filter('gettext_woocommerce', 'translate_text');
add_filter('ngettext', 'translate_text');
add_filter('gettext_with_context', 'translate_text');
function translate_text($translated) {
$translated = str_ireplace('Products', 'Donations', $translated);
$translated = str_ireplace('Product', 'Donation', $translated);
return $translated;
}
Add this to your active theme functions.php file

Custom permalink load template

I created functions below to customize the permalink of a specific category. Everything is working right!
When accessing the post with this permalink example.com.br/negocios/name-post everything is ok.
But if I access these same posts through example.com.br/name-post it also loads, being that it was for 404 or redirected to the created permalink.
add_filter( 'pre_post_link', 'idinheiro_custom_permalink', 10, 3 );
function idinheiro_custom_permalink( $permalink, $post ){
$category = get_the_category($post->ID);
$slug = $category[0]->slug;
$tag = '%postname%';
if ( !empty($category) && $slug === 'negocios' ) {
$permalink = "/$slug/$tag";
}
return $permalink;
}
add_filter( 'generate_rewrite_rules', 'idinheiro_custom_rewrite_rules' );
function idinheiro_custom_rewrite_rules( $wp_rewrite ) {
$new_rules = [
'^negocios/([^/]+)/?$' => 'index.php?name=$matches[1]',
];
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
return $wp_rewrite->rules;
}

WooCommerce Shop Page Endpoint. No Products

I've been trying to add an endpoint for the shop page using this code:
add_action( 'init', 'add_city_endpoint' );
function add_city_endpoint() {
add_rewrite_endpoint( 'city', EP_PAGES );
}
to get products filtered when url is something like: /shop/city/new-york/
But I get page with no products.
How can I make it work?
Alright, I'll answer my own question.
Since the shop page is not an actual wordpress page, but an archive page for "product" post type, the endpoints don't work well for my purpose. And the proper way would be to use add_rewrite_rule instead:
add_action( 'init', 'city_add_rewrite_rules' );
function city_add_rewrite_rules() {
$shop_page = ( $shop_page_id = wc_get_page_id( 'shop' ) ) && get_post( $shop_page_id ) ? get_page_uri( $shop_page_id ) : 'shop';
add_rewrite_tag( '%sr_city%', '([^&]+)' );
add_rewrite_rule(
"^$shop_page/city/([^/]+)/?$", 'index.php?post_type=product&sr_city=$matches[1]', 'top'
);
//pagination
add_rewrite_rule(
"^$shop_page/city/([^/]+)/page/([0-9]+)?$", 'index.php?post_type=product&sr_city=$matches[1]&paged=$matches[2]', 'top'
);
}

WordPress - Ovveride plugin template in theme

I have plugin that create new post type. Also plugin set single template for it's single page.
add_filter( 'single_template', array( &$this, 'register_ipa_product_post_type_single_template' ) );
function register_ipa_product_post_type_single_template( $single_template ) {
global $post;
if ( $post->post_type == 'product' ) {
$single_template = IPA_PRODUCT_POST_TYPE_TEMPLATE_FOLDER . 'single-product.php';
}
return $single_template;
}
How i can override single-product.php in my theme.
I don't found any solutions for my question at this site.
just filter it a little later than the current function (ps if doing this within a class you need to reference it using array(&$this, 'function'). I left it out as i assume you are using the functions.php or function override....etc
add_filter( 'single_template', 'register_ipa_product_post_type_single_template', 100 );
function change_temp( $single_template ) {
global $post;
if ( $post->post_type == 'product' ) {
$single_template = 'path to your template file';
}
return $single_template;
};

Categories