還十分混亂,哈哈,但是從它的聲音,你需要不同的模板基於哪個類別的職位是當你瀏覽一個帖子中顯示出來?
如果是的話,你可以嘗試設置成你的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)
,並創建文件的單cat1-2.php「和「單default.php',只需添加if語句來檢查帖子是否在某個類別(或類別)中並加載正確的模板。您也可以使用ID,名稱和它們的slug作爲in_category
函數的選擇器,read more here。
編輯: 凱,以及你需要學習插件編程來真正做到這一點。我已經開始爲你提供一個快速插件來幫助你。它的作品,只是不完美。您絕對可以使用不同的系統,例如在類別菜單中綁定類別,但我不喜歡使用該頁面的設置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。
希望幫助一些,
最大
嘿細辛,是的,非常混亂哉!所以基本上我想要不同版本的content.php和content-single.php根據類別顯示。上面的代碼是否適用於此?謝謝你的時間。 –
是的,這應該這樣做:3,隨意問你是否想要幫助實施它。 – Ben
我試過了,它似乎沒有工作,阿格。有什麼方法可以聯繫您解決此問題嗎?我準備爲修復付費。 –