【问题标题】:Category For Pages In WordPressWordPress中页面的类别
【发布时间】:2015-07-13 11:36:53
【问题描述】:

我需要对 WordPress 的搜索结果进行分类,我通过向帖子和自定义帖子类型添加类别来做到这一点。现在,只列出没有任何类别的页面,我认为也可以将类别添加到页面,但我不知道它的后果。

请您分享您的想法和经验。

谢谢

【问题讨论】:

  • page 帖子类型并不意味着有类别。问题是,没有设置与页面相关的功能来接受或使用类别。
  • @PieterGoosen,谢谢,但我只需要在搜索结果中对页面进行分类,我不会将分类用于任何其他目的,这会导致任何性能问题吗?
  • 不,不应该。我只是觉得您在搜索页面上的排序错误。你真的需要分类页面来进行排序
  • @PieterGoosen,谢谢,我创建了搜索页面,它会分类显示内容,例如当我们搜索音乐时,它会列出内容,内容将按摇滚、爵士、流行等分类...现在客户需要对“帮助”、“联系表格”等页面进行分类...

标签: wordpress custom-wordpress-pages


【解决方案1】:

Wordpress 不提供在页面中创建类别的选项。

可以做的一件事是修改搜索查询 这将仅在所有页面中搜索您。

function SearchFilter($query) {
    if ($query->is_search) {
       $query->set('post_type', 'page');
    }
    return $query;
    }

    add_filter('pre_get_posts','SearchFilter');

当您只想在搜索中显示页面时,在某些特定条件下添加此挂钩。

另一方面将显示除 pages 之外的所有内容。因为你将不得不通过他们的 ID 排除页面

function SearchFilter($query) {
    if ($query->is_search) {
       $excludeId = array(23,23,23);
       $query->set('post__not_in', array($excludeId));
    }
    return $query;
    }

    add_filter('pre_get_posts','SearchFilter');  

【讨论】:

  • 脆弱的解决方案。您不应该将页面 ID 放入模板中。
  • 好的,你可以通过Wp_Query获取数组中需要排除的所有页面ID
【解决方案2】:

WordPress 本身并没有您想要的全球分类法。 WordPress 中的类别与博客文章相关联。

为了实现这一点,我将结合使用令人难以置信的 Advanced Custom Fields (ACF) 插件和修改 functions.php 来为您的网站添加自定义分类并将其应用于您的所有帖子类型。

步骤 1

在functions.php中,制作一个像这样的自定义分类(根据您的需要进行修改):

// add_action registers the taxonomy into wordpress
add_action( 'init', 'setup_my_tax' );

// this function sets up the taxonomy to whatever standards you want
// reference register_taxonomy on codex.wordpress.org
function setup_my_tax() 

  // first parameter becomes the slug of the tax
  register_taxonomy( 'capabilities', array( 'post' ), array(

    // labels will determine how it show up in a menu
    'labels' => array(
      'add_new_item' => 'Add New ',
      'all_items' => 'All Capabilities',
      'edit_item' => 'Edit Capability',
      'menu_name' => 'Capabilities',
      'name' => 'Capabilities',
      'new_item' => 'New Capability',
      'not_found' => 'No Capabilities Found',
      'not_found_in_trash' => 'No Capabilities Found in Trash',
      'parent' => 'Parent of Capability',
      'search_items' => 'Search Capabilities',
      'singular_name' => 'Capability',
      'view_item' => 'View Capability'
    ),

    // important individual settings from register_taxonomy
    'hierarchical' => true,
    'public' => true,
    'query_var' => true,
    'show_admin_column' => true,
    'show_ui' => true
  ));
}

第二步

安装 ACF 后,您将使用 GUI 创建包含此分类的自定义字段集。自定义字段集下方是 rule 选项,它们向您展示如何将自定义位应用于所有实体。

第三步

page.php 和其他模板中,您可以像这样引用您的分类术语:

// Put the capabilities into an array variable
$capObjects = get_field('capabilities');

// Iterate through the the array of tags and output them
// In WordPress, you have to use the term ID to lookup the term name
foreach ($capObjects as $capObject):
  echo '<li class="capabilities-item">';
  echo '<a href="' . get_term_link($capObject) . '">' . $capObject->name . '</a> ';
  echo '</li>';
endforeach;

您现在可以通过跨越所有类型内容的真实标签来调整您的搜索模板。

【讨论】:

    猜你喜欢
    • 2017-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多