【问题标题】:how to add a different class / style to wordpress posts?如何为 wordpress 帖子添加不同的类/样式?
【发布时间】:2013-06-05 10:01:55
【问题描述】:

我正在尝试为特定的帖子添加一个不同的类,这将通过不同的风格来调用。我已经玩过 post_class 函数了,但我仍然几乎不知道如何实现这一点,有什么想法吗?

<?php if (have_posts()) : ?>
<?php $c = 0;while (have_posts()) : the_post(); $c++;
if( $c == 3) {
    $style = 'third';
    $c = 0;
}
else $style='';
?>
<div <?php post_class($style) ?> id="post-<?php the_ID(); ?>">

我尝试添加这个,为它创建一个自定义样式,只是改变了背景和文本的颜色

我可以用这些来编辑帖子在主页上的外观吗,如果可以的话,我就可以快速解决这个问题,而不是找到那个晦涩的代码:D

【问题讨论】:

  • 发布一些代码或您尝试过的内容或您面临的问题
  • 您可以使用通常由 wordpress 添加到正文的常规后期类。每个帖子都有一个唯一的 ID 和一个像 postid-206 这样的类。您现在可以在选择器中使用此类来设置您想要的样式。

标签: php css wordpress styles


【解决方案1】:
<div <?php post_class($style) ?> id="post-<?php the_ID(); ?>">

使用 firebug 或查看源代码检查此代码创建的类名 你可能会看到这样的东西

<div class="divclassname" id="post-206">

现在,在加载页面的 css 样式表中,您需要根据 class (divclassname) 或 id (post-205) 的值组成新规则,如下所示。

.divclassname {
background-color: #f3f3f3;
color: #ffffff;
}

#post-205 {
background-color: #cccccc;
color: #a19402;
}

【讨论】:

    【解决方案2】:

    就像您正在使用的动态 ID。

    <div class="post-class-<?php echo get_the_ID(); ?>" id="post-<?php the_ID(); ?>">
    

    【讨论】:

      【解决方案3】:
      <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
      <h2 id="post-<?php the_ID(); ?>">
      

      【讨论】:

        【解决方案4】:

        在你的循环之外:

        $classes = array(
            0=>'first-column',
            1=>'second-column',
            2=>'third-column'
        );
        $i = 0;
        

        在你的循环中:

        <article id="post-<?php the_ID(); ?>" <?php post_class($classes[$i++%3]); ?>>
        

        输出如下:

        <article class="first-column format-standard hentry category-uncategorized">
        <article class="second-column format-standard hentry category-uncategorized">
        <article class="third-column format-standard hentry category-uncategorized">
        <article class="first-column format-standard hentry category-uncategorized">
        <article class="second-column format-standard hentry category-uncategorized">
        <article class="third-column format-standard hentry category-uncategorized">
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-07-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-12-26
          • 2012-05-31
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多