首页 > 解决方案 > 将类添加到 Wordpress 中的档案列表中的类别

问题描述

我对 PHP 不是很熟悉,但我正在学习。我正在使用 Wordpress,并且想创建一个可过滤的“新闻更新”页面,这是“最近 15 个帖子”中的帖子档案列表。

我有4个类别:

当我创建分配给此特定类别的帖子时,我希望为列表项提供该类别的类。

我希望我的页面的结果

使用以下 CSS:

.category-useful:before {
        float: right;
    position: inherit;
    color: white;
    font-family: "Font Awesome 5 Pro";
    font-weight: 900;
    content: "\f129";
    zoom: 3;
    padding: 5px 10px 5px 20px;
}

.category-urgent_important, .category-urgent_important h2 {
    color: white;
    background-color: red;
}

.category-urgent_important:before {
        float: right;
    position: inherit;
    color: white;
    font-family: "Font Awesome 5 Pro";
    font-weight: 900;
    content: "\f071";
    zoom: 3;
    padding: 5px 5px 5px 20px;
}

.category-for_you, .category-for_you h2 {
    color: white;
    background-color: green;
}

.category-for_you:before {
    float: right;
    position: inherit;
    color: white;
    font-family: "Font Awesome 5 Pro";
    font-weight: 900;
    content: "\f02d";
    zoom: 3;
    padding: 5px 5px 5px 20px;
}

.category-uncategorised {
display: none;
}

这是我当前的标记:

  function category_ac(){
    $categories = get_the_category();
    foreach ( $categories as $category ) {
        echo esc_html( $category->cat_name );
    }
  }

  $how_many_last_posts = intval(get_post_meta($post->ID, 'archived-posts-no', true));
  if($how_many_last_posts > 200 || $how_many_last_posts < 2) $how_many_last_posts = 15;

  $my_query = new WP_Query('post_type=post&nopaging=1');
  if($my_query->have_posts()) {
    echo '<h1 class="widget-title">Last '.$how_many_last_posts.' Posts <i class="fa fa-bullhorn" style="vertical-align: baseline;"></i></h1>&nbsp;';
    echo '<div class="archives-latest-section"><ol>';
    $counter = 1;
    while($my_query->have_posts() && $counter <= $how_many_last_posts) {
      $my_query->the_post();
      ?>
      <li class="<?php echo 'category-'.category_ac() ?>"><a href="<?php the_permalink() ?>" rel="category tag" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title();?></a></li>
      <?php
      $counter++;
    }
    echo '</ol></div>';
    wp_reset_postdata();
  }

问题是,它有效,但像这样:

<div class="archives-latest-section"><ol>      <li class="Uncategorized"><a href="https://www.website.com/2019/10/07/test-5/" rel="category tag" title="Permanent Link to Test 5">Test 5</a></li>
            <li class="New Benefitcategory-"><a href="https://www.website.com/2019/10/07/test-4/" rel="category tag" title="Permanent Link to Test 4">Test 4</a></li>
            <li class="Usefulcategory-"><a href="https://www.website.com/2019/10/07/test-3/" rel="category tag" title="Permanent Link to Test 3">Test 3</a></li>
            <li class="Urgent &amp; Importantcategory-"><a href="https://www.website.com/2019/10/07/test-2/" rel="category tag" title="Permanent Link to Test 2">Test 2</a></li>
            <li class="For Youcategory-"><a href="https://www.website.com/2019/10/07/test-1/" rel="category tag" title="Permanent Link to Test 1">Test 1</a></li>
      </ol></div>

如何操作'category_ac();'的输出 并在前面粘贴“类别-”的同时删除空格和大写?

我找到了这个线程:

剥离 php 变量,用破折号替换空格

虽然这意味着“category_ac()”需要是一个要更改的变量。

我似乎无法应用自己的变量,例如:

$test = category_ac();

...因为那没有做任何事情,所以我想我做错了。

标签: phphtmlwordpress

解决方案


如果你离开category-了 php 括号而只是 echo 该函数将起作用。然后您可以尝试使用类别 slug 而不是名称,这可能会解决您的名称问题

这是我的更改的代码$category->slug<li class="category-<?php echo category_ac() ?>">

让我知道它是否适合你

  function category_ac(){
    $categories = get_the_category();
    foreach ( $categories as $category ) {
        echo esc_html( $category->slug );
    }
  }

  $how_many_last_posts = intval(get_post_meta($post->ID, 'archived-posts-no', true));
  if($how_many_last_posts > 200 || $how_many_last_posts < 2) $how_many_last_posts = 15;

  $my_query = new WP_Query('post_type=post&nopaging=1');
  if($my_query->have_posts()) {
    echo '<h1 class="widget-title">Last '.$how_many_last_posts.' Posts <i class="fa fa-bullhorn" style="vertical-align: baseline;"></i></h1>&nbsp;';
    echo '<div class="archives-latest-section"><ol>';
    $counter = 1;
    while($my_query->have_posts() && $counter <= $how_many_last_posts) {
      $my_query->the_post();
      ?>
      <li class="category-<?php echo category_ac() ?>"><a href="<?php the_permalink() ?>" rel="category tag" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title();?></a></li>
      <?php
      $counter++;
    }
    echo '</ol></div>';
    wp_reset_postdata();
  }

推荐阅读