仅显示主要类别(酵母)以进行自定义分类

时间:2019-03-06 18:30:42

标签: wordpress taxonomy yoast

我只需要显示模板(wordpress)中自定义分类法的主要类别(从YOAST默认添加的功能)。 该分类法来自learnpress插件中的自定义“ course_category”。 这是一种将类别添加到插件创建的课程中的分类法。

我想创建一个在循环中调用它的函数,但是我不知道如何针对自定义分类法正确编写它。

我发现此代码可用于标准类别(不用于自定义税项)

dados <-
  dados %>%
  mutate(
    xstart = as.factor(c(rep(1, nrow(.) / 2), rep(16, nrow(.) / 2))),
    xend = as.factor(c(rep(2, nrow(.) / 2), rep(18, nrow(.) / 2))),
    ystart = -Inf,
    yend = Inf
  )

p <-
  ggplot(dados, aes(x = group, y = var)) +
  theme_bw() +
  geom_rect(
    aes(
      xmin = xstart,
      xmax = xend,
      ymin = ystart,
      ymax = yend,
    ),
    fill = c(rep("gray90", nrow(dados) / 2), rep("gray80", nrow(dados) / 2))
  ) +
  geom_boxplot() +
  scale_x_discrete(
    breaks = as.character(c(0:2, 16:18)),
    labels = c("PT", "T01", "T02", "POT1", "POT2", "POT3")
  )
p

稍后可以通过

致电
<?php
function get_primary_category( $post = 0 ) {
if ( ! $post ) {
$post = get_the_ID();
}
// SHOW YOAST PRIMARY CATEGORY, OR FIRST CATEGORY
$category = get_the_category( $post );
$primary_category = array();
// If post has a category assigned.
if ($category){
$category_display = '';
$category_slug = '';
$category_link = '';
$category_id = '';

if ( class_exists('WPSEO_Primary_Term') )
{
  // Show the post's 'Primary' category, if this Yoast feature is available, & one is set
  $wpseo_primary_term = new WPSEO_Primary_Term( 'category', get_the_id( $post ) );
  $wpseo_primary_term = $wpseo_primary_term->get_primary_term();
  $term = get_term( $wpseo_primary_term );
  if (is_wp_error($term)) {
    // Default to first category (not Yoast) if an error is returned
    $category_display = $category[0]->name;
    $category_slug = $category[0]->slug;
    $category_link = get_category_link( $category[0]->term_id );
    $category_id = $category[0]->term_id;

  } else {
    // Yoast Primary category
    $category_display = $term->name;
    $category_slug = $term->slug;
    $category_link = get_category_link( $term->term_id );
    $category_id = $term->term_id;
  }
}
else {
  // Default, display the first category in WP's list of assigned categories
  $category_display = $category[0]->name;
  $category_slug = $category[0]->slug;
  $category_link = get_category_link( $category[0]->term_id );
  $category_id = $term->term_id;
}
$primary_category['url'] = $category_link;
$primary_category['slug'] = $category_slug;
$primary_category['title'] = $category_display;
$primary_category['id'] = $category_id;

}
return $primary_category;
}
?>

0 个答案:

没有答案