我正在构建一个Wordpress主题,在首页上,用户可以选择他们想要的帖子排序方式。然后,我创建了两个函数,通过变量进行用户选择$user_choice
并根据该选择开发一个查询。
但是,我在范围方面遇到了问题,并收到了以下通知/错误:
注意:未定义变量:user\\u choice in/Users/Chappell/Websites/LFB/wp content/themes/LFB/front page循环。php在线3
注意:未定义变量:user\\u choice in/Users/Chappell/Websites/LFB/wp content/themes/LFB/front page循环。php在线5
注意:未定义变量:header\\u query in/Users/Chappell/Websites/LFB/wp content/themes/LFB/front page循环。php在线7
致命错误:调用/Users/Chappell/Websites/LFB/wp content/themes/LFB/front page循环中的非对象上的成员函数have\\u posts()。php在线7
首页。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-->