Skip to content

Instantly share code, notes, and snippets.

@guzuri
guzuri / WordPress.php
Created October 3, 2013 01:32
読み込んでいるテンプレートの名称を取得する
<?php
global $template;
$now_template = $template;
//hoge:Themeを入れているフォルダの名称
$now_template = preg_replace("/.+hoge\/(.+).php/","\\1",$now_file);
?>
@guzuri
guzuri / WordPress.php
Created October 3, 2013 10:06
ループ、カスタムフィールドの例
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
//ACFの基礎形
<?php the_field('hoge'); ?>
<?php endwhile;?>
<?php endif; ?>
@guzuri
guzuri / WordPress.php
Created October 3, 2013 10:16
$post の中身
stdClass Object
(
[ID] => 4025
[post_author] => 2
[post_date] => 2010-04-24 18:07:14
[post_date_gmt] => 2010-04-24 09:07:14
[post_content] => WordPress ではページ毎にテンプレートを用意することが可能...
[post_title] => WordPress のページテンプレートの情報を取得する方法
[post_category] => 0
[post_excerpt] =>
@guzuri
guzuri / WordPress.php
Created October 4, 2013 04:18
カスタムタクソノミーの出力(リンク無し)
<?php
//hoge:出力したいカスタムタクソノミー
$tarms = get_the_terms( $post -> ID ,'hoge' );
foreach ( $tarms as $tarm ) {echo $tarm -> name;}
//'category''post_tag'でそれぞれdefaultカテゴリーとタグも利用可
//$termsの[数値]はどーもカテゴリID諸々が入る様で[0]とかでは空っぽ。
?>
@guzuri
guzuri / WordPress.php
Created October 10, 2013 04:39
get_postsテンプレート
<?php if(get_field('movie_flag') == 'search'): ?>
<?php
$search = get_field('movie_tag');
$max_result = get_field('movie_count');
$arg =
array(
'post_type' => 'd_movie',
'posts_per_page' => $max_result ,
'tax_query' => array(
array(
@guzuri
guzuri / WordPress.php
Created October 10, 2013 05:00
WordPress テンプレートタグ
<?php the_time(); ?>
現在の記事の投稿時間を表示。
<?php the_date(); ?>
現在の記事の投稿日時を表示。
<?php the_title(); ?>
現在の記事のタイトルを表示
<?php the_content(); ?>
@guzuri
guzuri / WordPress.php
Created October 11, 2013 00:06
search.phpを読みこむデータによって振り分ける
//function.phpに追記
add_filter('template_include','custom_search_template');
function custom_search_template($template){
if ( is_search() ) {
//表示する投稿タイプを取得→それをsearch-★.phpの★部分に埋め込む
$post_types = get_query_var('post_type');
foreach ( (array) $post_types as $post_type )
$templates[] = "search-{$post_type}.php";
$templates[] = 'search.php';
$template = get_query_template('search',$templates);
@guzuri
guzuri / WordPress.php
Created October 23, 2013 09:41
タクソノミーのスラッグから各種情報取得
<?php
//IDの取得
echo get_term_by('slug',$term,$taxonomy)->term_id;
//名前の取得
echo get_term_by('slug',$term,$taxonomy)->name;
//URLの取得
echo get_term_link($term,$taxonomy);
/*
@guzuri
guzuri / WordPress.php
Created November 21, 2013 02:16
タクソノミーカテゴリーページで、現在表示中の諸々を表示する
<?php
//困った時の
print_r($wp_query);
//で、
$hoge = get_queried_object();
print_r($hoge);