WordPress WordPressの関数で、echoがいるのといらないのがある~
ひよっこWebデザイナーのひよこです(⌒∇⌒)
最近、WordPressの勉強を始めました。
WordPressの関数って、出力するのにechoがいるのといらないのがある…
なんとなく、get_で始まる関数は、echoをつけるんかな…という感じ。
the_title()とか、the_content()は、echoがいらない…。
なんで? なんで? なんで? (・_・?
気になって、夜も眠れなくなりそうなので、(←うそつけー)
WordPressのサイトで関数の中身を調べてみました。
WordPress › the_title() | Function | WordPress Developer Resources
function the_title( $before = '', $after = '', $echo = true ) {
$title = get_the_title();
if ( strlen($title) == 0 )
return;
$title = $before . $title . $after;
if ( $echo )
echo $title;
else
return $title;
}
な~んだ。the_title()は、関数の中でechoの処理までしてるんだ~
んで、get_the_date()も調べてみると…
WordPress › get_the_date() | Function | WordPress Developer Resources
function get_the_date( $d = '', $post = null ) {
$post = get_post( $post );
if ( ! $post ) {
return false;
}
if ( '' == $d ) {
$the_date = mysql2date( get_option( 'date_format' ), $post->post_date );
} else {
$the_date = mysql2date( $d, $post->post_date );
}
/**
* Filter the date a post was published.
*
* @since 3.0.0
*
* @param string $the_date The formatted date.
* @param string $d PHP date format. Defaults to 'date_format' option
* if not specified.
* @param int|WP_Post $post The post object or ID.
*/
return apply_filters( 'get_the_date', $the_date, $d, $post );
}
こっちは、returnで値を戻してるわー。
だから、戻り値を受け取った側で、
出力処理(echo)をする必要があるんやね~(;^ω^)
しかし…
echoの処理までしてくれるのか否かは、命名規約でわかるってことなのかな?
WordPressの関数の命名規約について探してみたけど、うまくヒットせず…。
もしご存知の方がおられたら、教えてくださいm(_ _)m
とにもかくにも、ひよこは今日も1つ勉強しましたーーーーーっ(⌒∇⌒)/
(こんなんで大丈夫か?!)