How to add .html extension only to pages without child pages?

I think you can achieve the desired functionality by selectively adding the .html extension to specific pages using a custom rewrite rule and the page_link filter.

// Add a custom rewrite rule to handle pages with .html extension
function custom_html_page_rewrite_rule() {
    add_rewrite_rule(
        '^([^/]+)(\.html)/?$',
        'index.php?pagename=$matches[1]',
        'top'
    );
}
add_action('init', 'custom_html_page_rewrite_rule', 10);

// Filter page links to append .html only to the specified pages
function append_html_extension_to_pages($link, $post) {
    $pages_to_add_html = array(
        'page1',
        'child-page1',
        'child-page2',
        'child-page3',
        'grand-child-page1',
        'grand-child-page2'
    );

    if (in_array($post->post_name, $pages_to_add_html)) {
        $link = trailingslashit($link) . $post->post_name . '.html';
    }

    return $link;
}
add_filter('page_link', 'append_html_extension_to_pages', 10, 2);

Replace the page names in the $pages_to_add_html array with the actual slugs of the pages you want to have the .html extension. This code will add the .html extension only to the specified pages and their child/grandchild pages while keeping other pages without the extension. Make sure to flush the rewrite rules by visiting the Permalink settings page in your WordPress admin after adding this code.

However, a more elegant and flexible approach would be to tag the pages you want to have the .html extension with a custom field or category and then use that criteria to determine which pages should get the extension. Here’s an updated answer with this method:

// Add a custom rewrite rule to handle pages with .html extension
function custom_html_page_rewrite_rule() {
    add_rewrite_rule(
        '^([^/]+)(\.html)/?$',
        'index.php?pagename=$matches[1]',
        'top'
    );
}
add_action('init', 'custom_html_page_rewrite_rule', 10);

// Filter page links to append .html to pages with a specific tag/category
function append_html_extension_to_pages($link, $post) {
    $html_extension_tag = 'add-html-extension'; // Customize this to your tag or category name

    if (has_term($html_extension_tag, 'post_tag', $post) || has_term($html_extension_tag, 'category', $post)) {
        $link = trailingslashit($link) . $post->post_name . '.html';
    }

    return $link;
}
add_filter('page_link', 'append_html_extension_to_pages', 10, 2);

In this approach:

  1. You create a custom tag or category (e.g., ‘add-html-extension’) and assign it to the pages you want to have the .html extension.

  2. Modify the $html_extension_tag variable to match the name of your custom tag or category.

  3. The code checks if a page has the specified tag or category, and if so, it appends the .html extension to the page link.

This way, you can easily control which pages get the extension by tagging or categorizing them appropriately in the WordPress admin without hard-coding page slugs.

deneme bonusudeneme bonusu veren sitelerpulibet girişOnwin Güncel Giriştürkçe altyazılı pornocanlı bahis casino