register post type:
<?php
namespace BLANPI\App;
defined('ABSPATH') || exit;
use BLANPI\App\Traits\Singleton;
class Blanpi_Post_Type
{
use Singleton;
protected function __construct()
{
$this->setup_hooks();
}
protected function setup_hooks()
{
add_action('init', [$this, 'blanpi_create_post_type']);
}
public static function blanpi_create_post_type()
{
$work_args = array(
'labels' => array(
'name' => __('Work', 'blanpi'),
'singular_name' => __('Work', 'blanpi'),
'menu_name' => __('Work', 'blanpi'), 'admin menu',
'name_admin_bar' => __('Work', 'blanpi'),
),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => true,
'menu_position' => null,
'show_in_rest' => true,
'supports' => array('title', 'editor', 'post-formats', 'author', 'custom-fields'),
'menu_icon' => 'dashicons-format-gallery'
);
register_post_type('work', $work_args);
}
}
rewrite:
<?php
namespace BLANPI\App;
defined('ABSPATH') || exit;
use BLANPI\App\Traits\Singleton;
class Blanpi_Setup
{
use Singleton;
protected function __construct()
{
$this->setup_hooks();
}
protected function setup_hooks()
{
add_filter('post_link', [$this, 'custom_permalink'], 10, 3);
add_filter('post_type_link', [$this, 'custom_permalink'], 10, 3);
add_action('init', [$this, 'custom_rewrite_rules']);
}
public function custom_rewrite_rules()
{
add_rewrite_rule(
'^work/([^/]+)/?$',
'index.php?post_type=work&p=$matches[1]',
'top'
);
add_rewrite_rule(
'^work/([^/]+)/([^/]+)/?$',
'index.php?post_type=work&name=$matches[2]',
'top'
);
}
public function custom_permalink($permalink, $post, $leavename)
{
if ($post && $post->post_type == 'work') {
$permalink = home_url('/') . substr(md5($post->ID), 0, 18) . '/';
}
return $permalink;
}
}
I want change the url link this:
https://webiste.com/8212a829abo92822kl
]]>I’ve setup Permalinks like this:
Custom Structure /magazine/%postname%/
Category base magazine
Category archive seems to work fine but single posts return 404 Page not found.
Is there any way to make it work together?
]]>import { Routes, Route, Link, BrowserRouter } from "react-router-dom";
function Home() {
return <h1>Home</h1>;
}
function Page1() {
return <h1>Page 1</h1>;
}
function Page2() {
return <h1>Page 2</h1>;
}
function App() {
return (
<BrowserRouter>
<nav>
<Link to="/page1">Page 1 </Link>
<Link to="/page2">Page 2</Link>
</nav>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/page1" element={<Page1 />} />
<Route path="/page2" element={<Page2 />} />
</Routes>
</BrowserRouter>
);
}
export default App;
I have chosen the string “test” as URL slug. But I don’t want to use the app via the urls “localhost/test” or “localhost/test/page1”. Instead they should be accessed via “localhost” or “localhost/page1”. So I added the following rewrite rule to my function.php file:
function rewrite_route() {
add_rewrite_rule('^(.+)', 'index.php?pagename=test', 'top');
}
add_action('init', 'rewrite_route');
This rule is intended to redirect a route like “localhost/page1” to the react app. Here comes the problem: In the wordpress dashboard at “Settings -> Reading -> Your homepage displays” there are two options available:
First Option: Your latest posts
With this option the routes “localhost/page1” and “localhost/page2” work as intended and show the pages from the react app. But of course the route “localhost” leads to page with the latest posts.
Second option: A static page (with Homepage set to “test”)
With this option the route “localhost” works as intended and shows the root page page of the react app. But the routes “localhost/page1” and “localhost/page2” don’t work anymore, as they deliver the same page as the route “localhost”.
So my question is the following: Is it possible with one of the two options enabled that the routes “localhost”, “localhost/page1” and “localhost/page2” show the content of my react app?
]]>Many thanks for your thoughts or tipps on solving my situation.
Andrew
example.com/brand/brand-name/category/category-name/
So what I was thinking of doing is what is in the code I wrote:
function brand_rewrite_rule() {
add_rewrite_rule( '^brand/([^/]*)/category/([^/]*)/?', 'index.php?pwb-brand=$matches[1]&product_cat=$matches[2]', 'top');
}
add_action('init', 'brand_rewrite_rule', 10, 0);
And it works great.
The only problem is in the pagination, going to page 2 he presents the same products, of the first page, at this link:
example.com/brand/brand-name/category/category-name/page/2/
Thanks!
]]>I want below URL:
https://www.example.com/shop/keyword/
to work as
https://www.example.com/shop/?search_text=keyword
I can achieve this behaviour for my /search/
as below. But this is not working for /shop/
page. What is the way to achieve this ?
function my_rewrite_rule() {
add_rewrite_rule('^search/([^/]*)?','index.php?pagename=search&search_text=$matches[1]','top');
}
but same code is not working for shop page.
]]>site.com/page-name/page/2/paged2/1
so I used this:
<?php
global $wp_rewrite, $wp_query_videos;
$wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;
//$paged2 = isset( $_GET['paged2'] ) ? (int) $_GET['paged2'] : 1;
(isset($wp_query->query_vars['paged2']) ? ($wp_query->query_vars['paged2'] > 1 ? $current2 = $wp_query->query_vars['paged2'] : $current2 = 1) : $current2 = 1) ;
$big = 999999999; // need an unlikely integer
$classiera_pagination = array(
//'base' => str_replace($big,"{$current}/paged2/%#%", esc_url( get_pagenum_link( $big ) ) ),
'format' => 'page/%#%',
'total' => $wp_query_videos->max_num_pages,
'current' => $current2,
'show_all' => false,
'type' => 'plain',
//'add_args' => array( 'paged' => $current, 'paged2' => $paged2)
);
if( $wp_rewrite->using_permalinks() )
$classiera_pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg('s',get_pagenum_link(1) ) ) . "page/{$current}/paged2/%#%", 'paged');
/*
if( !empty($wp_query->query_vars['s']) )
$classiera_pagination['add_args'] = array('s'=>get_query_var('s'));
*/
echo '<div class="pagination">' . paginate_links($classiera_pagination) . '</div>';
?>
and this rules of rewriting
function wpse21802_init(){
global $wp_rewrite;
$wp_rewrite->flush_rules();
add_rewrite_rule( '([^/]+)/page/([^/])/?$', 'index.php?pagename=$matches[1]&paged=$matches[2]');
add_rewrite_rule( '([^/]+)/page/([^/])/paged2/([^/])/?$', 'index.php?pagename=$matches[1]&paged=$matches[2]&paged2=$matches[3]');
}
add_action( 'init', 'wpse21802_init', 10, 0);
but a problem happens … after I use the page once to go for example to page 2
site.com/page-name/page/1/paged2/2/
the paging links are getting like this for page 1
site.com/page-name/page/1/paged2/2/page/1/paged2/1/
This is generating error at the time of paging. What may be the problem?
I am trying to customize the url using rewrite rule in wordPress.
Rewrite rule working fine if the url contains only the English character but when i am adding Hebrew value it not giving the parameter value.
I am accessing the url parameter value using
global $wp_query;
$category_name_data = $wp_query->query_vars['category_id'];
if i print this $category_name_data veritable its adds character like
-%D7%91%D7%A8%D7%96 insted of Hebrew word.