1

Seems that I am just too stupid to achieve this. Seems so simple, yet… I created a template and try to display a custom portfolio (registered by a custom post plugIn) This works fine with the following code:

<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;?>

but I can’t get to work the commented out code part and display the categories inside the class tag of my container element (for each post displayed in the loop).

I actually found also this concept:

https://lorelle.wordpress.com/2007/09/06/using-wordpress-categories-to-style-posts

which seemed to be exactly what I need but sadly this didn’t work at all for me. (placed the function inside functions.php on my child theme and theme both without any result)

What I am misunderstanding here? Can someone show me the correct code I have to use? Would be so awesome. Thanks in advance!

EDIT

So this finally brings me on the right way:

<?php
[…]
$category = get_the_category();
$firstCategory = $category[0]->cat_name;?>



<div class="some_base_class <?php echo $firstCategory ?>">

I missed the “echo” thing inside my div. Now I will have to find the way to display all categories of my post instead of only the first.

EDIT 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>
4

1 回答 1

1

我只是在functions.php中编写代码类别,如下所示:

function sps_category(){
    $categories = get_the_category();   
    foreach ( $categories as $category ) {
        echo '<a href="'.esc_url( get_category_link( $category->term_id ) ).'">
                    '.esc_html( $category->cat_name ).'
            </a>';
    }
}

我在我的页面上调用我的函数

<?php sps_category() ?>

但在其他方面,您可以像这样在类中编写代码:

$categories = get_the_category();   
foreach ( $categories as $category ) {
    echo '<div class=".esc_attr($category->cat_name)."><a href="'.esc_url( get_category_link( $category->term_id ) ).'">
                '.esc_html( $category->cat_name ).'
            </a><div>';
    }

如果您只想显示 1 个类别,它可以是可编辑和动态的。并且不要忘记使用转义函数,如果你在属性 html 中写入一些 php 变量/函数,例如使用esc_attr(somecode)class/title/name/id 属性。

于 2016-01-25T11:21:41.717 回答