テンプレート
2021.04.11
WordPress用テンプレート
functions.php
ACFから値を取得する
function get_ACF_datas() {
$args = array(
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => array(' "post_type" '),
);
$my_posts = get_posts($args);
$datas = array();
foreach($my_posts as $post) {
setup_postdata($post);
$post_id = $post -> ID;
$fields = get_field_objects($post_id);
if ($fields && ($fields[' "field_name" ']['value'] == '1')) {
$field_value = $fields[' "field_name" ']['value'];
$data = new Data( $field_value, ... );
array_push($datas, $data);
}
}
return $datas
}
SCFから値を取得する
function get_SCF_datas() {
$post_id = 1; // データを保管しているpost_id
$grouplist = SCF::get(' "group_fields_name" ', $post_id);
$datas = array();
foreach($grouplist as $group => $value) {
if ($value['field_name'][0] == 'true') {
$field_value = $value[' "field_name" '];
$data = new Data( $field_value, ... );
array_push($datas, $data);
}
}
return $datas;
}
サイト内検索の検索対象にACF, SCFを追加する
/**
* サイト内検索の検索対象にカスタムフィールドを追加する
*/
function cf_search_join( $join ) {
global $wpdb;
if ( is_search() ) {
$join .= ' LEFT JOIN ' . $wpdb->postmeta . ' ON ' . $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
}
return $join;
}
add_filter( 'posts_join', 'cf_search_join' );
function cf_search_where( $where ) {
global $wpdb;
if ( is_search() ) {
$where = preg_replace(
"/\(\s*" . $wpdb->posts . ".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
"(" . $wpdb->posts . ".post_title LIKE $1) OR (" . $wpdb->postmeta . ".meta_value LIKE $1)", $where );
// 特定のカスタムフィールドを検索対象から外す(※1)
// $where .= " AND (" . $wpdb->postmeta . ".meta_key NOT LIKE 'number')";
// $where .= " AND (" . $wpdb->postmeta . ".meta_key NOT LIKE 'zip')";
// $where .= " AND (" . $wpdb->postmeta . ".meta_key NOT LIKE 'access')";
}
return $where;
}
add_filter( 'posts_where', 'cf_search_where' );
function cf_search_distinct( $where ) {
global $wpdb;
if ( is_search() ) {
return "DISTINCT";
}
return $where;
}
add_filter( 'posts_distinct', 'cf_search_distinct' );
CSS
html {
font-family: 游ゴシック体, "Yu Gothic", YuGothic, "ヒラギノ角ゴ Pro", "Hiragino Kaku Gothic Pro", メイリオ, Meiryo, "MS Pゴシック", "MS PGothic", DengXian, sans-serif !important;
}
body {
margin: 0;
}
img {
vertical-align: top;
}
input, textarea {
font-family: inherit;
font-weight: inherit;
}
.hidden {
visibility: hidden;
}
.relative {
position: relative;
}
.absolute {
position: absolute;
}
.button {
border: none;
border-radius: 1rem;
color: white;
padding: 8px 62px;
font-size: 0.8rem;
cursor: pointer;
transition: 0.1s all;
box-shadow: 1px 1px 4px black;
}
.button:hover {
transform: scale(1.1, 1.1);
transition: 0.1s all;
}
input {
border: none;
border-radius: 24px;
box-shadow: 1px 1px 4px black;
padding: 10px 16px;
}
.flex {
display: flex;
}
.flex-center {
display: flex;
justify-content: center;
align-items: center;
}
.flex-start {
display: flex;
justify-content: flex-start;
align-items: center;
}
.flex-end {
display: flex;
justify-content: flex-end;
align-items: center;
}