Question

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

Was it helpful?

Solution

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

OTHER TIPS

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!

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.

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

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

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.

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
$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; ?'.'>';
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top