2017-04-11 52 views
0

我對於Wordpress和CSS有點新,我希望你們能幫助我。 我想更改特定類別下所有帖子的sitelogo .site-branding的位置。在Wordpress中更改類別中的帖子的CSS

我可以將我的style.css中的site-branding更改爲我的喜好,但它會影響整個網站,包括所有網頁,帖子和類別。

.site-branding { 
    left: 50%; 
    position: absolute; 
    top: 40%; 
    -webkit-transform: translate(-50%, -50%); 
    -ms-transform: translate(-50%, -50%); 
    transform: translate(-50%, -50%); 

希望你們能幫助我。

在此先感謝。

+0

請在發佈從源鏈接或東西(HTML /代碼)小提琴。 – dutchsociety

+0

您應該能夠通過正文中的課程來定位特定的類別頁面。假設你在主題中使用body_class()。 –

回答

0

您可能需要Javascript和Jquery才能完成此操作。

因此,如果這是你的URL結構:www.mysite.com/home/category/,

(function($){ 
var $pathArray = window.location.pathname.split('/'); 
var $location = pathArray[2] 
var $branding = $(".site-branding") 
if ($location == 'cat1' || $location == 'cat2') { 
    $branding.css({ 
     'left' : '50%', 
     'position' : 'absolute', 
     'top' : '40%', 
     '-webkit-transform' : 'translate(-50%, -50%)', 
     '-ms-transform' : 'translate(-50%, -50%)', 
     'transform' : 'translate(-50%, -50%)' 
    }); 
} 
}); 

假設你在你的WordPress的實例上運行的子主題(其中,如果你不是,你肯定應該)你可以在header.php文件的底部添加這個腳本。

0

爲此,我認爲您必須在header.php中添加一個PHP條件,檢查當前帖子是否在該特定類別中,然後將另一個類添加到您的徽標標籤中以更改其位置。

會是這樣的:

<h1 class="site-branding <?php in_array(YOUR_CATEGORY_ID, get_the_category((get_the_ID())) ? ' new-posision' : '' ; ?>"></h1> 
0

的WordPress可以添加類別爲一類以身體標記。 如果您的主題如此,您可以爲類別添加特定的css規則。

.category .site-branding { 
    /* your css rules here */ 
} 
1

WordPress的給你加它的名字你的樣式表(通常是「類-X」)的目標類別的能力,那麼你就可以唯一的目標在你的CSS任何和所有類別。

Here's a great step by step article for targeting this class in your css

一個例子可能是:

<body class="archive category category-agriculture category-40"> 

隨着類別的農業是你想要的目標,像這樣的類:

body.category-agriculture .site-branding {color: red;} 
1

謝謝你的努力,但我有一點點幫助,解決了這個問題。

我加入這個過濾器,以我的functions.php

function pn_body_class_add_categories($classes) { 

// Only proceed if we're on a single post page 
if (!is_single()) 
    return $classes; 

// Get the categories that are assigned to this post 
$post_categories = get_the_category(); 

// Loop over each category in the $categories array 
foreach($post_categories as $current_category) { 

    // Add the current category's slug to the $body_classes array 
    $classes[] = 'category-' . $current_category->slug; 

} 

// Finally, return the $body_classes array 
return $classes; 
} 
add_filter('body_class', 'pn_body_class_add_categories'); 

該過濾器將添加類別名稱爲一類到body單帖。

例如,如果該類別被稱爲新聞,它會將category-news作爲單個帖子上的課程添加到主體。

一旦這樣做了,你可以使用體類只針對單帖.site-branding在新聞類別是這樣的:

.single.category-news .site-branding { 
left: 20%; 
}