php - 将类添加到 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> ';
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 & 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();'的输出 并在前面粘贴“类别-”的同时删除空格和大写?
我找到了这个线程:
虽然这意味着“category_ac()”需要是一个要更改的变量。
我似乎无法应用自己的变量,例如:
$test = category_ac();
...因为那没有做任何事情,所以我想我做错了。
解决方案
如果你离开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> ';
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();
}
推荐阅读
- r - 带有排名的ggplot2条形图
- php - 从表单中获取 ip 地址
- javascript - 如何停止 JavaScript 在不包含查询元素的页面上运行?
- string - 第一个重复字符 | 运行
- sparql - 查找 DBPedia 资源的所有 Wikilinks,其中 Wikilinks 共享相同的主题
- datetime - 如何破译这个 8 位数的时间戳?
- django - 在 django restframework 中覆盖查询集时如何从数据库中过滤产品
- android - 在主活动中侦听首选项按钮单击
- java - Java中的手动依赖注入(没有spring boot)
- sql - 用相同数据集中的相似行值替换缺失值(无连接)