douquanhui5735 2016-01-25 11:28
浏览 50

将类别标题添加到wordpress插件

I am using a Wordpress plugin to add a list of categories and 4 posts to the home page of a website.

What I am trying to do is add the title and a "More" link to each category but I can't quite get it right.

Below is the plugin code, with my new section highlighted in comments near the bottom. At the minute, it is only showing the title for the first 2 categories, then repeating the same titles and their links again, even though the category is different.

<?php
    global $post;

    // default attributes for shortcode
    $shortcode_default_attrs = array(
         'author' => '',
         'show' => 5,
         'excerpt' => 'false',
         'thumbnail' => 'false',
         'post_type' => 'post',
         'category' => '',
         'tag' => ''
    );

    // overrides default attributes set above and separates into individual varaibles.
    if(!empty($inserted_attrs)) {
        extract(shortcode_atts( $shortcode_default_attrs, $inserted_attrs ));
    }

    if(!empty($author)) {

        // checks if there are multiple authors set.
        $author_has_comma = strpos($author, ',');
        if($author_has_comma === false) {

            // gets the author data for a single author.
            $author_data = get_user_by( 'login', $author );

            if(!empty( $author_data)) {
                $post_args = array(
                    'author' => $author_data->ID,
                    'posts_per_page' => $show,
                    'post_type' => $post_type,
                    'category_name' => $category,
                    'tag' => $tag,
                );
            }

        } else {

            // gets the author data for multiple authors.
            $authors = explode(',', $author);
            $author_data = '';
            foreach($authors as $author_login) {
                $author_user = get_user_by('login', $author_login);
                $author_data .= $author_user->ID . ',';
            }

            $post_args = array(
                'author' => $author_data,
                'posts_per_page' => $show,
                'post_type' => $post_type,
                'category_name' => $category,
                'tag' => $tag,
            );
        }
    } elseif (empty($author)) {

        $post_args = array(
            'author' => $author,
            'posts_per_page' => $show,
            'post_type' => $post_type,
            'category_name' => $category,
            'tag' => $tag,
        );
    }

    // gets posts form database
    $post_query = new WP_Query( $post_args );

    // displays posts 
    if($post_query) {
        $data = '';
        $data .= '<div class="recent_post_by_cc"><ul>';
        while ($post_query->have_posts()) : $post_query->the_post();
            $data .= '<li>';



            // displays a link to the post, using the post title
            $data .= '<a href="' . get_permalink() . '" title="' . get_the_title() . '">';
            $data .= get_the_title();
            $data .= '</a>';

            // display a linked featured image if post has
            if($thumbnail == 'true') { 
                $data .= '<div class="cc_left">';
                if(has_post_thumbnail()) { 
                    $data .= '<a href="' . get_permalink() . '" title="' . get_the_title() . '">';
                    $data .= get_the_post_thumbnail(get_the_id(), 'thumbnail');
                    $data .= '</a>';
                }
                $data .= '</div>';
            }



            // displays the post excerpt if "excerpt" has been set to true
            if($excerpt == 'true') {
                $data .= '<p>' . get_the_excerpt() . '</p>';
            }

            $data .= '<div style="clear: both;"></div></li>';
        endwhile;

        // =================================================
        // Start of my added section
        // =================================================

            $data .= '<div class="more-home-link">';
        $category = get_the_category(); 
        $data .= '<a href="'.get_category_link($category[0]->cat_ID).'">More</a>';

        $data .= '</div>';


            $data .= '<div class="home-title">';
            $categories = get_the_category();
         echo '<h2><a href="'.$cat_link.'">'.$categories[0]->cat_name.'</a></h2>' ;
        $data .= '</div>';

        // =================================================
        // End of my added section
        // =================================================

        $data .='</div>';
        $data .= '</ul>';


    }

    wp_reset_postdata();

    echo $data;
  • 写回答

2条回答 默认 最新

  • dongyun9120 2016-01-25 12:03
    关注
    $cat_link
    

    seems to be uninitialized

    also:

    $category[0]->cat_name
    

    will only show the name of the first element ( [0] ). Are the posts in multiple categories?

    Probably you need something like:

    foreach (get_categories() as $category) {
        echo '<div>..... {$category->cat_name} ....</div>';
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题