【问题标题】:Redirect to latest post重定向到最新帖子
【发布时间】:2011-12-31 06:07:08
【问题描述】:

我想将我的 wordpress 博客的访问者重定向到 latest single post。 我找不到插件,也不知道怎么做!

例如www.example.com --> www.example.com/?p=123

【问题讨论】:

    标签: php wordpress plugins redirect


    【解决方案1】:

    快速google search 将帮助您。 将此代码放在index.php 或主页顶部,get_header() 函数上方。

    <?php
    /*
    index.php (Blog Home Page):
    Redirect To First Post
    */
    
    if (have_posts()) {
        while (have_posts()) {
            the_post();
            wp_redirect(get_permalink());
        }
    }
    ?>
    

    【讨论】:

    • 我在这里找到了一些东西:wordpress.org/support/topic/…
    • 但现在我想重定向到自定义帖子类型!例如www.example.com/?product=432
    【解决方案2】:

    您是否尝试过从 WP 内容表中选择 MAXID,然后在您的重定向中使用该 ID,这将是最新的帖子。

    或者

    我相信 wordpress 中有一个设置可以使主页成为最新帖子。

    【讨论】:

    • 逻辑上是正确的,但由于 WP 提供了内置功能,所以有点矫枉过正。
    【解决方案3】:

    add_action('wp_head','hook_css');

    function hook_css() {
    if( is_front_page() ) {
    $latest = get_posts( "post_type=post&numberposts=1" );
    $permalink = get_permalink( $latest[0]->ID );
    $output="<meta http-equiv='refresh' content='4;url=" . $permalink . "'/>";
    
    echo $output;
    
    }}
    

    【讨论】:

      猜你喜欢
      • 2018-03-22
      • 2017-10-05
      • 2021-01-06
      • 1970-01-01
      • 2012-01-25
      • 1970-01-01
      • 1970-01-01
      • 2020-08-10
      • 1970-01-01
      相关资源
      最近更新 更多