1

前回の質問は複雑すぎて、正直なところ、答えようとしている人々を気にせず、私を混乱させていました。

私は現在、1 つのカテゴリの投稿が割り当てられたページを作成する必要がありますが、両方の投稿ページは同じ content.php と content-single.php を使用していますが、見た目の理由からこれらのページの異なる反復を使用するために両方のページを使用していました。

例として、http://dev.n8geeks.com/blog/にアクセスし、最初のブログ投稿をクリックします。サムネイルが表示されます。これはクールで、私が欲しいものです。ただし、ここに表示されているように、「ビデオ」ページに表示されます。http://dev.n8geeks.com/videos/ (投稿をクリックすると) サムネイル ボックスも表示されます (ただし、この投稿ページ カテゴリにはサムネイルは添付されません)。

これが、content.php と content-single.php の異なる反復を使用する必要がある理由ですが、方法がわかりません。また、「ビデオ」ページが「ブログ」ページと同じフォーマットになっていれば素晴らしいのですが、これを実現する方法がわかりません。

現在の「ビデオ」ページに使用しているコードは次のとおりです。

<?php get_header(); ?>

<div id="content">
<div id="main">

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; else: endif; ?>

<?php query_posts('category_name='.get_the_title().'&post_status=publish,future');?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1 class="entry-title"><a href="<?php the_permalink(); ?>">
<?php the_title();  ?></a></h1>
<p><?php the_content(); ?>
<?php endwhile; else: endif; ?>

</div>
</div>

<?php get_footer(); ?>

事前に感謝します。あなたが信じられないほどの助けに本当に感謝しています。今は午前 4 時 33 分で、この問題を解決しようとして頭がおかしくなりそうです。

よろしく

4

1 に答える 1

1

まだ非常に紛らわしいですが、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 です。

お役に立てば幸いです、
マックス

于 2012-06-23T03:42:48.150 に答える