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
- 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
1 Answer
Reset to default 0In 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.
- Add Rewrite Rules.
- 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
版权声明:本文标题:permalinks - Display the content of another page from a specific URL 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736290030a1928428.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论