如何在另一个 PHP 代码中插入一个 PHP 代码?

发布于 2024-12-08 02:46:27 字数 448 浏览 2 评论 0原文

我正在使用 WordPress。我很高兴看到代码,但我也有兴趣直接学习如何自己做 - 所以如果你知道我在哪里可以找到教程或可以给我信息,我将不胜感激!

我正在调用帖子并希望在 PHP 代码中包含 PHP 代码,这是用于主题选项面板的。

<?php
query_posts('cat=-51&posts_per_page=3&paged='.$paged); 
if (have_posts()) :
?>

我想把 51 放在哪里:

<?php echo get_option('to_postc_home'); ?>

我想把 3 放在哪里:

<?php echo get_option('to_posti_home'); ?>

I'm using WordPress. I appreciate being shown the code, but this is one I am interested in straight out learning how to do myself too - so if you know where I can find a tutorial or can give me information I'd appreciate it!

I'm calling posts and want to include a PHP code within a PHP code, this is for a theme options panel.

<?php
query_posts('cat=-51&posts_per_page=3&paged='.$paged); 
if (have_posts()) :
?>

Where the 51 is I want to put:

<?php echo get_option('to_postc_home'); ?>

Where the 3 is I want to put:

<?php echo get_option('to_posti_home'); ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

迎风吟唱 2024-12-15 02:46:27

如果我的解释正确,这就是您所需要的,请使用串联运算符 . 使用这些函数代替纯文本,例如:'this is text' 与 <代码>'这个'.get_option('stuff').'文本'

<?php
    query_posts('cat='.get_option('to_postc_home').'&posts_per_page='.get_option('to_posti_home').'&paged='.$paged); 
    if (have_posts()) :
?>

If I'm interpreting right, this is what you need, use the concatenation operator . to use those functions in place of plain text ex: 'this is text' versus 'this '.get_option('stuff').' text'

<?php
    query_posts('cat='.get_option('to_postc_home').'&posts_per_page='.get_option('to_posti_home').'&paged='.$paged); 
    if (have_posts()) :
?>
面如桃花 2024-12-15 02:46:27

要包含另一个文件中的 php 文件,请使用 include 函数

To include a php file from another file you use the include function

滴情不沾 2024-12-15 02:46:27

您可以在任何您想要的地方使用它

<?php
query_posts('cat=-51&posts_per_page=3&paged='.$paged); 
if (have_posts()) :
?>
hello world
<?php echo get_option('to_postc_home'); 
endif;

You can use it whereever you want

<?php
query_posts('cat=-51&posts_per_page=3&paged='.$paged); 
if (have_posts()) :
?>
hello world
<?php echo get_option('to_postc_home'); 
endif;
不弃不离 2024-12-15 02:46:27
<?php
  $a = get_option('to_postc_home');
  $b = get_option('to_posti_home');

  query_posts("cat={$a}&posts_per_page={$b}&paged={$paged}");

  if (have_posts())
?>
<?php
  $a = get_option('to_postc_home');
  $b = get_option('to_posti_home');

  query_posts("cat={$a}&posts_per_page={$b}&paged={$paged}");

  if (have_posts())
?>
温柔少女心 2024-12-15 02:46:27

这称为字符串连接,当您将文字字符串 'cat=-51&posts_per_page=3&paged=' 与变量 < 连接时,您已经在代码的第一行使用了它。代码>$分页。在 PHP 中,. 运算符可以执行此操作。

因此,在您的代码中,您可以执行以下操作:

<?php
query_posts('cat=-' . get_option('to_postc_home') . '&posts_per_page=' . get_option('to_posti_home') . '&paged='.$paged); 
?>

这将在您指定的位置注入函数调用的输出。

That is called string concatenation, and you are already using that on the first line of your code, when you concatenate the literal string 'cat=-51&posts_per_page=3&paged=' with the variable $paged. In PHP, the . operator does that.

So, in your code, you can do this:

<?php
query_posts('cat=-' . get_option('to_postc_home') . '&posts_per_page=' . get_option('to_posti_home') . '&paged='.$paged); 
?>

This will inject the output of the function calls at the places you indicated.

灼痛 2024-12-15 02:46:27
<?php
$cat = get_option('to_postc_home');
$per_page = get_option('to_posti_home');

query_posts("cat=${cat}&posts_per_page=${per_page}&paged=".$paged);
if (have_posts())
?>
<?php
$cat = get_option('to_postc_home');
$per_page = get_option('to_posti_home');

query_posts("cat=${cat}&posts_per_page=${per_page}&paged=".$paged);
if (have_posts())
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文