まだ非常に紛らわしいですが、1 つの投稿を表示しているときに、その投稿がどのカテゴリに属しているかに基づいて、さまざまなテンプレートが表示されるようにしたいですか?
もしそうなら、これをsingle.phpとして設定してみてください:
<?php get_header(); ?>
<?php
if ( have_posts() ) { the_post(); rewind_posts(); }
if ( in_category(1) || in_category(2) ) {
include(TEMPLATEPATH . '/single-cat1-2.php');
}
else {
include(TEMPLATEPATH . '/single-default.php');
}
?>
<?php get_footer(); ?>
( http://wordpress.org/support/topic/alternate-single-post-template-for-specific-categoriesから)
'single-cat1-2.php' と 'single-default.php' ファイルを作成し、if ステートメント チェックを追加して、投稿が特定のカテゴリ (またはカテゴリ) にあるかどうかを確認し、正しいテンプレートを読み込みます。ID、名前、およびそれらのスラッグをin_category
関数のセレクターとして使用することもできます。詳細については、こちらを参照してください。
編集: そうですね、実際にこれを行うには、プラグイン プログラミングを学ぶ必要があります。私はあなたを助けるための簡単なプラグインを始めました。それは機能しますが、完璧ではありません。カテゴリ メニューでカテゴリを関連付けるなど、別のシステムを使用することもできますが、そのページの設定 API をいじる気がしませんでした。
したがって、プラグイン ディレクトリに新しいディレクトリを作成し、それを と呼びPostCatTheme
、そこに という名前の新しいファイルを作成し、index.php
これを入れます。
<?php
/*
* Plugin Name: Post Category Templates
*/
//Replace __FILE__ with whatever the real path is because of symbolic link
/**
* Allows declarations of which categories a single-post template is assigned to
*/
class WordpressPostCatTheme
{
private $pluginDir, $templates;
function __construct ()
{
$this->pluginDir = dirname(__FILE__);
add_action("init", array($this, "load"));
add_filter('single_template', array($this, 'get_post_template'));
}
public function WPCT_deactivate ()
{
delete_option("PostCatTheme_templates");
}
public function load ()
{
register_deactivation_hook( __FILE__, array(&$this, 'WPCT_deactivate') );
$this->templates = get_option("PostCatTheme_templates");
if ($this->templates === FALSE)
{
$this->templates = $this->get_post_templates();
update_option("PostCatTheme_templates", $this->templates);
}
}
// This function scans the template files of the active theme,
// and returns an array of [category] => {file}.php]
public function get_post_templates()
{
$themes = get_themes();
$theme = get_current_theme();
$templates = $themes[$theme]['Template Files'];
$post_templates = array();
$base = array(trailingslashit(get_template_directory()), trailingslashit(get_stylesheet_directory()));
foreach ((array)$templates as $template)
{
$template = WP_CONTENT_DIR . str_replace(WP_CONTENT_DIR, '', $template);
$basename = str_replace($base, '', $template);
// don't allow template files in subdirectories
if (false !== strpos($basename, '/'))
continue;
$template_data = implode('', file( $template ));
$categories = '';
if (preg_match( '|Categories (.*)$|mi', $template_data, $categories))
$categories = _cleanup_header_comment($categories[1]);
//The categories are split by a | (pipe), if there aren't any pipes, assume it's just
//one category, otherwise split at the pipe
if (empty($categories))
continue;
if (strpos($categories, "|") === FALSE)
$categories = array($categories);
else
$categories = explode("|", $categories);
foreach ($categories as $category)
{
if (!empty($category))
{
if (isset($post_templates[$category]))
throw new Exception("Error, category assigned to more than one template");
if(basename($template) != basename(__FILE__))
$post_templates[trim($category)] = $basename;
}
}
}
//file_put_contents($this->pluginDir . "/log", json_encode($post_templates));
return $post_templates;
}
// Filter the single template value, and replace it with
// the template chosen by the user, if they chose one.
function get_post_template($template)
{
global $post;
$cats = wp_get_post_categories($post->ID);
//Go through each category, until one hits
foreach ($cats as $c)
{
$templateP = $this->templates[$c];
if(!empty($templateP) && file_exists(TEMPLATEPATH . "/{$templateP}"))
{
$template = TEMPLATEPATH . "/{$templateP}";
break;
}
}
return $template;
}
}
if (!isset($PostCatThemePlugin))
$PostCatThemePlugin = new WordpressPostCatTheme;
?>
その後、カスタムの single.php テンプレートCategories: 1|2
で、ヘッダー セクション (場所Template Name
) にコードを追加します。これらを変更または追加するときはいつでも、プラグインを非アクティブ化してから再度アクティブ化し、この情報が保存されているキャッシュを更新してください。
カテゴリの ID を取得するには、カテゴリを編集し、URL の tag_ID= の後の数字がカテゴリの ID です。
お役に立てば幸いです、
マックス