2015-10-08 180 views
1

我試圖從本教程中的代碼創建的WordPress「category.php」頁面:http://scratch99.com/wordpress/wp-seo/siloing/解析錯誤:語法錯誤,意外「endforeach」(T_ENDFOREACH)

我的當前主題(通過Themeforest Sociallyviral )沒有頁面「category.php」,所以我創建了一個並添加了下面的代碼:

作者注: 「代碼應該如下所示:請注意,您不能直接使用此代碼 - 它需要適合你的主題,以便包裝div等類型與主題的其餘部分相匹配,並且樣式被拾取。「

// work out category id, then see if there are any subcategories 
$main_cat_ID = get_query_var('cat'); 
$subcats = get_categories(array('parent' => $main_cat_ID, 'hide_empty' => 0)); 

// if it is page 1 
if ($paged < 2) : 

    // show the category description 
    echo category_description(); 

    // if there are subcategories, loop through and show them 
    if (count($categories) > 0) : 
     foreach ($subcats as $category) : ?>  
      <div class="subcategory"> 
       <h2><a class="subcategory-link" href="<?php echo get_category_link($category->term_id); ?>"> 
       <?php echo $category->name; ?></a></h2> 
       <?php echo wpautop($category->description); ?> 
      </div> 
     <?php 
     endforeach; 
    endif; 

    // get sticky posts and show them if they exist 
    $args = array('category__in' => $main_cat_ID, 'include' => get_option('sticky_posts')); 
    $myposts = get_posts($args); 
    if (count($myposts) > 0) : 
     foreach ($myposts as $value) : ?> 
     <div class="sticky"> 
      <h3> 
       <a href="<?php echo get_permalink($value->ID); ?>" rel="bookmark" 
       title="<?php echo $value->post_title; ?>"><?php echo $value->post_title; ?></a> 
      </h3> 
      <div class="entry-content"> 
      <?php 
      // if there is an excerpt, use it, otherwise roll our own (get_the_excerpt won't work outside the loop) 
      if ($value->post_excerpt) : 
       echo wpautop($value->post_excerpt); 
      else : 
       $sjc_excerpt = explode('<!--more-->', $value->post_content); 
       if (count($sjc_excerpt) >= 2) : 
        echo wpautop(strip_tags($sjc_excerpt[0])); 
       else : 
        echo wpautop(implode (' ', array_slice(explode(' ', strip_tags($sjc_excerpt[0])), 0, 45)) . ' ...'); 
       endif; 
      endif; ?> 
      </div> 
     </div> 
     <?php 
     endforeach; 
    endif; 

// pages 2 and onwards don't worry about cat desc or stickies, but link to main cat page 
else : ?> 
     <p>For an introduction to this topic and our latest articles, please see the main 
     <a href="<?php echo get_category_link($main_cat_ID); ?>"><?php single_cat_title(); ?></a> page.</p> 
<?php 
endif; 

// if we have any posts in the main loop (no stickies there) 
if (have_posts()) : ?> 

    <ul class="other-posts"> 

    <?php /* Start the Loop */ ?> 
    <?php while (have_posts()) : the_post(); ?> 

     <li><a href="<?php the_permalink() ?>" rel="bookmark" 
     title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></li> 

    <?php endwhile; ?> 
    </ul> 

    <?php 
    // you need to replace this with whatever your theme uses! 
    sjc_content_nav('nav-below'); 

// if no posts, and also no stickies and no categories, go with no results 
elseif (count($myposts) == 0 && count($subcats) == 0) : 

    get_template_part('no-results', 'archive'); 

endif; 

如果必要的話,我下面的主題文件 「archive.php」

<?php $mts_options = get_option(MTS_THEME_NAME); ?> 
<?php get_header(); ?> 
<div id="page"> 
    <div id="content_box"> 
     <h1 class="postsby"> 
      <?php if (is_category()) { ?> 
       <span><?php single_cat_title(); ?><?php _e(" Archive", "mythemeshop"); ?></span> 
      <?php } elseif (is_tag()) { ?> 
       <span><?php single_tag_title(); ?><?php _e(" Archive", "mythemeshop"); ?></span> 
      <?php } elseif (is_author()) { ?> 
       <span><?php $curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author)); echo $curauth->nickname; _e(" Archive", "mythemeshop"); ?></span> 
      <?php } elseif (is_day()) { ?> 
       <span><?php _e("Daily Archive:", "mythemeshop"); ?></span> <?php the_time('l, F j, Y'); ?> 
      <?php } elseif (is_month()) { ?> 
       <span><?php _e("Monthly Archive:", "mythemeshop"); ?>:</span> <?php the_time('F Y'); ?> 
      <?php } elseif (is_year()) { ?> 
       <span><?php _e("Yearly Archive:", "mythemeshop"); ?>:</span> <?php the_time('Y'); ?> 
      <?php } ?> 
     </h1> 
     <?php $j = 1; if (have_posts()) : while (have_posts()) : the_post(); $featured = get_post_meta($post->ID, 'mts_featured', true); ?> 
      <article class="latestPost excerpt<?php echo (($j+2) % 3 == 0) ? ' first' : ''; ?><?php echo ($j % 3 == 0) ? ' last' : ''; ?>" itemscope itemtype="http://schema.org/BlogPosting"> 
       <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" rel="nofollow" id="featured-thumbnail"> 
        <?php echo '<div class="featured-thumbnail">'; the_post_thumbnail('featured',array('title' => '')); echo '</div>'; ?> 
        <?php if (function_exists('wp_review_show_total')) wp_review_show_total(true, 'latestPost-review-wrapper'); ?> 
        <?php if ($featured) : ?><div class="post-label"><i class="fa fa-star"></i> <span><?php _e('Featured','mythemeshop'); ?></span></div><?php endif; ?> 
       </a> 
       <header> 
        <h2 class="title front-view-title"><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2> 
        <?php if (! empty($mts_options["mts_home_headline_meta"])) { ?> 
         <div class="post-info"> 
          <?php if(! empty($mts_options["mts_home_headline_meta_info"]['date'])) { ?> 
           <span class="thetime updated"><i class="fa fa-calendar"></i> <span itemprop="datePublished"><?php echo human_time_diff(get_the_time('U'), current_time('timestamp')) . __(' ago','mythemeshop'); ?></span></span> 
          <?php } ?> 
          <?php if(! empty($mts_options["mts_home_headline_meta_info"]['category'])) { ?> 
           <span class="thecategory"><i class="fa fa-tags"></i> <?php mts_the_category(', ') ?></span> 
          <?php } ?> 
         </div> 
        <?php } ?> 
       </header> 
      </article><!--.post excerpt--> 
     <?php $j++; endwhile; endif; ?> 

     <!--Start Pagination--> 
     <?php if (isset($mts_options['mts_pagenavigation_type']) && $mts_options['mts_pagenavigation_type'] == '1') { ?> 
      <?php mts_pagination(); ?> 
     <?php } else { ?> 
      <div class="pagination pagination-previous-next"> 
       <ul> 
        <li class="nav-previous"><?php next_posts_link('<i class="fa fa-angle-left"></i> '. __('Previous', 'mythemeshop')); ?></li> 
        <li class="nav-next"><?php previous_posts_link(__('Next', 'mythemeshop').' <i class="fa fa-angle-right"></i>'); ?></li> 
       </ul> 
      </div> 
     <?php } ?> 
     <!--End Pagination--> 
    </div> 
<?php get_footer(); ?> 

我,當我嘗試打開類別頁面得到這個錯誤:

「解析錯誤:語法錯誤,第20行/wp-content/themes/sociallyviral/category.php中的意外'endforeach'(T_ENDFOREACH)「

此語法錯誤,已解決!

謝謝大家!

此致敬禮!

+0

您是否在文件開頭有一個<?php,因爲它在這裏不存在? –

+0

不,我非常感謝你:/ – kodden

+0

很高興我能幫上忙。我爲更多用戶添加了一些更詳細的答案和一些解釋,如果您願意,您可以接受,所以其他人知道此問題已解決。 –

回答

0

您在文件開頭缺少<?php標記。

沒有它,代碼的第一位,解析器將解析下面的代碼PHP:

 echo get_category_link($category->term_id); 
     echo $category->name; 
     echo wpautop($category->description); 
    endforeach; 
endif; 

,你可以看到,首先要達到(三echo的旁邊)是endforeach;這是解析器拋出錯誤的地方。


作者指出下面說:」 ......它需要適合您的主題使得包裝的div等符合您的主題和的其餘風格被拾起。「

這意味着,您需要根據您和您的模板需要,在您的代碼中提供<div>classstyle addributes。詳細地說,您打算應用您的模板使用的類,以應用所需的佈局。你應該對模板使用的類有一些參考(可能是something like this)。你可以在哪裏看看使用哪些類。

This是bootstrap的參考看起來像 - 只是給你一些你想要的東西。

0

在你的代碼的開頭

<?php // show the category description 
echo category_description(); 

// if there are subcategories, loop through and show them 
if (count($categories) > 0) : 
    foreach ($subcats as $category) : ?>  
     <div class="subcategory"> 
      <h2><a class="subcategory-link" href="<?php echo get_category_link($category->term_id); ?>"> 
      <?php echo $category->name; ?></a></h2> 
      <?php echo wpautop($category->description); ?> 
     </div> 
    <?php 
    endforeach; 
endif; 

// get sticky posts and show them if they exist 
$args = array('category__in' => $main_cat_ID, 'include' => get_option('sticky_posts')); 
$myposts = get_posts($args); 
if (count($myposts) > 0) : 
    foreach ($myposts as $value) : ?> 
    <div class="sticky"> 
     <h3> 
      <a href="<?php echo get_permalink($value->ID); ?>" rel="bookmark" 
      title="<?php echo $value->post_title; ?>"><?php echo $value->post_title; ?></a> 
     </h3> 
     <div class="entry-content"> 
     <?php 
     // if there is an excerpt, use it, otherwise roll our own (get_the_excerpt won't work outside the loop) 
     if ($value->post_excerpt) : 
      echo wpautop($value->post_excerpt); 
     else : 
      $sjc_excerpt = explode('<!--more-->', $value->post_content); 
      if (count($sjc_excerpt) >= 2) : 
       echo wpautop(strip_tags($sjc_excerpt[0])); 
      else : 
       echo wpautop(implode (' ', array_slice(explode(' ', strip_tags($sjc_excerpt[0])), 0, 45)) . ' ...'); 
      endif; 
     endif; ?> 
     </div> 
    </div> 
    <?php 
    endforeach; 
endif; 

    // pages 2 and onwards don't worry about cat desc or stickies, but link  to main cat page 
    else : ?> 
    <p>For an introduction to this topic and our latest articles, please see the main 
    <a href="<?php echo get_category_link($main_cat_ID); ?>"><?php single_cat_title(); ?></a> page.</p> 
<ul class="other-posts"> 

<?php /* Start the Loop */ ?> 
<?php while (have_posts()) : the_post(); ?> 

    <li><a href="<?php the_permalink() ?>" rel="bookmark" 
    title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></li> 

<?php endwhile; ?> 
</ul> 

<?php 
// you need to replace this with whatever your theme uses! 
sjc_content_nav('nav-below'); 

// if no posts, and also no stickies and no categories, go with no results 
    elseif (count($myposts) == 0 && count($subcats) == 0) : 

    get_template_part('no-results', 'archive'); 

endif; 

您的問題將得到解決將一個PHP的標籤。

+0

非常感謝! – kodden

+0

@koddenif您的問題解決然後標記爲最佳答案。 –

相關問題