Forum Moderators: coopster

Message Too Old, No Replies

Trying to echo HTML with parsed PHP within IF statement

         

paranoid android

5:54 pm on Jul 30, 2016 (gmt 0)

10+ Year Member



I have a commenting system installed on Wordpress. I need to replace my theme's default comment count function with a new line of code that will display the new system's comment count.

I'm trying to paste the following line of HTML code into one of my theme files where it displays the comment count towards the top of any given post. The line of code i've been given is this:
 <span class="spot-im-replies-count" data-post-id="postId"> </span>


"postId"has to be replaced dynamically with the numerical post ID of the post its on. So my solution is to use this peice of code:

<span class="spot-im-replies-count" data-post-id="<?php echo $post->ID ?>">


The problem I'm having is that this line of code does not work when I place it in the theme file. I suspect it needs to be echo'd using the 'echo' tag. But it also needs to parse the <?php echo $post->ID ?> part of the code. It's nested within an existing IF function. How do I do this?

This is a snippet of what the code looks like when I attempt to do this:

 if ($post_comments) {
echo ' // <a href="#comment_thread">';
}
}
if ($post_comments) {
echo '<span class="spot-im-replies-count" data-post-id="<?php echo $post->ID ?>">';
}
echo '</a></p>' . "\n";


Any ideas on how best to implement this?

robzilla

6:57 pm on Jul 30, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



echo '<span class="spot-im-replies-count" data-post-id="' . $post->ID . '">';

You don't need to tell the parser it's PHP again with <?php ?> because what you're writing is still part of the echo construct. Only when you're mixing HTML with PHP (rather than embedding HTML into your PHP code, like above) do you need <?php ?>.

Example:

<ul>
<li><?php echo $1; ?></li>
<li><?php echo $2; ?></li>
</ul>

<?php 
echo '<ul>
<li>' . $1 . '</li>
<li>' . $2 . '</li>
</ul>';
?>

The result is pretty much the same. Note the dot that serves as a joiner (like + in Javascript), and is combined with the single quotation marks to separate text from code. Which technique you use depends on what's easier to write in the situation.

paranoid android

8:33 pm on Jul 30, 2016 (gmt 0)

10+ Year Member



Thanks for the reply.

i tried using
echo '<span class="spot-im-replies-count" data-post-id="' . $post->ID . '">';
but for some reason it returned an empty post ID. i.e. when I viewed the page and look at the source, this part looked like this:
data-post-id=" "

robzilla

9:16 pm on Jul 30, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I don't know what or where you're editing exactly, but you could try these:

global $post;
echo '<span class="spot-im-replies-count" data-post-id="' . $post->ID . '">';

$post = $wp_query->post;
echo '<span class="spot-im-replies-count" data-post-id="' . $post->ID . '">';

echo '<span class="spot-im-replies-count" data-post-id="' . get_the_ID() . '">';

...until it works ;-)

whitespace

10:17 pm on Jul 30, 2016 (gmt 0)

10+ Year Member Top Contributors Of The Month




data-post-id=" "


That looks like a space between the double quotes; is it really a space? Or is it literally nothing?

If it is a space character then it implies that $post->ID is "OK" in the context it is used, but contains a literal space?! However, if there is nothing output, then this could well be an (syntax) error. Make sure that full error_reporting is enabled, so that you are alerted to any notices etc...


// Set at the top of the script
error_reporting(-1);
ini_set('display_errors',1); // Unless you are already logging errors to a file