0

I am trying to add wordpress pages using codes on theme activation. But the main problem is i cannot check whether the page with that particular slugs exist or not.

Our code looks like this :

add_action("after_switch_theme",function(){
    if(is_page('all-post-list')){
        return;
    }else{
        wp_insert_post(['post_title' =>'List Of All the Posts','post_status'=>'publish','post_name'=>'all-post-list','post_type'=>'page','page_template'=>'All Post List']);
        return;
    }
});

This hook is inserted in custom hook.

The main problem is with is_page

it is not working .

when i var_dump(is_page('all-post-list')) it always return false although there are pages.

But when i var dump that in header.php it works as expected.

Why is not that working in php . From Codex i read the note of using is_page and the code is outside loop then why is it nor working?

Is there any workaround??

Thanks.

1

is_page() checks if page is being displayed, it has nothing to do with checking if page exists at all in installation.

You likely want get_page_by_path() or get_page_by_title() instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.