WordPress の検索結果を分類する必要があります。投稿とカスタム投稿タイプにカテゴリを追加することでそれを行いました。現在、カテゴリのないページのみがリストされています。ページにカテゴリを追加することも可能だと思いますが、その結果はわかりません.
あなたの考えや経験を教えてください。
ありがとうございました
WordPress の検索結果を分類する必要があります。投稿とカスタム投稿タイプにカテゴリを追加することでそれを行いました。現在、カテゴリのないページのみがリストされています。ページにカテゴリを追加することも可能だと思いますが、その結果はわかりません.
あなたの考えや経験を教えてください。
ありがとうございました
Wordpress には、ページにカテゴリを作成するオプションがありません。
できることの 1 つは、検索クエリを変更することです。これにより、すべてのページでのみ検索が行われます。
function SearchFilter($query) {
if ($query->is_search) {
$query->set('post_type', 'page');
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
検索でページのみを表示したい場合、特定の条件でこのフックを追加します。
反対側は、ページ以外のすべてを表示することになります。そのため、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');
WordPress には、必要なグローバル分類法がネイティブにありません。WordPress のカテゴリは、ブログ記事に関連付けられています。
これを実現するには、信じられないほどのAdvanced Custom Fields
(ACF) プラグインと変更を組み合わせて使用functions.php
し、サイトにカスタム分類法を追加して、それをすべての投稿タイプに適用します。
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;
すべての種類のコンテンツにまたがる真のタグで検索テンプレートを調整できるようになりました。