Wordpress Comment Loop on blog page - php

i would like to add a comment form and display the latest comment for each blog post on the main blog loop so i have used this code and added it in the main blog loop after the content.
global $withcomments;
$withcomments = 1;
comments_template( '', true );
but it displays only on the first blog post and not on the other ones.
I would like the form to be displayed on all the blog posts in the loop and only display the latest comment.
To be more exact as seen in the screenshot below, on the first post the comment textarea appears with the button and on the second post the textarea does not appear with the buttons despite the fact that the code was added in the loop.
Comment form not showing properly - screenshot

You want to use the get_comments() function.
In get_comments, you can set order parameter as 'desc'. so you get result in descending order.
<?php foreach (get_comments() as $comment): ?>
<!--Your Contents-->
<?php endforeach; ?>

Related

Wordpress single.php another get_post function destory the first function

I am trying to deal with two get_post functions, in single.php the first get_post function is the post from the wordpress, But after that I called get_post function to other post also to use both of them in the same page but after I call the first get_post ( the main post ) I get the only second data and cant reach the first data.
My code called to the second function ( The first is from wordpress post):
$main_post = get_field('main_post');
$main_p = get_post($main_post->ID);
Then I am trying to use the variable $post OR the_title() OR any other functions to get the first post and it always returning the info of the $main_p post
for example
get_the_title( get_post()->ID )
returns the $main_p post id and not the main post of the single.php
any soulutions ?
I may be wrong, but it seems to me that you are trying to post a different post format with normal post format?
I, myself use get_post_format() so it can be styled differently or have different options.
in single.php I use
<!-- checking if there are any blogposts to be shown using have_posts check which is a wordpress function-->
<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?> <!-- the correct syntax for while loop in wordpress to show all the blogposts -->
<?php get_template_part('content', get_post_format()); ?>
<?php endwhile; ?>
<?php else : //else stament if there aren't any posts (put inside if before endif)?>
<p><?php __('No Posts Found'); ?></p>
<?php endif; ?> <!-- stop checking for blog posts-->
</div><!-- /.blog-main -->
Inside functions.php I activated the post-formats inside wp_theme_setup() function
add_theme_support('post-formats', array('aside', 'gallery'));
In this case i activated the gallery and aside posts
That way I have 2 different post types on one page
like this
image from my theme blog page
Here is also a video tutorial on post formats from Traversy media
https://www.youtube.com/watch?v=CRa7eiqyiCM&list=PLc5p9nvpdwBlrNU0hr1f0kXPRkh0aGo1Q&index=7
The key reason why your post values are being overwritten, the additional get_post() declarations are overriding the default query. Now, the code in your pastebin is a pretty massive dog's breakfast, so a direct solution is a rather large undertaking (e.g. the indentation is all over the place, the code snippets are less than ideal regarding their readability, etc...). However, I can point you in the right direction for the solution.
When I pull content from another page on my WordPress sites, I avoid using get_post() in favour of declaring a fresh new WP_Query() (that's just my preference), following it up with a wp_reset_postdata() declaration.
Here's an example of multiple queries on a single template in the WordPress codex:
https://codex.wordpress.org/Class_Reference/WP_Query#Multiple_Loops
The key here is the wp_reset_postdata(). I'd recommend looking into it's purpose. It'll save you a lot of grief:
https://codex.wordpress.org/Function_Reference/wp_reset_postdata

Add page numbers to post on wordpress theme

I am building custom theme. If I have got long post (like review, tutorial) I would like to cut this post for 5 parts. I would love to make navigation at the bottom of post. So, I have added this code to post, but it does not show navigation:
<!--nextpage-->
So, I have added this in the place of single.php, but it also does not show this:
<?php wp_link_pages(); ?>
How to add this element?
OK, I got it. It is very simple...
<?php wp_link_pages(); ?>
Shoud be in the loop...

How to add multiple post in single wordpress page as per post id

I can try this code in page text but not working
<?php
$post_id = '138';
echo get_post_field('post_content', $post_id);
?>
show a code only not display content,
provide solutions in which file I can add this code.
wordpress provide insert_pages plugin and in this plugin in page add post or another page title, content etc

How can I use a [Shortcode] in my custom field wordpress

I have a custom wordpress post.php, all posts within a specific category will use this template. In nearly every post there will be scrolling testimonials, to handle this I am using a layer slider.
Within the page I have a custom field of testimonial that is filled with something like [layreslider id="2"]
In my php file I have:
<div class="trips-testimonials">
<?php do_shortcode(get_post_meta($post->ID, 'testimonial', true)); ?>
</div>
This seems to do nothing. If I add echo to the PHP:
<?php echo do_shortcode(get_post_meta($post-ID, 'testimonial', true)); ?>
I get the output of [layreslider id="2"]
Here is a sample page, the blue box under the photo is where the slider should show up.
http://www.ct-social.com/ctsdev/aff/capri/
Thank you very much for your help.
looks like you miss the > from post Id so it should read
<?php do_shortcode(get_post_meta($post->ID, 'testimonial', true)); ?>
Also you may need to call global $post; if outside the loop.

Wordpress display page contents and a loop?

I have a custom loop in a page template to display posts by category by give category and tag. It works, but above the loop I need to show the content of the page itself, i.e. the content that's been entered into the Wordpress Dashboard normally.
What do I add to my template to display this content?
I have tried:
$id = $post->ID;
get_page($id);
// then my custom loop
Which does get the current page ID, but no content.
In WordPress, calling
<?php the_content(); ?>
will pull in the content from the WYSIWYG editor on the page that is using that template as long as it is inside the loop.
The most basic example of this might look like this:
<?php while (have_posts()) : the_post();/* Start loop */ ?>
<?php the_content(); ?>
<?php endwhile; /* End loop */ ?>
<!-- Enter your custom loop for displaying posts by category down here -->
More info here: http://codex.wordpress.org/Function_Reference/the_content
In my case, with a modern theme and using Gutenberg blocks, I had to apply filters to the content before the posts loop, as mentioned in this thread here: Proper way to get page content
Following one of the examples, a simple working solution would be:
<?php
$id = 669; // page ID that has been assigned for posts
$post = get_post($id);
$content = apply_filters('the_content', $post->post_content);
echo $content;
?>
<!-- Enter your custom loop for displaying posts by category down here -->
To display the content of the page, using the the_content() function.
Added your custom loop after this function call.

Categories