2011-11-06 72 views
0

我创建了自己的wordpress主题,它有点不同,因为它不会有单个页面(或者至少不会有单个页面可到达)。整个网站只包含主页(包含循环)和以前的帖子页面。如何在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(以前的职位页面由infite滚动插件替换)。

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

我看到它的方式有两种可能的解决方案:

  1. 更改permalinkstructure向后显示分页(所以x.com/paged=231成为第一个“之前的”页面然而,这不是用户友好的。 。
  2. 只用ID生成链接,让WordPress的手柄自定义重定向页面在那个时间当前点。

有没有更好的方法?我敢肯定,这是已经解决的地方,我真不敢找不到它

回答

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; ?>