最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

database - Pages from custom table

programmeradmin0浏览0评论

I have custom table "catalog" inside wordpress database:

id|name|description|slug|price

I have template file "catalog" and page "Catalog" where i show all items from catalog table. But how i can show single item from this list as a page?

domain.dev/catalog/ (this part works) domain.dev/catalog/my-single-item-page (this i can't figure out how to make to work)

how i can use "my-single-item-page " as dynamic variable?

slug = "my-single-item-page";

i have this snippet inside my functions.php file

    function add_query_vars_catalog_item_filter( $vars ) {
        $vars[] = "catalog_item";
        return $vars;
    }
    add_filter( 'query_vars', 'add_query_vars_catalog_item_filter' );

    function catalog_rewrite() {
        add_rewrite_rule('^catalog/([\w-]+)/?', 'index.php?pagename=catalog&catalog_item=$matches[1]', 'top');
    }
    add_action('init', 'catalog_rewrite');

but when i go to domain.dev/catalog/some-my-item i got 404 page

I use this custom table because i generate form CSV. And in future it will be simpler for regenerate data

I have custom table "catalog" inside wordpress database:

id|name|description|slug|price

I have template file "catalog" and page "Catalog" where i show all items from catalog table. But how i can show single item from this list as a page?

domain.dev/catalog/ (this part works) domain.dev/catalog/my-single-item-page (this i can't figure out how to make to work)

how i can use "my-single-item-page " as dynamic variable?

slug = "my-single-item-page";

i have this snippet inside my functions.php file

    function add_query_vars_catalog_item_filter( $vars ) {
        $vars[] = "catalog_item";
        return $vars;
    }
    add_filter( 'query_vars', 'add_query_vars_catalog_item_filter' );

    function catalog_rewrite() {
        add_rewrite_rule('^catalog/([\w-]+)/?', 'index.php?pagename=catalog&catalog_item=$matches[1]', 'top');
    }
    add_action('init', 'catalog_rewrite');

but when i go to domain.dev/catalog/some-my-item i got 404 page

I use this custom table because i generate form CSV. And in future it will be simpler for regenerate data

Share Improve this question edited Aug 15, 2019 at 15:50 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Aug 15, 2019 at 15:07 napaliasnapalias 1032 bronze badges 2
  • 1 Have you flushed the rewrite rules? (just visit the permalink settings page) – Sally CJ Commented Aug 16, 2019 at 9:10
  • 1 @SallyCJ Are you serious? Such a quick thing. How i do not think about that :D. Thank you!!! You can write answer and i will accept :) – napalias Commented Aug 16, 2019 at 18:35
Add a comment  | 

1 Answer 1

Reset to default 1

Your code worked absolutely fine for me, so I suspected you forgot to flush the rewrite rules.

Each time after you added code which alters the WordPress rewrite rules, e.g. add_rewrite_rule() or code which registers/un-registers a CPT or custom taxonomy with URL rewriting enabled, you should always flush the rewrite rules which means WordPress will empty/reset the saved rewrite rules (in the database and .htaccess file) and regenerate (and re-save) them which would include your newly added or updated rewrite rules.

How to flush the rewrite rules: Just visit the Settings → Permalink Settings page. And there's no need to click on the "Save Changes" button.

There's also a programmatic way (see flush_rewrite_rules()), but in general, the above trick is the preferred way unless you are writing a plugin where you would for example, call the function during the plugin activation and deactivation.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论