使用Form更改PHP变量

时间:2012-07-18 作者:marctain

我使用此功能通过meta\\u键拉取自定义帖子:

<?php $args = array(
    \'numberposts\'     => 10,
    \'meta_key\'        => \'allvotes\',
    \'orderby\'         => \'meta_value\',
    \'order\'           => \'DESC\',
    \'post_type\'       => \'things\',
    \'post_status\'     => \'publish\' );
$mystuff = get_posts( $args );
?> 
我想使用一个表单来编辑我的变量,但当我尝试我的代码时,它不会返回任何帖子。。。

<?php
 $filter = $_POST[\'country\'];
 $submit = $_POST[\'submit\'];
 if(isset($submit)){
    $args = array(
    \'numberposts\'     => 10,
    \'meta_key\'        => \'$filter\',
    \'orderby\'         => \'meta_value\',
    \'order\'           => \'DESC\',
    \'post_type\'       => \'things\',
    \'post_status\'     => \'publish\' );
$mystuff = get_posts( $args );
$name = $filter ;
}
else {
    $args = array(
    \'numberposts\'     => 10,
    \'meta_key\'        => \'canada\',
    \'orderby\'         => \'meta_value\',
    \'order\'           => \'DESC\',
    \'post_type\'       => \'things\',
    \'post_status\'     => \'publish\' );
$mystuff = get_posts( $args );
$name = \'Everywhere\';
};
?>

1 个回复
最合适的回答,由SO网友:EAMann 整理而成

不要将变量放在单引号中。

这是:

if(isset($submit)){
    $args = array(
        \'numberposts\'     => 10,
        \'meta_key\'        => \'$filter\',
        \'orderby\'         => \'meta_value\',
        \'order\'           => \'DESC\',
        \'post_type\'       => \'things\',
        \'post_status\'     => \'publish\' );
    $mystuff = get_posts( $args );
    $name = $filter ;
}
应该是这样的:

if(isset($submit)){
    $args = array(
        \'numberposts\'     => 10,
        \'meta_key\'        => $filter,
        \'orderby\'         => \'meta_value\',
        \'order\'           => \'DESC\',
        \'post_type\'       => \'things\',
        \'post_status\'     => \'publish\' );
    $mystuff = get_posts( $args );
    $name = $filter ;
}

结束

相关推荐

PHP variables in a post?

我对PHP非常陌生。在Wordpress网站上,我想使用带有Javascript API V3的自定义谷歌地图。一些帖子中会有地图,我想使用PHP变量来确定唯一的位置、缩放级别、信息窗口文本等。Google maps javascript将包含所有样式、地图类型以及地图之间一致的任何内容。因此,对于有地图的帖子,在帖子中写上类似的内容:<?php $location = \"-34.397, 150.644\"; $zoom = \"8\"; ?> &