duanlu1950 2016-01-25 07:28
浏览 28
已采纳

如何将帖子类别显示为类

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>
  • 写回答

1条回答 默认 最新

  • dshtze500055 2016-01-25 11:21
    关注

    I just write code category at functions.php like this:

    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>';
        }
    }
    

    and i call my functions at my page

    <?php sps_category() ?>
    

    but in other ways, you can write your code in class like this:

    $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>';
        }
    

    It can editable and dynamic if you want to show just 1 category. And dont forget to use escape function if you put some php variabel/functions if you write in attribut html for example use esc_attr(somecode) if it class/title/name/id attribute.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么