【问题标题】:URL variable use in PHP array (wordpress related)在 PHP 数组中使用 URL 变量(wordpress 相关)
【发布时间】:2012-10-10 20:27:39
【问题描述】:

这个棘手的问题让我很抓狂。我的网址是:www.mysite.com/page/?id=sun

我想获取变量 (sun) 并将其放入我的 PHP 数组中:

<?php 
 $tag = $_GET['id'];

 wp_reset_query();

 query_posts(array('post_type'=> 'projects','technologiestags'=> $tag) );
 if(have_posts()):
 while(have_posts()):the_post();
 ?>

 <div class="technology">
 <h4><?php the_title(); ?></h4><br/><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(''); ?></a>

</br></br><div class="readmore"><a href="<?php the_permalink(); ?>">Read more&nbsp;&raquo;</a></div>

</div><!--END of technology Id-->

<?php endwhile; endif; wp_reset_query();?>

如您所见,我使用了“.echo "$tag".”,但它不起作用。有没有其他方法可以获取 URL 变量并将其插入 PHP。

提前致谢。

【问题讨论】:

  • 如果这确实是您的完整来源,请记住 if():while():(冒号)需要关闭 endif/endwhile 语句。如果您真的想要单个语句体,请删除冒号。
  • 嗨 lanzz,完整代码是: 'projects','technologiestags'=> $tag) ); if(have_posts()): 而(have_posts()):the_post(); ?>
  • 嗯,很有帮助。考虑在您的问题中对其进行编辑。
  • 抱歉,我已将完整代码添加到顶部的主问题框中...

标签: php arrays wordpress url variables


【解决方案1】:
query_posts(array('post_type'=> 'projects','technologiestags' => $tag);

【讨论】:

    【解决方案2】:

    $tag 是一个变量,可以用来代替传递字符串:

    query_posts(array('post_type'=> 'projects','technologiestags' => $tag);
    

    【讨论】:

    • 非常感谢仍然没有运气。出现错误:解析错误:语法错误,意外的 T_VARIABLE,在 /home/public_html/wp-content/plugins/wp-php-widget/wp-php-widget.php(52) 中期待 ')' : eval()' d 代码在第 7 行
    • 只需在分号前添加另一个右括号即可消除错误。
    【解决方案3】:

    一些改变,试试:

    if(isset($_GET['id'])):
            $tag = stript_tags($_GET['id']);
            wp_reset_query();
            query_posts(array(
                    'post_type'=> 'projects',
                    'technologiestags'=> $tag)
            );
            if(have_posts()):
                while(have_posts()):
                    the_post();
                    ?>
                    <div class="technology">
                        <h4><?= the_title(); ?></h4>
                        <div class="post-title"><a href="<?php the_permalink(); ?>"><?= the_post_thumbnail(''); ?></a></div>
                        <div class="readmore"><a href="<?= the_permalink(); ?>">Read more&nbsp;&raquo;</a></div>
                    </div>
                    <?
                endwhile;
                wp_reset_query();
            endif;
        endif;
    

    【讨论】:

    • 嗨 NeoMacuser,不幸的是我没有运气 - 错误是:解析错误:语法错误,意外 T_VARIABLE,期待 ')'
    猜你喜欢
    • 2018-02-20
    • 2012-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多