2

WordPressプラグインディレクトリ内にカスタムページのテンプレートファイルを作成しましたが、正しいパスが見つかりません。このコードは機能しません:

update_post_meta( $pas_tasks_page_id, '_wp_page_template', dirname( __FILE__ ) . '/task-list-template.php' );

テンプレートファイルを手動でワードプレスのテーマに入れ、コードを次のように変更した場合にのみ機能します。

update_post_meta( $pas_tasks_page_id, '_wp_page_template', '/task-list-template.php' );

しかし、プラグイン開発者として、手動ではなくプラグインディレクトリ内に新しいテンプレートを作成したいと思います。どうすればいいですか?

4

1 に答える 1

5

最近、「template_include」フィルターを使用して、このようなことを行いました。私はこのようにしました:

function include_template_files() {
    $plugindir = dirname( __FILE__ );

    if (is_post_type_archive( 'post-type-name' )) {
        $templatefilename = 'archive-post_type_name.php';
        $template = $plugindir . '/theme_files/' . $templatefilename;
        return $template;
    }

    if ('post-type-name' == get_post_type() ){
        $templatefilename = 'single-post-type-name.php';
        $template = $plugindir . '/theme_files/' . $templatefilename;
        return $template;
    }
}
add_filter( 'template_include', 'include_template_files' );

ワードプレスの条件を使用して、どのテンプレートが要求されているかを確認し、プラグイン ディレクトリに「theme_files」フォルダーを作成し、適切な名前のワードプレス テンプレート ファイルをそこに配置しました。これは、カスタム投稿タイプ用の単一のアーカイブ テンプレートを作成することでした。

于 2013-01-22T16:16:01.567 に答える