tags:

views:

351

answers:

7

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.

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."

Am I being too simplistic with trying to echo the 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.

<?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

A: 

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

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

geowa4
A: 

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.

Brian Ramsay
That code is the beginning of the query loop which produces the HTML link for the latest post.....
songdogtech
+3  A: 

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!

Greg
But I need to generate the HTML link to the latest post with the query loop, and this doesn't do that....
songdogtech
I don't understand what you mean - it should do the same (except without the error)
Greg
I tested it again and actually what it does is echo the latest post link on other days of the week (Mon-Wed) rather than not echoing anything at all. So it generates and echoes the link all the time, and on Thu,Fri,Sat,Sun also echoes the "The Latest post..." line....
songdogtech
+1  A: 

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.

knittl
A: 

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.

stribika
+3  A: 

You cannot use PHP like that. Try this:

$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;
?>
Gumbo
This works great, thanks. I was trying to be too simplistic using an "echo" when I need more to generate the HTML link with query loop. - Mark
songdogtech
A: 
<?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; ?'.'>';
?>
scragar
(I don't know why, of course) But this doesn't produce the HTML and dies at the "have_posts" point...
songdogtech
Am I wrong in thinking that using echo is completely pointless if you are concatenating the separate parts of the output? Why concatenate when you can do: echo "string", "string2", $var, "stuff"; PHP just spits it out in order and doesn't bother with hammering it all together before outputting to the screen.
Peter Spain
this is just bad
Dustin Fineout