【问题标题】:Adding category title to wordpress plugin将类别标题添加到 wordpress 插件
【发布时间】:2016-01-25 11:28:43
【问题描述】:

我正在使用 Wordpress 插件将类别列表和 4 个帖子添加到网站的主页。

我正在尝试为每个类别添加标题和“更多”链接,但我不能完全正确。

下面是插件代码,我的新部分在底部附近的 cmets 中突出显示。目前,它只显示前 2 个类别的标题,然后再次重复相同的标题及其链接,即使类别不同。

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

【问题讨论】:

  • 请更详细地解释您想要实现的目标。您想添加the_titlemore_links 但添加到什么:添加到一个类别或添加到所有类别?您想要打印类别链接的位置 - 在每个帖子之后(因为不同的帖子可以附加到不同的类别),或者在打印所有帖子之后。因为现在您从上次打印的帖子中获取类别,并且只打印其中的第一个 $categories[0]
  • 我想为每个类别添加类别标题和更多链接。我想显示类别标题、带有帖子标题和特色图片的 4 个最新帖子,然后显示更多链接,将您带到类别页面。我想对每个类别都这样做。

标签: php wordpress


【解决方案1】:

我想你从这个插件为每个类别运行了这个简码,并将category_slug 传递给简码,以便能够获得最后 4 个帖子,因为来自

的这一行
$post_args: 'category_name' => $category,

如果是这样,那么您已经在此处拥有当前类别 slug,您可以使用

获取类别数据
get_category_by_slug( $category ); 

那么你就可以这样做了

<?php
    $curr_cat = get_category_by_slug( $category );

    $data .= '<div class="more-home-link">';
    $data .=    '<a href="'.get_category_link( $curr_cat->cat_ID ).'">More</a>';
    $data .= '</div>';

    $data .= '<div class="home-title">';
    $data .= '<h2><a href="'.get_category_link( $curr_cat->cat_ID ).'">'.$curr_cat->cat_name.'</a></h2>';
    $data .= '</div>';
?>

如果你使用get_the_category(),并且只是从数组中获取第一个结果,你不能确定你得到了正确的类别,因为一个帖子可以有多个类别,你只选择其中的第一个,不知道它的顺序。

因此,您的结果中仅显示 2 个类别。

此外,更改插件代码也不是一个好习惯,因为这消除了以后更新此插件的可能性。所以也许更好的方法是创建自己的函数,其中 echoreturn shortcode 带有类别链接,然后在插件的每个短代码之后调用它。比如:

[shortcode category1]
your_function('category1');

[shortcode category2]
your_function('category2');

【讨论】:

  • 啊,是的,你是对的!感谢您的帮助,非常感谢。
  • 欢迎您。很高兴我能帮助你。 :) 请查看我编辑的一些建议的答案,如果这是您需要的答案,如果您将其标记为正确的答案将很高兴。
【解决方案2】:
$cat_link

似乎未初始化

还有:

$category[0]->cat_name

只会显示第一个元素的名称( [0] )。帖子是否属于多个类别?

可能你需要类似的东西:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-16
    • 2012-06-09
    • 1970-01-01
    • 2011-12-23
    相关资源
    最近更新 更多