【问题标题】:How to create permalinks without single pages in wordpress?如何在 wordpress 中创建没有单页的永久链接?
【发布时间】:2011-11-06 14:59:25
【问题描述】:

我正在创建自己的 wordpress 主题,这有点不同,因为它没有单页(或至少,没有单页可访问)。整个网站只包含主页(带有循环)和以前的帖子页面。

我想链接到循环内的各个帖子,例如 site.com#post-124 或 site.com/paged=5#post-214。

我已经创建了一个这样做的函数:

function getPermalink($id,$postsPerPage) {
    $postNumber = Get_Post_Number($id); 
    //a function that get's the post number based on 
    //the chronical order of published posts.
    $page = floor(($postNumber - 1) / $postsPerPage);
    $url = get_option('home');

    if($page > 0) {
        $url .= '/?paged=' . ($page + (1 - floor($page / 5)));
    }

    $url .= '#post-' . $id;
    return $url;
}

你可以在这里看到它:http://mijnrealiteit.nl(以前的帖子页面被一个无限滚动插件取代)。

这可行,但是当我开始添加帖子时它会中断,因为之前的所有帖子都会转移回更远的页面(这会使链接无效)。

我认为有两种可能的解决方案:

  1. 更改永久链接结构以向后显示分页(因此 x.com/paged=231 成为第一个“上一页”页面。但这对用户不友好。
  2. 仅使用 ID 创建链接,并让 wordpress 处理自定义重定向到当前时间点的页面。

还有更好的选择吗?我确定这已经在某个地方解决了,只是找不到。

【问题讨论】:

    标签: php wordpress permalinks


    【解决方案1】:

    我被一个朋友推向了正确的方向,我使用选项 2 很容易地构建它:

    getPermalink 函数现在更简单了:

    function getPermalink($id) {
        return get_option('home') . '/?f=' . $id;
    }
    

    我没有进行任何自定义重定向,我只是在主页上检查了 GET 请求中传递了一个“f”:

    $perma = $_GET['f'];
    if(isset($perma) && !is_paged()) {
        $customposts = get_posts('p=' . $perma ); 
        foreach( $customposts as $post ) : 
            setup_postdata($post); ?>
                //load the post
        <?php endforeach;  
    }?> 
    

    如果是这样,则将使用 Wordpress 的 get_posts 函数获取帖子。我还检查了已经发送的帖子的(正常)循环:

    <?php while (have_posts()) : the_post(); 
        if(get_the_ID() != $perma) { ?>
            //load the post
    <?php } endwhile; ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多