我有两页:
/contact
/contact-team
/contact
页面有一个弹出窗口,在用户选择时,页面必须重新加载,但包含/contact-team
.是否有一个过滤器挂钩,可以在URL生成后加载不同的帖子?
我已经试过了pre_get_posts
设置帖子ID,但它被重定向到该ID。我希望加载的页面/contact
但内容应该来自/contact-team
.有什么想法吗?
我有两页:
/contact
/contact-team
/contact
页面有一个弹出窗口,在用户选择时,页面必须重新加载,但包含/contact-team
.是否有一个过滤器挂钩,可以在URL生成后加载不同的帖子?
我已经试过了pre_get_posts
设置帖子ID,但它被重定向到该ID。我希望加载的页面/contact
但内容应该来自/contact-team
.有什么想法吗?
您可以使用request
钩子可更改相同URL的加载页面:
add_filter( \'request\', function( $request ){
//Replace this with custom logic to determine if the user should see the contact-team page
$load_team_contact = true;
if( $load_team_contact && isset( $request[\'pagename\'] ) && \'contact\' == $request[\'pagename\'] ){
$request[\'pagename\'] = \'contact-team\';
}
return $request;
} );
您只需确定用户是否应该查看联系人团队页面,该页面会根据您的设置而有所不同。你可以试试the_content
过滤以下内容以从其他帖子/页面加载内容
function get_contact_team_page_content ( $content ) {
// Condition based on user selection via popup e.g. $_GET or $_POST
if (\'condition\') {
$contact_team = get_post(14); // e.g. contact-team page ID
$content = $contact_team->post_content;
return $content;
}
return $content;
}
add_filter( \'the_content\', \'get_contact_team_page_content\');
使用cookie可以:
function get_contact_team_page_content ( $content ) {
if (isset($_COOKIE[\'alternative_content\'])) {
$contact_team = get_post( (int)$_COOKIE[\'alternative_content\'] ); // e.g. contact-team page ID
unset($_COOKIE[\'alternative_content\']);
return $contact_team->post_content;
}
return $content;
}
add_filter( \'the_content\', \'get_contact_team_page_content\');
通过这种方式(在主题或插件的functions.php中添加上述代码),您可以过滤the_content
基于cookie的存在在您的页面中,可以在单击按钮上附加一个功能,以重新加载页面:
<!-- 158 is your contact-team page ID -->
<button onClick="loadAlternateContent(158)">change</button>
<script>
function loadAlternateContent(post_id){
var date = new Date();
date.setTime( date.getTime() + (1*60*1000) ); //expiration in 1 minute
var expires = "; expires=" + date.toGMTString();
document.cookie = "alternative_content=" + post_id + expires +"; path=/";
window.location.reload();
}
</script>
您可以看到内容是post\\u id 158内容,但url是相同的。此时,手动重新加载页面时,第一次仍然会显示替代内容,因为cookie已被PHP删除,但它仍然存在于浏览器中。要避免这种行为,您应该具有删除“alternative\\u content”页面上的cookie的功能:
<script>
window.addEventListener("load", function(event) {
document.cookie = \'alternative_content=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;\';
});
</script>
我不确定我是否理解你的问题,但你可以通过以下方式在另一篇文章中获取文章数据:
$post = get_post( 123 ); // Where 123 is the ID
$output = apply_filters( \'the_content\', $post->post_content );
如果需要更多控制,也可以使用wp\\u query。我想在搜索表单中创建三个过滤器,其中包括三个不同的过滤器(标签、类别和帖子)。是否可以创建这些过滤器?如果可能的话,意味着我如何创建它?基本上,我不擅长PHP。所以,请帮我解决这个问题。谢谢