【发布时间】:2016-05-01 10:39:34
【问题描述】:
似乎我太愚蠢了,无法做到这一点。看起来很简单,然而…… 我创建了一个模板并尝试显示一个自定义投资组合(由自定义帖子插件注册)这适用于以下代码:
<div id="container">
<?php
//Define your custom post type name in the arguments
$args = array('post_type' => 'boxes_scientists');
//Define the loop based on arguments
$loop = new WP_Query( $args );
//Display the contents
while ( $loop->have_posts() ) : $loop->the_post();
?>
/*
<?php
foreach((get_the_category()) as $category) {
echo $category->cat_name . ' ';
}
?>
<?php the_category(', '); ?>
*/
<div class="some_base_class [categories of the post need to go here]">
<a class="element" href="<?php the_permalink(); ?>"></a>
<div class="portfolio-box">
<div class="portfolio-naming">
<h2 class="portfolio-title"><?php the_title(); ?></h2>
<h3 class="portfolio-attributes"><?php the_content(); ?></h3>
</div>
</div>
<?php the_post_thumbnail(); ?>
</div>
<?php endwhile;?>
但我无法处理注释掉的代码部分并在我的容器元素的类标记内显示类别(对于循环中显示的每个帖子)。
其实我也发现了这个概念:
https://lorelle.wordpress.com/2007/09/06/using-wordpress-categories-to-style-posts
这似乎正是我所需要的,但遗憾的是这对我来说根本不起作用。 (将函数放在我的子主题和主题的functions.php中,没有任何结果)
我在这里误解了什么?有人可以告诉我我必须使用的正确代码吗?会太棒了。提前致谢!
编辑
所以这终于让我走上了正确的道路:
<?php
[…]
$category = get_the_category();
$firstCategory = $category[0]->cat_name;?>
<div class="some_base_class <?php echo $firstCategory ?>">
我错过了 div 中的“回声”。现在我必须找到方法来显示我的帖子的所有个类别,而不是只显示第一个。
编辑 2:
<div id="boxes_section" class="main-content master-section-content nano_boxes no-detect no-padding">
<div class="container">
<div class="row">
<div class="col-md-12 normal-column start-animated-content az-fade-in" data-delay="300">
<div class="blank-divider" style="height: 30px;"></div>
<div id="portfolio-item-section" class="portfolio-output masonry-ly-portfolio classic-module no-pagination" data-cols="3">
<?php
//Define your custom post type name in the arguments
$args = array('post_type' => 'boxes_scientists');
//Define the loop based on arguments
$loop = new WP_Query( $args );
//Display the contents
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="single-portfolio-item az-col-full-width-4 [NEED THE CLASSES HERE]">
<a class="classic-portfolio-box normal-type-prt" href="<?php the_permalink(); ?>">
<p class="site_leave"><i class="font-icon-forward"></i>You are going to leave this website</p>
</a>
<div class="portfolio-box">
<div class="portfolio-naming">
<h2 class="portfolio-title"><?php the_title(); ?></h2>
<h3 class="portfolio-attributes"><?php the_content(); ?></h3>
</div>
</div>
<?php the_post_thumbnail(); ?>
</div>
<?php endwhile;?>
</div>
</div>
</div>
</div>
</div>
【问题讨论】:
-
您是否创建/注册了
boxes_scientists类别? -
是的!至少看起来是因为它显示在我的帖子中。
-
看看如何通过 CPT wordpress.stackexchange.com/questions/14331/…获取术语
-
首先看来我必须道歉,因为我现在看到这个问题似乎与stackoverflow.com/questions/17272969/…重复
-
第二,谢谢诺曼。也许您对丢失的注册是正确的,因为我尝试了另一个问题的答案中显示的代码(请参阅我的编辑),但它也不起作用。还是我放置 php 代码部分的方式存在语义错误?我对php语言很陌生。
标签: php wordpress post categories display