私はWordpressテーマを作成していて、フロントページでユーザーは自分の投稿をどのように注文したいかを選択することができます。それから、変数$user_choice
を通してユーザーの選択を行い、その選択に基づいてクエリを開発する2つの関数を作成しました。
しかし、スコープに問題があり、次のような注意/エラーが表示されます。
通知:未定義の変数:3行目の/Users/Chappell/Websites/LFB/wp-content/themes/lfb/front-page-loop.phpにあるuser_choice
Notice:未定義の変数:/Users/Chappell/Websites/LFB/wp-content/themes/lfb/front-page-loop.php内の5行目にあるuser_choice
Notice:7行目の/Users/Chappell/Websites/LFB/wp-content/themes/lfb/front-page-loop.phpにある未定義の変数:header_query
致命的なエラー:7行目の/Users/Chappell/Websites/LFB/wp-content/themes/lfb/front-page-loop.phpで、オブジェクト以外のオブジェクトに対してメンバー関数have_posts()を呼び出す
front-page.php
<?php get_header(); ?>
<div class="container">
<div class="row">
<div class="col-md-8">
<?php
$user_choice = 'most-recent';
echo $user_choice;
if (isset($_GET['formSubmit'])) {
$user_choice = $_GET['options'];
}
switch ($user_choice) {
case $user_choice === 'most-commented';
$title_text = 'most commented';
break;
case $user_choice === 'most-recent';
$title_text = 'most recent';
break;
case $user_choice === 'most-viewed';
$title_text = 'most viewed';
break;
case $user_choice === 'featured';
$title_text = 'featured';
break;
default:
echo 'No posts were found matching this query.';
}
?>
<div class="row">
<div class="col-sm-8">
<h1 class="page-header home-header">Now displaying <?php echo $title_text . " Liverpool articles"; ?></h1>
</div><!--col-sm-8-->
<div class="col-sm-4">
<form role="form" class="form-inline pull-right orderer">
<div class="form-group">
<label class="sr-only" for="options">Order By:</label>
<select class="form-control order-select" name="options">
<option value="most-recent" <?php if ($user_choice === 'most-recent') { echo 'selected'; }?>>Most Recent</option>
<option value="most-commented" <?php if ($user_choice === 'most-commented') { echo 'selected'; }?>>Most Commented</option>
<option value="most-viewed" <?php if ($user_choice === 'most-viewed') { echo 'selected'; }?>>Most Viewed</option>
<option value="featured" <?php if ($user_choice === 'featured') { echo 'selected'; }?>>Featured</option>
</select>
</div><!--form-group-->
<div class="form-group">
<input type="submit" class="btn btn-default" id="orderer" value="Submit" name="formSubmit">
</div><!--form-group-->
</form>
</div><!--col-sm-4-->
</div><!--row-->
<?php get_template_part('front-page-loop'); ?>
</div><!--col-md-8-->
<?php get_sidebar(); ?>
</div><!--row-->
</div><!--container -->
<?php get_footer(); ?>
Front-page-loop.php
<?php
/* Takes the user choice from the home page select button and produces
* a WP_Query relevant to that choice. Displays 1 posts per query.
* @param string $user_choice
*/
function main_post_arg_Finder($user_choice) {
switch ($user_choice) {
case $user_choice === 'most-commented';
$args = array(
'orderby' => 'comment_count',
'posts_per_page' => 1
);
break;
case $user_choice === 'most-recent';
$args = array(
'orderby' => 'date',
'posts_per_page' => 1,
'cat' => 239
);
break;
case $user_choice === 'most-viewed';
$args = array(
'meta_key' => 'lfb_post_views_count',
'orderby' => 'meta_value_num',
'posts_per_page' => 1
);
break;
case $user_choice === 'featured';
$args = array(
'orderby' => 'date',
'posts_per_page' => 1,
'category_name' => 'featured'
);
break;
default:
echo 'No posts were found matching this query.';
}
$header_query = new WP_Query($args);
return $header_query;
}
/* Takes the user choice from the home page select button and produces
* a WP_Query relevant to that choice. Displays an unlimited amount of posts per
* query.
* @param string $user_choice
*/
function posts_arg_Finder($user_choice) {
switch ($user_choice) {
case $user_choice === 'most-commented';
$args = array(
'orderby' => 'comment_count',
);
break;
case $user_choice === 'most-recent';
$args = array(
'orderby' => 'date',
'cat' => 239
);
break;
case $user_choice === 'most-viewed';
$args = array(
'meta_key' => 'lfb_post_views_count',
'orderby' => 'meta_value_num',
);
break;
case $user_choice === 'featured';
$args = array(
'orderby' => 'date',
'category_name' => 'featured'
);
break;
default:
echo 'No posts were found matching this query.';
}
$query = new WP_Query($args);
return $query;
}
?>
<div class="row">
<?php echo $user_choice;
main_post_arg_Finder($user_choice);
if ( $header_query->have_posts() ) : while ( $header_query->have_posts() ) : $header_query->the_post() ?>
<?php $do_not_duplicate = $post->ID; ?>
<div class="col-md-12">
<a href="<?php the_permalink();?>"><h2><?php the_title();?></h2></a>
<?php the_post_thumbnail('thumbnail', array('class' => 'img-responsive')); ?>
<?php post_meta(); ?>
<?php the_excerpt(); ?>
<hr>
</div><!--col-md-12-->
<?php endwhile;
else :
echo '<p>No main post found</p>';
endif; ?>
</div><!--row-->
<div class="row">
<?php posts_arg_Finder();
$i = 0;
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
if ( $post->ID == $do_not_duplicate ) continue;
if ($i != 0 && $i % 1 == 0) { // add new row after 1 post
echo '</div><!--row--><div class="row">';
}
$i++; ?>
<div class="col-md-12">
<a href="<?php the_permalink();?>"><h2><?php the_title();?></h2></a>
<?php post_meta(); ?>
<?php the_excerpt(); ?>
<hr>
</div><!--col-md-12-->
<?php endwhile;
else :
echo '<p>No posts found</p>';
endif; ?>
</div><!--row-->
私は別のスレッドで答えを見つけました。 locate_templateを通して変数を渡す
基本的にWordPressの関数get_template_partは変数$user_choice
のスコープを変更します。 include()を使用したときのPHPデフォルトの動作は、現在のスクリプトの変数がインクルードファイルでまだ使用可能であることです。ただし、get_template_part()
は関数なので、そのテンプレートファイル内の変数はすべてローカルスコープを持ちます。それゆえエラー。