2015-12-21 82 views
0

您好,我是WordPress自定义模板中的新成员。任何人都可以告诉我如何为一个类别加载不同的模板。类别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 
} 

回答

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中,并为每个类别创建不同的模板

+0

我试过这样做,但得到了php错误。不知道问题在哪里。你能否在我的index.php或content.php文件中添加上面的代码 –

0

检查此页Template Hierachy。基本上,如果你想定位一个特定的类别,你可以创建一个文件,例如category-{slug}.php {slug}是你想要定位的类别。

0

我建议你在https://developer.wordpress.org/themes/basics/template-hierarchy/

了解WordPress模板层次要回答你的问题,你需要创建一个类别 - {ID} .PHP或类别 - {}塞PHP文件。在你的主题文件夹(只选择一个)

  • ID =您的类别编号
  • 塞=您的类别塞

创建文件后,那么你可以,如果你想要把你的自定义代码。