Related Posts code causing wrong post comments to load in Wordpress - html

I've been trying to show a related posts segment using wordpress.
My intention is to align an adsense block and 5 random posts adjacently under the post, and above the comments.
After a lot of trial and error, I was able to work something out that seemed to align well, and not affect any other part of the post layout.
Now I see that the comments aren't loading right. The post loads the wrong comments from another post randomly. I understand that I'm calling for additional posts in the related posts code, but is there anyway to load the main post comments instead of random comments from the random posts shown?
I'm just not able to figure it out. I'm new to this, and didn't want to mess with css stylesheets, so I've made all the changes in the single.php itself.
<div style="width: 575px;">
<div style="float: left;width: 250px;height: 250px;">
<250x250 adsense code>
</div>
<div style="float: right;width: 310px;height: 250px;margin: 0px;list-style: none;line-height: 1.5em;font-size: 1em;font-weight: bold;font-family: verdana, sans-serif;margin-left: 10px;padding-top: 10px;">
<?php $posts = get_posts('orderby=rand&numberposts=5'); foreach($posts as $post) { ?>
<li><span style="color: #0000FF;"><?php the_title(); ?></span>
</li>
<?php } ?>
</div>
<div style="clear: both;"></div>
</div>
Any help will be deeply appreciated. Thank you!

The reason maybe is that the $post variable stores the information of the post you are currently viewing. Your foreach loop then changes the $post variable. In fact, you may find that the comments appearing are for the last post in your 'related posts' section.
To get around this, before your foreach statement, put:
$temp = $post;
and then afterwards, reset the $post variable, by putting
$post = $temp;
If this doesn't work (put global $post; before the $temp=$post; line).
Hope this helps!

Related

Add id attribute to h1 element of wordpress post

The default rendering of the h1 element of a wordpress post looks like this:
<h1 class="entry-title"> XYZ </h1>
I would like to add the id attribute to the h1 element with the value of the post's title. The result should look as follows
<h1 id="XYZ" class="entry-title"> XYZ </h1>
I have already looked through many posts here, but couldn't find an answer to my question. Shouldn't there be a simple filter for functions.php to override the rendering of the h1 element?
Depending on where you are pulling the single post template, you can modify the output like this:
<h1 id="<?php echo get_the_title(); ?>" class="entry-title"> XYZ </h1>
Look at the single.php file and see where you are loading the template for displaying single posts. The above should run inside the WordPress loop.
Also get_the_title() pulls the title without formating, what you are looking for is probably the post slug.

CakePHP renders HTML email with <p> tags at every new line

Here is the cake code..... notice the $email_body
<?php
$this->Email->reset();
$this->Email->delivery = 'smtp';
$this->Email->smtpOptions = array(
'port'=>'465',
'timeout'=>'30',
'host' => 'ssl://smtp.gmail.com',
'username'=>'noreply#example.com',
'password'=>'a_password_you_cant_see',
);
$this->Email->sendAs = 'html';
$this->Email->template = 'default';
$this->Email->from = '"NO-REPLY" <noreply#example.com>';
$this->Email->to = "rakib#example.com";
$this->Email->subject = "test PHP html email";
$email_body = "Hello message body
<hr />
This is rakib
<br />
<table width=\"100%\" bgcolor=\"#ff0\">
<tr>
<td>
In a table
</td>
</tr>
</table>";
$this->Email->send($email_body);
?>
After sending this email out, when I view the Original mail contents via the Show Original button from GMail's drop down menu [at top-right corner of an email], here is what the HTML looks like:
<!-- Starting to render - email/html/default -->
<p> Hello message body</p>
<p> <hr /></p>
<p> This is rakib</p>
<p> <br /></p>
<p> <table width="100%" bgcolor="#ff0"></p>
<p> <tr></p>
<p> <td></p>
<p> In a table</p>
<p> </td></p>
<p> </tr></p>
<p> </table></p>
<p> </p>
<!-- Finished - email/html/default -->
<p> and </p> tags got included at EVERY new line..... why is that? Using CakePHP 1.3
When doing anything with emails, try and stick to the MVC principles. By writing your email's HTML (view) in what is likely to be a controller can make things a little messy and tends to bloat your code (e.g. composing markup in your controller).
Use templates to author the structure of your email and then use view variables ($this->set(...)) to apply specific values to it (See documentation).
I'm afraid I don't know why <p> tags are being inserted but I suspect it has got something to do with the newline character \n which is implicitly inserted each time you hit the return key.
In summary, move your markup to the template and everything should be better.
If you're directly setting the body of the email, CakePHP expects the text passed to be plain text.
By setting the type of email to HTML (Email->sendAs = 'html'), CakePHP will create a HTML version of your plain-text body by converting new-lines to <p> tags
In your case, you pass HTML as message body, but CakePHP assumes it is plain-text, therefore converts new lines to <p> tags as well
Read the documentation here: Sending a basic message
note
Although this should explain your question, please look at the answer that Sam provided as that will give you an answer on how you should send an HTML email!
I found the solution to this... I needed to create my own default.ctp file in my app folder at app/views/elements/email/html/default.ctp and put the following in the file
<?php
echo $content;
That solved my problem. Please look below for the analysis of this problem.
[P.S. I did the same thing for app/views/elements/email/text/default.ctp for text based mails to print correctly]
The problem was that, since i didn't declare any default.ctp element in my own app folder, cake was falling back to the view element in its own core lib folder at cake/libs/view/elements/email/html/default.ctp . Over there, this is what it does.
<?php
$content = explode("\n", $content);
foreach ($content as $line):
echo '<p> ' . $line . "</p>\n";
endforeach;
?>
That's where the <p> tags were coming from at every new line. Thanks to #thaJeztah's answer. In order to avoid this from happening, i needed to create my own default.ctp element in my app folder that would eventually override the default.ctp element in the core's lib folder.
That's one bad case scenario.

Wordpress Page.ly MultiEdit Plugin Not Working

I'm trying to use the Wordpress page.ly MultiEdit plugin here:
http://wordpress.org/extend/plugins/pagely-multiedit/
I can't get it to work correctly. I get an error:
Bottom, Left, Bottom, Left region(s) are not declared in the template.
Here's a screenshot.
Here is the code for the template file I created:
<?php
/*
Template Name: Home
MultiEdit: Right
*/
?>
<?php get_header(); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="content">
<?php the_content(); ?>
</div>
<div class="sidecontent">
<?php multieditDisplay(‘Right’); ?>
</div>
<?php endwhile; endif; ?>
<?php get_footer(); ?>
Am I missing something?
Thanks.
I guess I didn't look at your code close enough. You're not calling them twice in the code, you're just not calling them at all. You can still follow the instructions in my answer and it will work.
Couple things here. It looks like you're calling the same two content blocks twice in the same page template. You can't do that. Each one has to have its own unique name. If you're simply trying to get "bottom', "left", and "right" you need to change you're comment code at the top of the page template.
Use this:
/*
Template Name: Home
MultiEdit: Bottom,Left,Right
*/
Then place these where you want the content to be displayed in your page template:
<?php multieditDisplay('Bottom'); ?>
<?php multieditDisplay('Left'); ?>
<?php multieditDisplay('Right'); ?>
Before you do any of that though, click on the "Screen Options" tab in the top corner of the admin page for your home page editor. Then click on the "Custom Fields" option. Then scroll down until you see the "Custom Fields" area and expand it. Then click on the "Show/Hide Multiedit Fields" so that you can see the ones you currently have in there. You need to delete each one of them to clear them from your editor. Follow these instructions and it will work, its an amazing plugin that I've used many times with great success. Once you fix it, please accept my answer. Let me know if you have any other issues with it.
Also, you don't need to use the call inside of a loop, just drop in the one of the three calls where you need it to be in the template. In fact, I'd recommend not using it inside of a loop.

Wordpress Post Text Excerpt in Sidebar?

I am curious if there is a way to output an excerpt of the post txt into a sidebar, but only the sidebar for that post.
What I have tried is using the get_content loop in the single.php into a custom sibar that I have created inside another DIV, but it displays the post txt, Comments and images in the sidebar.
<div id="Sidebar">
<?php the_content(); ?>
</div>
Question: Is there anyway to display Post text in the sidebar?
You could do this:
<div id="sidebar">
<?php
the_content();
//display only on post page
if(is_single()){
the_excerpt();
}
</div>
Hope that works!

WP: the_content doesn't display properly

<p>
<p class="grid_9">
<?php
echo $post->post_content;
?>
</p>
</p>
But result is not what I expected:
<p> </p>
<p class="grid_9"> </p>
Post's content
Maybe someone has any ideas how to solve it?
I tried
<p>
<p class="grid_9">
<?php
echo apply_filters('the_content',$post->post_content);
?>
</p>
</p>
But the result is the same.
Nested paragraph tags don't work properly because the closing </p> is optional, you should use a div, or other container instead of your outside
The reason might be in the external plugins you have using. Please disable all plugins and try it again. Or make search in the whole project by phrase 'the_content' (including quotes) and try to comment each of them to find the reason.
I saw often in plugins bad usage of basic php/wordpress practices. It might break expected result. For example, I had the same issue in my project on PHP 7.0, because bad written filter in external plugin does not return anything.
add_filter('the_content', 'lbe_myplugin_lightbox');
I disabled it, then fixed the function and everything works fine now. Hope, it helps a bit.
Good luck!
PS It would be nice to know your PHP version as well.