私はこれを達成するには愚かすぎるようです。とてもシンプルに見えますが... テンプレートを作成し、カスタム ポートフォリオを表示しようとしました (カスタム ポスト プラグインによって登録されています)。これは、次のコードで正常に動作します。
<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>