Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

Wordpress uses parent template after moving child theme modified template into subfolder (able to select Child template but not working)

I have been browsing the web for 4 hrs. Those solutions won't solve my problem.

The modified template in child theme was working when placed in root directory. However, after child templates are moved to a subfolder, they can be selected when adding to a page. But once I refresh the website, a new page using the parent template is automatically created.

The code uses et_get_page_link( "profile" ) to retrieve the link.

The corresponding code is as below. I xdebugged it and found that I triggered

    // update page template option
    update_post_meta( $id, '_wp_page_template', 'page-' . $page_type . '.php' );

for some reason. And didn't trigger when child template was under root directory.

/**
 *
 * Get the page template link
 *
 * @param string $pages : login or register
 * @param array $params : array of query var
 *
 * @return $link
 * @author Dakachi
 * @version 1.0
 * @copyright enginethemes.com team
 */
if ( ! function_exists( 'et_get_page_link' ) ) {
    function et_get_page_link( $pages, $params = array(), $create = true ) {

        $page_args = array(
            'post_title'   => '',
            'post_content' => __( 'Please fill out the form below ', ET_DOMAIN ),
            'post_type'    => 'page',
            'post_status'  => 'publish'
        );

        if ( is_array( $pages ) ) {

            // page data is array (using this for insert page content purpose)
            $page_type = $pages['page_type'];
            $page_args = wp_parse_args( $pages, $page_args );
        } else {

            // pages is page_type string (using this only insert a page template)
            $page_type               = $pages;
            $page_args['post_title'] = $page_type;
        }
        /**
         * get page template link option and will return if it not empty
         */
        $link = get_option( $page_type, '' );
        if ( $link ) {
            $return = add_query_arg( $params, $link );

            return apply_filters( 'et_get_page_link', $return, $page_type, $params );
        }

        // find post template
        $pages = get_pages( array(
            'meta_key'    => '_wp_page_template',
            'meta_value'  => 'page-' . $page_type . '.php',
            'numberposts' => 1
        ) );

        // page not existed
        if ( empty( $pages ) || ! is_array( $pages ) ) {

            // return false if set create is false and doesnot generate page
            if ( ! $create ) {
                return false;
            }
            if ( ! ae_get_option( 'auto_create_page', false ) ) {
                // insert page
                $id = wp_insert_post( $page_args );

                if ( $id ) {

                    // update page template option
                    update_post_meta( $id, '_wp_page_template', 'page-' . $page_type . '.php' );
                }
            } else {
                $id = - 1;
            }
        } else {

            // page exists
            $page = array_shift( $pages );
            $id   = $page->ID;
        }
        if ( $id != - 1 ) {
            $return = get_permalink( $id );
        } else {
            $return = home_url();
        }
        /**
         * update transient page link
         */
        update_option( 'page-' . $page_type . '.php', $return );

        if ( ! empty( $params ) && is_array( $params ) ) {
            $return = add_query_arg( $params, $return );
        }

        return apply_filters( 'et_get_page_link', $return, $page_type, $params );
    }
}

I am only getting too many files and I don't want to put everything under root directory of child theme. So it is not a big issue bothering me too much. But I feel very annoying I couldn't put php files by type into subfolders. But it is wasting me too much time.

After creating this page manually with admin privilege

After creating this page manually with admin privilege

Reload the website, a new page using parent template is automatically created

screen shots

question from:https://stackoverflow.com/questions/65914982/wordpress-uses-parent-template-after-moving-child-theme-modified-template-into-s

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...