2015-11-05 66 views
0

我有兩個單獨的頁面,顯示了鏈接到wordpress中完整帖子的不同類別列表。這兩個目前在我的主題single.php打開,但類別需要在每個頁面上不同的風格。在不同的頁面中打開wordpress帖子

我已經創建的模板頁,但不知道如何打開手機的single.php

以外的其他頁面上的帖子,以簡化:我怎樣打開帖子上的single.php

的另一個版本代碼我有打開完整的職位是:

<?php // PAGE LINK/TITLE   
if (is_page()) { 
    $cat=get_cat_ID($post->post_title); //use page title to get a category ID 
    $posts = get_posts ("category_name=case-study&posts_per_page=10"); 
    if ($posts) { 
    foreach ($posts as $post): 
     setup_postdata($post); 

?> 
<div class="serve-inner-split"> 
       <div id="case-split"> 
         <div id="case-left" class=" serve-left"> 
          <div id="case-study-content"> 
           <h1 class="case-study-title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1> 
           <p><?php //PULLS IN EXCERPT 
            $my_excerpt = get_the_excerpt(); 
            if ('' != $my_excerpt) { 
             // Some string manipulation performed 
            } 
            echo $my_excerpt; // Outputs the processed value to the page 

            ?> 
           </p> 
           <a href="#" class="header-quote">READ CASE STUDY</a> 
          </div> 
         </div> 
         <div id="case-right" class="serve-grey"> 
    <?php 
if (has_post_thumbnail()) { // PULLS IN IMAGE check if the post has a Post Thumbnail assigned to it. 
    the_post_thumbnail(); 
} 
?> 
         </div> 
        </div> 

</div> 



<?php endforeach; 
    } 
} 
?> 

回答

1

你好:)我會被你需要不同的方式顯示單後每種情況下創建自定義後類型實現這一目標。在這種情況下你可以參考WP Codex https://codex.wordpress.org/Post_Types。最終,它將允許您創建儘可能多的單個發佈模板。

+0

或者他可以以不同的方式添加類別字段和樣式index.php;) –

+0

自定義帖子類型是我最喜歡的「開箱即用」解決方案,對於WP管理員面板不太熟悉的用戶也很方便使用:) – markoffden

0

我會創建額外的類別字段。在functions.php添加

add_action ('edit_category_form_fields', 'mytheme_extra_category_fields'); 
add_action ('category_add_form_fields', 'mytheme_extra_add_category_fields'); 

if (! function_exists('mytheme_extra_category_fields')){ 
    function mytheme_extra_category_fields($tag) { 
     $t_id = $tag->term_id; 
     $cat_meta = get_option("category_$t_id"); 
     ?> 
     <tr class="form-field"> 
      <th scope="row" valign="top"><label for="extra1"><?php esc_attr_e('Blog Layout', 'mytheme'); ?></label></th> 
      <td> 
       <select name="Cat_meta[blog_layout]"> 
        <?php 
        echo '<option value="layout1" '.selected($cat_meta['blog_layout'], 'layout1', false).'>'.esc_html__('Layout 1', 'mytheme').'</option> '; 
        echo '<option value="layout2" '.selected($cat_meta['blog_layout'], 'layout2', false).'>'.esc_html__('Layout 2', 'mytheme').'</option> '; 
        ?> 
       </select> 
      </td> 
     </tr> 

    <?php 
    } 
} 

if (! function_exists('mytheme_extra_add_category_fields')){ 
    function mytheme_extra_add_category_fields($tag) { 
     $t_id = (is_object($tag))?$tag->term_id:''; 
     $cat_meta = get_option("category_$t_id"); 
     ?> 

     <div class="form-field"> 
      <label for="extra1"><?php esc_attr_e('Blog Layout', 'mytheme'); ?></label></th> 
      <select name="Cat_meta[blog_layout]"> 
       <?php 
       echo '<option value="layout1" '.selected($cat_meta['blog_layout'], 'layout1', false).'>'.esc_html__('Layout 1', 'mytheme').'</option> '; 
       echo '<option value="layout2" '.selected($cat_meta['blog_layout'], 'layout2', false).'>'.esc_html__('Layout 2', 'mytheme').'</option> '; 
       ?> 
      </select> 
     </div> 

     <?php 
    } 
} 
add_action ('edited_category', 'mytheme_save_extra_category_fileds'); 
add_action ('created_category', 'mytheme_save_extra_category_fileds'); 

if (! function_exists('mytheme_save_extra_category_fileds')){ 
    function mytheme_save_extra_category_fileds($term_id) { 
     if (isset($_POST['Cat_meta'])) { 
      $t_id = $term_id; 
      $cat_meta = get_option("category_$t_id"); 
      $cat_keys = array_keys($_POST['Cat_meta']); 
      foreach ($cat_keys as $key){ 
       if(isset($_POST['Cat_meta'][$key])){ 
        $cat_meta[$key] = $_POST['Cat_meta'][$key]; 
       } 
      } 
      update_option("category_$t_id", $cat_meta); 
     } 
    } 
} 

這應該輸出的選擇「博客佈局」當你添加的類別到您的文章。然後在您的index.php添加

$cat_id = get_query_var('cat'); 
$cat_data = get_option("category_$cat_id"); 

<?php if(isset($cat_data['blog_layout']) && $cat_data['blog_layout'] == 'layout1'): ?> 

\\Your layout1 here 

<?php else: ?> 

\\Your layout2 here 

<?php endif; ?> 

這應該有效。

0

要解決這個問題我替換整個的single.php文件與此代碼:

<?php 
if (in_category('2')) {include (TEMPLATEPATH . '/page-case-study-post.php'); 
} 
else { include (TEMPLATEPATH . '/page-services-post.php'); 
} 
?>; 

發送或者類別,這取決於其設置爲兩個不同的網頁。

相關問題