dongyong2063 2016-03-23 15:38
浏览 21
已采纳

使用“假页面”创建子页面的子页面

I found this tutorial to make so called fake pages, so I could dynamically create pages with custom fields. It works very well, but I need to tweak it to fit my needs.

Like if for example a book have reviews from different sources:

mysite.com/books/to-kill-mockingbird/reviews/id-for-review

This mean that to-kill-mockingbird is the slug for post type book and id-for-review is the slug for post type reviews. I can easily get this to work by removing books/to-kill-mockingbird, and just have mysite.com/reviews/id-for-review, but I prefer to have the full url.

Any suggestions?

I'm trying to create a new rewrite rule that puts the id-for-review into a query variable. I have added $review-id for that purpose.

Here's the rule I tried to make

if ($slug == 'reviews') {
           $newrules['books/([^/]+)/' . $slug . '/([^/]+)/?'] = 'index.php?book=$matches[1]&fpage=' . $slug . '&review-id=$matches[2]';
           $newrules['books/([^/]+)/' . $slug . '/?$'] = 'index.php?book=$matches[1]&fpage=' . $slug;
        }
        else
        {
           $newrules['books/([^/]+)/' . $slug . '/?$'] = 'index.php?book=$matches[1]&fpage=' . $slug;
        }

I have a template named single-book.php where the correct template is being used:

$current_fp = get_query_var('fpage');
$review-id = get_query_var('review-id');

if (!$current_fp) {
        get_template_part( 'single', 'book-index' );
    } else if ($current_fp == 'books') {
    if (!$review-id) {
        get_template_part( 'single', 'book-reviews' );
        }
        else
        {
        get_template_part( 'single', 'book-review' );
        }
    };

If I now go to /books/to-kill-mockingbird/reviews/, it uses the template for listing all reviews. But when I go to a review, it seems to use the standard WordPress template.

Here's my .htaccess:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress
  • 写回答

1条回答 默认 最新

  • dongwu9063 2016-03-24 14:47
    关注

    Seems I came closer to the answer than I thought, after some tweaking and editing of my question. Most of my code is now correct.

    Here's the rules that needs to put replaced in the example from the tutorial. This is put inside the foreach loop, and creates two rules for 'reviews'.

    if ($slug == 'reviews') {
               $newrules['books/([^/]+)/' . $slug . '/([^/]+)/?'] = 'index.php?book=$matches[1]&fpage=' . $slug . '&review-id=$matches[2]';
               $newrules['books/([^/]+)/' . $slug . '/?$'] = 'index.php?book=$matches[1]&fpage=' . $slug;
            }
            else
            {
               $newrules['books/([^/]+)/' . $slug . '/?$'] = 'index.php?book=$matches[1]&fpage=' . $slug;
            }
    

    Now inside the single-books.php template, use this to get correct template:

    $current_fp = get_query_var('fpage');
    $review-id = get_query_var('review-id');
    
    if (!$current_fp) {
            get_template_part( 'single', 'book-index' );
        } else if ($current_fp == 'books') {
        if (!$review-id) {
            get_template_part( 'single', 'book-reviews' );
            }
            else
            {
            get_template_part( 'single', 'book-review' );
            }
        };
    

    Now inside single-book-review.php template, you use this to get the post data based on the slug from the query var:

    $review_id = get_query_var('review-id');
    
    $post = get_page_by_path( $review_id, OBJECT, 'review' )
    

    Also remember to go to Settings -> Permalinks and click save when you change the rule, to flush the rules.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。