设置WordPress程序加载的页面

huangapple go评论73阅读模式
英文:

Set page that wordpress loads programmatically

问题

我目前正在尝试设置一个 PHP 函数,用于设置 WordPress 加载的页面。我在我的(子)主题的 functions.php 中使用一个挂钩来确定何时显示特定页面。
我正在查看类似于 update_option('page_for_posts', 'page123') 的东西,但不是特别针对首页。

背景:我需要创建一种重定向,不能通过 .htaccess 文件解决,因为我需要基于数据库条目进行重定向。重定向不能被页面的用户注意到。

我尝试过使用 update_option('show_on_front', 'page');update_option('page_for_posts', 'Beispiel-Seite'); 进行重定向,但没有得到期望的结果。

function setupRedirect() {
    // 确定要重定向到哪个页面/站点

    // 重定向站点
    update_option('page_on_front', $pageRedirectNameOrID);
    update_option('show_on_front', 'page');
}

编辑:
我找到了一些关于 add_rewrite_rule() 函数的帖子,但我无法让它工作。
这是我目前的代码:

add_rewrite_rule(
    'url-getting-called',
    'post-url-to-redirect-to',///?slug='.$eiaSite->slug,
    'top'
);

我刷新/重新保存了永久链接,但它不起作用...

希望这些信息对你有所帮助。

英文:

I am currently trying to setup a php-function, that sets the page that wordpress loads. I use a hook in my functions.php of my (child-)theme to determine when i want to display the special page.
I am looking at something like update_option('page_for_posts', 'page123') but not specifically for the front page.

Background: I need to make a redirect kind of thing, that i cant solve with an htaccess, because i need to redirect based on database-entries. The redirect has to not be notices by the users of the page.

I tried redirects via update_option( 'show_on_front', 'page' ); and update_option( 'page_for_posts', 'Beispiel-Seite' ); but i didnt get the desired results

function setupRedirect() {
    // Determine which page/site to redirect to

    // redirect the site
	update_option( 'page_on_front', $pageRedirectNameOrID );
	update_option( 'show_on_front', 'page' );
}

Edit:
I found a few posts about the add_rewrite_rule() function but i cant get it to work.
Thats what i got so far.

add_rewrite_rule(
	'url-getting-called',
	'post-url-to-redirect-to',///?slug='.$eiaSite->slug,
	'top'
);

I refreshed/re-saved the perma-links but its not working...

答案1

得分: 1

你可以使用 the template_include filter 来更改加载的模板,这可以明确地从另一页获取数据;将以下内容添加到你的主题的 functions.php 文件或 mu-plugin(未经测试)中:

add_filter( 'template_include', static function ( $template ) {
    $switch_to_special = false; // 更改模板的逻辑

    if ( ! $switch_to_special ) {
        return $template;
    }

    return get_theme_file_path( 'template-special.php' );
} );
英文:

You could use the template_include filter to change the template that's loaded, which could explicitly pull data from that other page; add the following to your theme's functions.php or an mu-plugin (untested):

add_filter( 'template_include', static function ( $template ) {
    $switch_to_special = false; // logic to change template

    if ( ! $switch_to_special ) {
        return $template;
    }

    return get_theme_file_path( 'template-special.php' );
} );

huangapple
  • 本文由 发表于 2023年7月13日 00:20:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76672623.html
  • custom-wordpress-pages
  • php
  • redirect
  • wordpress

如何将MySQL表列作为变量放入 :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定