0

您好,我是 WordPress 自定义模板的新手。谁能告诉我如何为一个类别加载不同的模板。

这是 index.php 文件

<?php
/**
 * The main template file
 *
 * No comment for now
 *
 * @package WordPress
 * @subpackage site
 * @since site
 */

get_header(); ?>

<?php
    $strFr='Featured';
    $strFinancial='Financial Reports';
    $strResearch='Research';
?>

    <div id="page-body" role="main">
        <div class="container">
            <div class="row">

                <div class="col-md-12 col-sm-12 col-xs-12 SearchTop">
                    <form role="search" method="get" id="searchform" class="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">
                        <div class="input-group">
                            <div class="input-group-addon"><i class="fa fa-search"></i></div>
                            <input type="text" value="<?php echo get_search_query(); ?>" name="s" id="s" class="form-control customsearchfiled" placeholder="Search of anything related to Site" />
                        </div>
                    </form>
                </div><!-- SearchTop -->

                <div class="col-md-12 col-sm-12 col-xs-12">
                    <div class="col-md-12 col-sm-12 col-xs-12 mb20 no-gutter">

                        <div class="col-md-12 col-sm-12 col-xs-12 no-gutter">
                        <?php if (have_posts()) : ?>
                            <?php /* Start the Loop */ ?>
                            <?php while (have_posts()) : the_post(); ?>
                                <?php get_template_part('content', get_post_format()); ?>
                            <?php endwhile; ?>

                        <?php else : ?>

                            <article id="post-0" class="post no-results not-found">

                                <?php if (current_user_can('edit_posts')) :
                                    // Show a different message to a logged-in user who can add posts.
                                    ?>
                                    <header class="entry-header">
                                        <h1 class="entry-title mt20"><?php _e('No posts to display', 'twentytwelve'); ?></h1>
                                    </header>

                                    <!--<div class="entry-content">
                                        <p><?php //printf(__('Ready to publish your first post? <a href="%s">Get started here</a>.', 'twentytwelve'), admin_url('post-new.php')); ?></p>
                                    </div> .entry-content -->

                                <?php else :
                                    // Show the default message to everyone else.
                                    ?>
                                    <header class="entry-header">
                                        <h1 class="entry-title"><?php _e('Nothing Found', 'twentytwelve'); ?></h1>
                                    </header>

                                    <div class="entry-content">
                                        <p><?php _e('Apologies, but no results were found. Perhaps searching will help find a related post.', 'twentytwelve'); ?></p>
                                        <?php get_search_form(); ?>
                                    </div><!-- .entry-content -->
                                <?php endif; // end current_user_can() check ?>

                            </article><!-- #post-0 -->

                        <?php endif; // end have_posts() check ?>
                    </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
<?php get_footer(); ?>

据我所知,索引文件正在调用显示帖子布局但不知道如何执行此操作的内容 php 文件。尝试了很多东西,但只得到 php 错误。我不太了解php语法

<?php
/**
 * Custom template with better structure than WP default.
 *
 * Used for both single and index/archive/search.
 *
 * @package WordPress
 * @subpackage site
 * @since Site 1.0
 */

# for one post
if (is_single()) {
    ?>
    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        <header class="entry-header">
            <?php if(function_exists('pf_show_link')){echo pf_show_link();} ?>
            <p class="text-center topcategory">
                <?php foreach((get_the_category()) as $category) { 
                    echo $category->cat_name . ' '; 
                    }
                ?>
            </p>
            <h1 class="entry-title"><?php the_title(); ?></h1>
            <p class="postmetadown">By :<span class="bluecolor"><?php the_author(); ?></span> | <span><?php the_date() ?></span></p>
        </header>

        <div class="entry-content col-md-9 col-sm-9 col-xs-12">
            <?php the_content(__('Continue reading <span class="meta-nav">&rarr;</span>', 'twentytwelve')); ?>
            <?php wp_link_pages(array('before' => '<div class="page-links">' . __('Pages:', 'twentytwelve'), 'after' => '</div>')); ?>

        </div>
        <div class="col-md-3 col-sm-3 col-xs-12">
            <?php get_sidebar(); ?>
        </div>

        <?php if (comments_open()) : ?>
            <!--<div id="disqus_thread"></div>
            
            <script type="text/javascript">
                /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
                var disqus_shortname = 'site'; // required: replace example with your forum shortname

                /* * * DON'T EDIT BELOW THIS LINE * * */
                (function () {
                    var dsq = document.createElement('script');
                    dsq.type =
                        'text/javascript';
                    dsq.async = true;
                    dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
                    (document.getElementsByTagName('head')[0] ||
                        document.getElementsByTagName('body')
                            [0]).appendChild(dsq);
                })();
            </script>
            <noscript>Please enable JavaScript to view the <a href="http://disqus.com/? ref_noscript">comments powered
                    by Disqus.</a></noscript>
            <a href="http://disqus.com" class="dsq- brlink">comments powered by <span class="logo- disqus">Disqus</span></a>-->
        <?php endif; ?>
        <footer class="entry-meta col-md-12">
            <?php if ($googlePlusAcc = get_the_author_meta('googleplus')) { ?>
                <?php hr_single_entry_meta(); ?>
            <?php } else { ?>
                <?php twentytwelve_entry_meta(); ?>
            <?php } ?>
            <?php edit_post_link(__('Edit', 'twentytwelve'), '<span class="edit-link">', '</span>'); ?>
            <?php if (is_singular() && get_the_author_meta('description') && is_multi_author()) : // If a user has filled out their description and this is a multi-author blog, show a bio on their entries. ?>
                <div class="author-info">
                    <div class="author-avatar">
                        <?php
                        /** This filter is documented in author.php */
                        $author_bio_avatar_size = apply_filters('twentytwelve_author_bio_avatar_size', 68);
                        echo get_avatar(get_the_author_meta('user_email'), $author_bio_avatar_size);
                        ?>
                    </div>
                    <!-- .author-avatar -->
                    <div class="author-description">
                        <h2><?php printf(__('About %s', 'twentytwelve'), get_the_author()); ?></h2>

                        <p><?php the_author_meta('description'); ?></p>

                        <div class="author-link">
                            <a href="<?php echo esc_url(get_author_posts_url(get_the_author_meta('ID'))); ?>"
                               rel="author">
                                <?php printf(__('View all posts by %s <span class="meta-nav">&rarr;</span>', 'twentytwelve'), get_the_author()); ?>
                            </a>
                        </div>
                    </div>
                </div>
            <?php endif; ?>
        </footer>
    </article>
<?php
#for listing page
} else {

    $category = get_the_category();
    if (!empty($category))
        $category = $category[0];
    ?>
    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        <?php //if (is_sticky() && is_home() && !is_paged()) : ?>
            <!--<div class="featured-post">
                <?php //_e('Featured post', 'twentytwelve'); ?>
            </div>-->
        <?php //endif; ?>       

        <div class="col-md-4 col-sm-4 col-xs-12 categoryimg no-gutter">
            <?php if (has_post_thumbnail()) { ?>
                <div class="post-thumbnal-wrapper-multiple">
                    <?php the_post_thumbnail(); ?>
                </div>
            <?php } ?>
        </div>
        <div class="col-md-8 col-sm-8 col-xs-12 categoryContent">
            <header class="entry-header">
                <h1 class="entry-title">
                    <a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a>
                </h1>
            </header>

            <?php if (is_search()) : // Only display Excerpts for Search ?>
                <div class="entry-summary">
                    <?php the_excerpt(); ?>
                </div>
            <?php else : ?>
                <div class="entry-content">
                    <?php the_excerpt_more(); ?>
                </div>
            <?php endif; ?>
            <div class="col-md-12">
                <footer class="entry-meta">
                    <?php twentytwelve_entry_meta(); ?>
                    <?php edit_post_link(__('Edit', 'twentytwelve'), '<span class="edit-link">', '</span>'); ?>
                </footer>
            </div>
        </div><!-- categoryContent -->
    </article>
<div class="clearfix"></div>
<?php
}
4

4 回答 4

0

查看此页面模板层次结构。基本上,如果你想定位一个特定的类别,你可以创建一个像category-{slug}.php{slug} 这样的文件作为你想要定位的类别 slug。

于 2015-12-21T08:17:32.947 回答
0

如果您想要类别页面的模板,请参阅https://codex.wordpress.org/Category_Templates。如果您想要根据类别为内容使用不同的模板,您可以使用(在您的 content.php 中)

<?php if (is_category('Category A')) : ?>
<p>This is the text to describe category A</p>
<?php elseif (is_category('Category B')) : ?>
<p>This is the text to describe category B</p>
<?php else : ?>
<p>This is some generic text to describe all other category pages, 
I could be left blank</p>
<?php endif; ?>

如果类别之间有很多差异,我会将其放在 index.php 中,并为每个类别创建不同的模板

于 2015-12-21T08:16:49.977 回答
0

在 archive.php 中添加您的代码,它将列出该类别中的所有帖子,并在 single.php 中添加详细页面模板。

https://codex.wordpress.org/Category_Templates

于 2015-12-21T09:45:13.173 回答
0

我建议您在https://developer.wordpress.org/themes/basics/template-hierarchy/中阅读有关 Wordpress 模板层次结构的信息

要回答您的问题,您需要创建一个 category-{id}.php 或 category-{slug}.php 文件。在您的主题文件夹中(仅选择一个)

  • id = 您的类别 ID
  • slug = 你的类别 slug

创建文件后,您可以根据需要放置自定义代码。

于 2015-12-21T08:21:50.523 回答