2016-01-25 87 views
0

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

我想要做的是将标题和“更多”链接添加到每个类别,但我不能完全正确地做到这一点。

下面是插件代码,我的新部分在底部附近的注释中突出显示。在那一刻,它只显示头两个类别的标题,然后重复相同的标题和链接,即使类别不同。

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

请详细解释一下你想达到的目标。您想要添加'the_title'和'more_links',但要添加到:一个类别或所有类别?您希望打印分类链接的位置 - 在每个文章之后(因为不同的文章可以附加到不同的类别),或者打印所有文章之后。因为现在你可以从上次打印的文章中获得分类,并且只打印其中的第一个分类'$ categories [0]' – pgk

+0

我想为每个分类添加分类标题和更多链接。我想显示分类标题,4个最新的帖子和帖子标题和精选图片,然后显示更多链接,将您带到分类页面。我想为每个类别执行此操作。 – JohnThomas

回答

0
$cat_link 

似乎未初始化

也:

$category[0]->cat_name 

将只显示所述第一元件的名称([0])。这些帖子是否属于多个类别?

也许你需要的东西,如:

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

我想你从这个插件为每个类别运行此简码,并通过category_slug至简码,才能够获得最后的4个员额,因为这条线从$post_args'category_name' => $category,

如果是这样,那么你已经在这里有当前类别蛞蝓,你可以抓住类别数据与get_category_by_slug($category);然后如果你使用这个get_the_category()你可以做到这一点

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

,并从阵列抢第一个结果,你可以不要支持你得到适当的类别,因为一个职位可以有多个类别,而你只需选择第一个职位,而不必知道其顺序。

因此,只有2个类别显示在您的结果中。

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

[shortcode category1] 
your_function('category1'); 

[shortcode category2] 
your_function('category2'); 
+0

啊,你是对的!感谢您的帮助,非常感谢。 – JohnThomas

+0

您的欢迎。很高兴我能帮上忙。 :)请参阅我的编辑答案的一些建议,如果这是你需要的答案,将很高兴,如果你把它标记为corect之一。 – pgk