public static function pagination( $query ) {
$total_pages = $query->max_num_pages;
$permalink_structure = get_option( 'permalink_structure' );
$paged = FLBuilderLoop::get_paged();
$base = html_entity_decode( get_pagenum_link() );
$add_args = false;
if ( $total_pages > 1 ) {
if ( ! $current_page = $paged ) { // @codingStandardsIgnoreLine
$current_page = 1;
}
$base = FLBuilderLoop::build_base_url( $permalink_structure, $base );
$format = FLBuilderLoop::paged_format( $permalink_structure, $base );
// Flag if it's a first posts module in an archive page.
// Fix pagination issues in archive page since it's using the main WP query for pagination.
if ( $query->is_archive && 1 === FLBuilderLoop::$loop_counter ) {
if ( isset( $query->query['settings']->data_source ) && 'custom_query' === $query->query['settings']->data_source ) {
$add_args = array(
'flpaging' => 1,
);
}
}
// phpcs:ignore
echo paginate_links(
array(
'base' => $base . '%_%',
'format' => $format,
'prev_text' => '<span class="btn btn-secondary btn-sm">' . __( 'Newer posts', 'memoq_bb' ) . '</span>',
'next_text' => '<span class="btn btn-secondary btn-sm">' . __( 'Older posts', 'memoq_bb' ) . '</span>',
'current' => $current_page,
'total' => $total_pages,
'type' => 'plain',
'add_args' => $add_args,
)
);
}
The link on the older posts button is https://mysite.com/resources/ebooks/page/2 .
Post type slug: ebook
Has archive: false
Custom rewrite slug: resources/ebooks
When I have the Rewrite option set to true, the pagination button link goes to 404, when it is set to false, it works fine. Both times the link on the button is the same.
]]>ex: xxx.site.com/shop/[product_name] -> default language
xxx.site.com/fr/store/[product_name] -> fr lang
First of all, in Permalink options, I can set only static string, like shop or store (/shop/%product_cat%).
So i came up with an idea to add custom tag (%shoppage%).
Now permalink setting looks like (/%shoppage%/%product_cat&)
Here’s the code
function create_tags($permalink, $post) {
if ( $post->post_type == 'product' ){
if (function_exists('pll_get_post')) {
$shoppage = pll__('shop');
} else {
$shoppage = 'shop';
}
$find = '%shoppage%';
$replace = $shoppage;
$permalink = str_replace( $find, $replace, $permalink );
}
return $permalink;
}
add_filter( 'post_type_link', 'create_tags', 6, 2 );
of course it did the job with replacing %shoppage% to translated string, but still that string is not recognized in register_post_type (from woocommerce), and it end with 404 error page. (rewrite rules are not working)
add_rewrite_tag
wont work aswell
Is there any way to achieve my idea?
Sincerely,
Dersime
i got this code here on the forum and it’s working fine.
add_filter('rewrite_rules_array','my_insert_rewrite_rules');
add_filter('query_vars','my_insert_query_vars');
add_action('wp_loaded','my_flush_rules');
// flush_rules() if our rules are not yet included
function my_flush_rules(){
$rules = get_option('rewrite_rules');
if(!isset($rules['(whats-new)/page-(\d*)$']) || !isset($rules['(whats-new)/page-(\d*)/(\d*)$'])){
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
}
// Adding a new rule
function my_insert_rewrite_rules($rules)
{
$newrules = array();
$newrules['(whats-new)/page-(\d*)/?$'] = 'index.php?pagename=$matches[1]¶m1=$matches[2]';
return $newrules + $rules;
}
// Adding the id var so that WP recognizes it
function my_insert_query_vars($vars)
{
array_push($vars, 'param1');
return $vars;
}
my problem is, i want to have multiple parameters and i don’t know how.
i already tried this one but it’s not working fine.
add_filter('rewrite_rules_array','my_insert_rewrite_rules');
add_filter('query_vars','my_insert_query_vars');
add_action('wp_loaded','my_flush_rules');
// flush_rules() if our rules are not yet included
function my_flush_rules(){
$rules = get_option('rewrite_rules');
if(!isset($rules['(whats-new)/page-(\d*)$']) || !isset($rules['(whats-new)/page-(\d*)/(\d*)$'])){
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
}
// Adding a new rule
function my_insert_rewrite_rules($rules)
{
$newrules = array();
$newrules['(whats-new)/page-(\d*)/?$'] = 'index.php?pagename=$matches[1]¶m1=$matches[2]';
$newrules['(whats-new)/page-(\d*)/(\d*)/?$'] = 'index.php?pagename=$matches[1]¶m1=$matches[2]¶m2=$matches[3]';
return $newrules + $rules;
}
// Adding the id var so that WP recognizes it
function my_insert_query_vars($vars)
{
//$vars[] = 'param1';
//$vars[] = 'param2';
array_push($vars, 'param1');
array_push($vars, 'param2');
return $vars;
}
thank you in advance!!
more powers,.
I have in page in wordpress – “sing” https://127.0.0.1/wordpress/sing/
I creat custom rewrite
add_action(‘generate_rewrite_rules’, ‘work_list’);
function work_list($wp_rewrite) {
$newrules = array();
$newrules[‘sing/music/’] = ‘index.php?clients=okay])’;
$wp_rewrite->rules = $newrules + $wp_rewrite->rules;
}
whem am in page in firebug say this page is 404
I’ve been trying to figure out the wordpress rewrite engine during the last two days. My aim is / was to create a “trackback” like uri endpoint which doesnt need any parameters to function and thereby manage template redirects for a custom print view page. I wanted to keep it as simple as possible and thus tried to use the function add_rewrite_endpoint()
.
This is what I got so far:
function printview_activation()
{
global $wp_rewrite;
add_rewrite_endpoint('printview', EP_ALL);
$wp_rewrite->flush_rules();
}
register_activation_hook( __FILE__, 'printview_activation');
function printview_deactivation()
{
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
register_deactivation_hook( __FILE__, 'printview_deactivation');
function printview_query_vars($vars)
{
array_push($vars, 'printview');
return $vars;
}
add_filter('query_vars','printview_query_vars');
The cool thing is: It works and it REALLY is a great alternative to creating complex custom rewrite rules (it was quite hard to figure out the whole functionality of the rewrite engine since there are no real tutorials on the web). One problem remains however and since I am kind of a perfectionist I would really like to make that last step too:
In order to make it function the URL has to look like https://mywordpress.com/random-page/printview/1 (or any other parameter) since wordpress doesnt include any endpoints that have no value in the query string. ($wp_query->query_vars[‘printview’]; returns null if it doesnt have a value).
And here comes my question: Is there an easy way to make it work so I dont have to use any parameters on the printview endpoint without using the generate_rewrite_rules function?
This is a post I found on a similiar topic (it explains why the query_var only gets set when it has a value): https://old.nabble.com/Rewrite-endpoints-td25232037.html
Maybe someone on this forum nows a nice approach on how to make wordpress recognize the query_var even if its “empty” (like it does for “/trackback/”) without having to change the core files.
It should work the same way “trackback” works: “If ‘/trackback/’ exists make it ‘trackback = 1’ (‘tb = 1’ in the code)”.
Thanks!
]]>