为什么这不会在PHP中回响?

时间:2021-05-04 16:06:26

I'm curious why this won't echo the HTML; I've perused the other questions in SO having to do with echo and print.

我很好奇为什么这不会回应HTML;我已经仔细研究了SO中与echo和print有关的其他问题。

It must be the PHP while loop in the string, but I've escaped the double quotes. There is something more complex happening, namely the error "Object of class WP-Query could not be converted to string."

它必须是字符串中的PHP while循环,但我已经转义了双引号。有一些更复杂的事情发生,即错误“类WP-Query的对象无法转换为字符串”。

Am I being too simplistic with trying to echo the PHP?

我试图回应PHP太简单了吗?

Edited for some formatting (which didn't want to work at first).

编辑了一些格式(最初不想工作)。

And, what I need to do is echo the HTML that is generated by the query loop, because that's the link to the wordpress post.

而且,我需要做的是回显查询循环生成的HTML,因为这是wordpress帖子的链接。

<?php $d=date("D"); if (in_array($d, array('Thu','Fri','Sat','Sun')))

echo "The latest post for Thursday, Friday, Saturday, Sunday:

<?php $my_query = new WP_Query('category_name=posts&showposts=1'); ?>

<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>

<a href=\"<?php the_permalink() ?>\" rel=\"bookmark\"><?php the_title(); ?></a>.

<?php endwhile; ?>" ;?>

'tanks, Mark

7 个解决方案

#1


You cannot use PHP like that. Try this:

你不能像这样使用PHP。试试这个:

$d=date("D");
if (in_array($d, array('Thu','Fri','Sat','Sun'))) {
    echo "The latest post for Thursday, Friday, Saturday, Sunday: ";
    $my_query = new WP_Query('category_name=posts&showposts=1');
    while ($my_query->have_posts()) {
        $my_query->the_post();
        echo "<a href=\";
        the_permalink();
        echo "\" rel=\"bookmark\">";
        the_title();
        echo "</a>";
    }
}

Or if you prefer this syntax:

或者,如果您更喜欢这种语法:

<?php
    $d=date("D");
    if (in_array($d, array('Thu','Fri','Sat','Sun'))):
?>
The latest post for Thursday, Friday, Saturday, Sunday:
<?php
        $my_query = new WP_Query('category_name=posts&showposts=1');
        while ($my_query->have_posts()):
            $my_query->the_post();
?>
<a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a>
<?php
        endwhile;
    endif;
?>

#2


You don't close your string - it should be:

你不要关闭你的字符串 - 它应该是:

<?php
$d=date("D");

if (in_array($d, array('Thu','Fri','Sat','Sun')))
    echo "The latest post for Thursday, Friday, Saturday, Sunday: ";

$my_query = new WP_Query('category_name=posts&showposts=1');

while ($my_query->have_posts())
{
    $my_query->the_post();
    ?><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>.<?php
}
?>

Your formatting isn't very good btw. Newlines aren't expensive!

你的格式不是很好btw。换行并不贵!

#3


the php interpreter stops interpreting at ?> and considers everything that comes later as normal input. thus all latter code is parsed again and not part of the echo.

php解释器停止解释?>并将稍后作为正常输入的所有内容都考虑在内。因此,后面的所有代码都会被再次解析,而不是回声的一部分。

#4


You open the php scripting twice: <?php should only be there once.

你打开php脚本两次:

<?php ... "The latest post for Thursday, Friday, Saturday, Sunday: <?php

#5


Why do you never exit the string?

为什么你永远不退出字符串?

<?php $my_query = new WP_Query('category_name=posts&showposts=1'); ?>

The above code is stuck right in the middle of your string.

上面的代码卡在你的字符串中间。

#6


It prints

The latest post for Thursday, Friday, Saturday, Sunday: <?php  = new WP_Query('category_name=posts&showposts=1'); ?><?php while (()) : (); ?><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>. <?php endwhile; ?>

which is what it should. PHP only parses in one pass.

它应该是什么。 PHP只在一次传递中解析。

#7


<?php
$d=date("D");
if(in_array($d, array('Thu','Fri','Sat','Sun')))
  echo 'The latest post for Thursday, Friday, Saturday, Sunday:'.
       ' <?php $my_query = new WP_Query(\'category_name=posts&showposts=1\'); ?'.
       '><?php while ($my_query->have_posts()) : $my_query->the_post(); ?'.
       '><a href="<?php the_permalink() ?'.
       '>" rel="bookmark"><?php the_title(); ?'.
       '></a>. <?php endwhile; ?'.'>';
?>

#1


You cannot use PHP like that. Try this:

你不能像这样使用PHP。试试这个:

$d=date("D");
if (in_array($d, array('Thu','Fri','Sat','Sun'))) {
    echo "The latest post for Thursday, Friday, Saturday, Sunday: ";
    $my_query = new WP_Query('category_name=posts&showposts=1');
    while ($my_query->have_posts()) {
        $my_query->the_post();
        echo "<a href=\";
        the_permalink();
        echo "\" rel=\"bookmark\">";
        the_title();
        echo "</a>";
    }
}

Or if you prefer this syntax:

或者,如果您更喜欢这种语法:

<?php
    $d=date("D");
    if (in_array($d, array('Thu','Fri','Sat','Sun'))):
?>
The latest post for Thursday, Friday, Saturday, Sunday:
<?php
        $my_query = new WP_Query('category_name=posts&showposts=1');
        while ($my_query->have_posts()):
            $my_query->the_post();
?>
<a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a>
<?php
        endwhile;
    endif;
?>

#2


You don't close your string - it should be:

你不要关闭你的字符串 - 它应该是:

<?php
$d=date("D");

if (in_array($d, array('Thu','Fri','Sat','Sun')))
    echo "The latest post for Thursday, Friday, Saturday, Sunday: ";

$my_query = new WP_Query('category_name=posts&showposts=1');

while ($my_query->have_posts())
{
    $my_query->the_post();
    ?><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>.<?php
}
?>

Your formatting isn't very good btw. Newlines aren't expensive!

你的格式不是很好btw。换行并不贵!

#3


the php interpreter stops interpreting at ?> and considers everything that comes later as normal input. thus all latter code is parsed again and not part of the echo.

php解释器停止解释?>并将稍后作为正常输入的所有内容都考虑在内。因此,后面的所有代码都会被再次解析,而不是回声的一部分。

#4


You open the php scripting twice: <?php should only be there once.

你打开php脚本两次:

<?php ... "The latest post for Thursday, Friday, Saturday, Sunday: <?php

#5


Why do you never exit the string?

为什么你永远不退出字符串?

<?php $my_query = new WP_Query('category_name=posts&showposts=1'); ?>

The above code is stuck right in the middle of your string.

上面的代码卡在你的字符串中间。

#6


It prints

The latest post for Thursday, Friday, Saturday, Sunday: <?php  = new WP_Query('category_name=posts&showposts=1'); ?><?php while (()) : (); ?><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>. <?php endwhile; ?>

which is what it should. PHP only parses in one pass.

它应该是什么。 PHP只在一次传递中解析。

#7


<?php
$d=date("D");
if(in_array($d, array('Thu','Fri','Sat','Sun')))
  echo 'The latest post for Thursday, Friday, Saturday, Sunday:'.
       ' <?php $my_query = new WP_Query(\'category_name=posts&showposts=1\'); ?'.
       '><?php while ($my_query->have_posts()) : $my_query->the_post(); ?'.
       '><a href="<?php the_permalink() ?'.
       '>" rel="bookmark"><?php the_title(); ?'.
       '></a>. <?php endwhile; ?'.'>';
?>