WPテーマを作る

style.cssとindex.phpが必須。
functions.phpはなくても動く。

固定ページにテンプレートを当てる時は、page-***.php
ファイルに「* Template Name: ***」を書いておくと、投稿ページにテンプレートのセレクトが表示されるようになる。

wp_head()とwp_footer()は必須なので必ず書くように。
wp_head()は</head>の直前に、wp_footer()は </body>の直前に書く。
これがないとプラグインが動かなかったりする。

// アイキャッチ画像を有効にする。
アイキャッチ画像を使う時は、functions.phpに記載が必要。
add_theme_support(‘post-thumbnails’);

// 追加関数
function add_files() {

wp_deregister_script(‘jquery’);
wp_enqueue_script( ‘jquery’, ‘//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js’, “”, “20160608”, false );
wp_enqueue_script( ‘smart-script’, get_template_directory_uri() . ‘/js/common.js’, array( ‘jquery’ ), ‘20160608’, true );
wp_enqueue_style( ‘main’, get_template_directory_uri() . ‘/css/main.css’, “”, ‘20160608’ );
}
add_action( ‘wp_enqueue_scripts’, ‘add_files’ );

//アーカイブページのスラッグを設定する
これを設定した際は、パーマリンク設定の画面を「保存」しないと適用されない。

function post_has_archive( $args, $post_type ) {

if ( ‘post’ == $post_type ) {
$args[‘rewrite’] = true;
$args[‘has_archive’] = ‘news’; //任意のスラッグ名
}
return $args;

}
add_filter( ‘register_post_type_args’, ‘post_has_archive’, 10, 2 );

テーマのフォルダを追加する

<img src=”<?php echo get_template_directory_uri(); ?>/images/s_1.svg” alt=””>

uploadのフォルダパスを取得する

<?php $upload_dir = wp_upload_dir(); echo $upload_dir[‘baseurl’]; ?>

index.phpはとりあえずこれにしとこ

<?php get_header();?>

<?php
if ( have_posts() ) :
while ( have_posts() ) : the_post(); ?>
<!–コンテンツここから–>

<?php the_content();?>

<?php
endwhile;
endif;
?>

<!–コンテンツここまで–>
<?php get_footer();?>

ショートコードでURLを呼び出す

//親テーマ
add_shortcode(‘oya’, ‘shortcode_gtdu’);
function shortcode_gtdu() {
return get_template_directory_uri();
}

//子テーマ
add_shortcode(‘ko’, ‘shortcod_gsdu’);
function shortcod_gsdu() {
return get_stylesheet_directory_uri();
}
//TOPページ
add_shortcode(‘top’, ‘shortcode_hurl’);
function shortcode_hurl() {
return home_url( ‘/’ );
}

//月別アーカイブ
add_shortcode(‘arcive’, ‘shortcode_arcive’);
function shortcod_arcive() {
return wp_get_archives( $args );
}

//カテゴリーアーカイブ
add_shortcode(‘cat_$id’, ‘shortcode_cat_$id’);
function shortcod_cat_$id() {
return get_category_link($id);
}

//投稿タイプのアーカイブ
add_shortcode(‘post_$post_type’, ‘shortcode_post_$post_type’);
function shortcod_post_$post_type() {
return get_post_type_archive_link( $post_type );
}

サイトのURLを取得する

<a href=”<?php echo esc_url( home_url( ‘/’ ) ); ?>about”>製品案内<span>PRODUCT</span></a>

yoast SEOの設定

左のメニューから「検索での見えかた」を選択。
「タイトルを強制的に書き換える」でtitleを設定する。テンプレートにtitleタグがあるとそちらが優先されてしまうのでテンプレート「header.php」からtitleタグは消しておいたほうがいい。「%%sitedesc%% 」は、一般設定のキャッチフレーズが表示される。

固定ページに自動的に<br>や<p>が入るのを防ぐ

add_filter(‘the_content’, ‘wpautop_filter’, 9);
function wpautop_filter($content) {
global $post;
$remove_filter = false;

$arr_types = array(‘page’); //自動整形を無効にする投稿タイプを記述 =固定ページ
$post_type = get_post_type( $post->ID );
if (in_array($post_type, $arr_types)){
$remove_filter = true;
}

if ( $remove_filter ) {
remove_filter(‘the_content’, ‘wpautop’);
remove_filter(‘the_excerpt’, ‘wpautop’);
}

return $content;
}

カスタムフィールドを取得

get_post_meta($post->ID , ‘カスタムフィールドの名前’ ,true);
表示中のカスタムフィールドは「$post->ID」で取得。指定したい時はここに投稿のIDをいれる。
最後のtrueをfalseにすると配列で返ってくる。複数の値がある場合はfalseにするんだろう。

固定ページのカスタムフィールドを取得

<!–?php echo get_post_meta(62, ‘about_subsidy_subtitle1’, true);?–>
固定ページのID。カスタムフィールドのスラッグ

GETで値を受け渡し

そのままではGETやPOSTの受け渡しはできない。
functions.phpに以下の関数を追記する必要がある。
query_varsの配列にGETなどで受け渡すkeyを登録する。

function set_org_query_vars( $query_vars ) {
$query_vars[] = ‘prm1’; // 独自のパラメータ prm1を配列最後尾に追加する。
$query_vars[] = ‘prm2’; // 独自のパラメータ prm2を配列最後尾に追加する。
return $query_vars;
}
add_filter(‘query_vars’, ‘set_org_query_vars’);

記事一覧 アーカイブの作り方

WordPressでカテゴリーごと・タグごとの投稿一覧を表示する方法

前の記事

動画再生 フルスクリーン

次の記事

ページ送り