admin管理员组

文章数量:1122832

I have searched plugins, tutorials and forums but I can't find a solution to what I need.

For example, I have 2 posts with different content, which are within url-1 and url-2. What I need is that url-1 shows the content that url-2 has at the permanent link level, I don't want internal functions that have to do with the $post variable or the get_post function Since I use plugin cache, these functions would not work, it would have to be at the permanent link URL level or url-rewriting.

the content of url-2 should be shown in url-1.

Graphically:

url-1 = content 1

url-2 = content 2

Now what I want is that when they enter url-1, content 2 is displayed without affecting the url

I have searched plugins, tutorials and forums but I can't find a solution to what I need.

For example, I have 2 posts with different content, which are within url-1 and url-2. What I need is that url-1 shows the content that url-2 has at the permanent link level, I don't want internal functions that have to do with the $post variable or the get_post function Since I use plugin cache, these functions would not work, it would have to be at the permanent link URL level or url-rewriting.

the content of url-2 should be shown in url-1.

Graphically:

url-1 = content 1

url-2 = content 2

Now what I want is that when they enter url-1, content 2 is displayed without affecting the url

Share Improve this question edited Sep 21, 2024 at 13:07 LΞИIИ asked Sep 18, 2024 at 15:48 LΞИIИLΞИIИ 1947 bronze badges 2
  • cache systems shouldn't interfere in your site functionalities. may be there is a configuration issue, edit your question to give us more details about the cache you use on your site. – mmm Commented Sep 19, 2024 at 16:13
  • @mmm I'm going to edit my question, but what I mean is that displaying the content of another page should not depend on whether the page is static by cache, what I want is to display the page without moving or affecting the url-1 – LΞИIИ Commented Sep 21, 2024 at 12:33
Add a comment  | 

1 Answer 1

Reset to default 0

In order to achieve this URL-level content display in WordPress without using internal functions that depend on the $post variable or get_post, we can make use of the template_redirect action along with URL rewriting.

  1. Add Rewrite Rules.
  2. Handle the Redirect.

Here’s an example of how to implement this:

// Here we have added rewrite rules.
function custom_rewrite_rules() {
    add_rewrite_rule( '^url-1/?$', 'index.php?pagename=url-2', 'top' );
}
add_action( 'init', 'custom_rewrite_rules' );

// This is to Serve content of url-2 when accessing url-1.
function load_template_for_url_1( $template ) {
    if ( is_page( 'url-1' ) ) {
        // Here we are getting the template for url-2.
        $new_template = locate_template( 'page-url-2.php' ); // We need to adjust based on your template file.
        if ( $new_template ) {
            return $new_template;
        }
    }
    return $template;
}
add_filter( 'template_include', 'load_template_for_url_1' );

// This is to flush rewrite rules on activation
function flush_rewrite_on_activation() {
    custom_rewrite_rules();
    flush_rewrite_rules();
}
register_activation_hook( __FILE__ , 'flush_rewrite_on_activation' );

Once we have done the above steps we need to replace url-1 and url-2 with the actual slugs of your posts. After adding this code, we may need to visit the WordPress Settings > Permalinks page to flush the rewrite rules manually. After this we need to ensure that your caching plugin is configured to allow for this type of redirection.

本文标签: permalinksDisplay the content of another page from a specific URL