Displaying threaded comments without wp_list_comments() - php

I am creating a custom comment layout, therefor I'm not using wp_list_comments(). My problem is that I can't seem to figure out for to effectively display replied comments in one another (threaded).
So far I have it so it only show the top most layer of comments.
<ol class="commentlist">
<?php foreach ($comments as $comment) : ?>
<?php if($comment->comment_parent == 0): ?>
<li class="comment">
<div class="main">
<div class="name">
<?php if(get_comment_author_url()): ?>
<?php comment_author(); ?>
<?php else: ?>
<?php comment_author(); ?>
<?php endif; ?>
</div>
<div class="text"><?php comment_text(); ?></div>
</div>
<div class="info">
<?= get_avatar($comment, $size = '90'); ?>
<div class="month"><?= comment_date('M'); ?></div>
<div class="day"><?= comment_date('dS'); ?></div>
<div class="year"><?= comment_date('Y'); ?></div>
</div>
<div class="clear"></div>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ol>
I know want to display the comments that are replies within this comment.

I have seen your code, i think there is restriction for the child comments. you had condition where parent comments will displaying only.
You need to create one more class OR CSS for child comments and for that also need to run loop inside this FOREACH loop and displaying only that childes where Parent would be the
$comment->comment_parent
.
I think i am right if i get your prob.
Thanks.

I have figured out how to achieve threaded comments while using a custom layout.
Inside of your functions.php file place the following code:
<?php function comment_layout($comment, $args, $depth) {
$GLOBALS['comment'] = $comment; ?>
// Your custom layout here
<?php } ?>
Now, inside your comments.php file simply places the following where you would like the comments to appear.
<?php wp_list_comments( array( 'callback' => 'comment_layout' ) ); ?>
What will this do is, loop through all the comments (in the correct order) and call your custom layout to display the comment.

Related

HTML only print once in Wordpress Loop

I have a code chunk that is inside the_content(); I'm using acf repeater as well. So when I post a blog, I'll either use the_content(); or the acf field. I have h2 tag ( latest articles ) that I only want printed one time, but it's printing everytime I make a post.
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<div class="container">
<div class="row">
<div class="col-md-4 sidebar-r">
<?php echo the_content(); ?>
</div><!-- end sidebar-r -->
<?php
$i = $wp_query->post_count;
if($i <=1) {
echo '<h2 class="link-title">
<?php the_sub_field('link_title'); ?>,
</h2>';
}else{
echo '';
}
?>
<div class="col-md-8 links-wrap">
<?php if(have_rows('daily_links')): ?>
<?php while(have_rows('daily_links')): the_row(); ?>
<a href="<?php the_sub_field('link_url'); ?>" target="_blank">
<h2 class="link-title">
<?php the_sub_field('link_title'); ?>,
</h2>
<h3 class="link-source">
<?php the_sub_field('link_source'); ?>
</h3>
</a>
<?php endwhile; ?>
<?php endif; ?>
</div><!-- end links wrap -->
</div><!-- end row -->
</div><!-- end container -->
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
You'll see I tried using php to count the posts and if more than one post, don't print the tag, but couldn't figure out the exact logic and syntax.
I am honestly struggling a bit to understand exactly what you are trying to do and since I do not even have the posts and other key pieces of information so that I can properly replicate your issue so that I can help you better, this is a little bit challenging. That being said, looking into some ideas I came across another stackoverflow question/answer that might be relevant for you in catching the first post and does something to it. The answer to the referenced question instance was this:
<?php if (have_posts()) : $postCount = 1; while (have_posts()) : $postCount++; ?>
<?php if($postCount == 2) { ?>
// SOMETHING TO DO WITH FIRST POST
<?php } else { ?>
// SOMETHING TO DO WITH ALL OTHER POSTS
<?php } ?>
This was suggested by user Bora in this answer from 2013.
Let me know if that helped!

How is this plugin creating multiple pages within the same plugin?

I'm extending an already existing plugin called Anspress. I'm new to wordpress/web dev and I can't figure out how the author makes different pages show up without creating different page templates. These images show how there is only 1 page template in the dashboard using a single shortcode. Within this "page" created by the plugin, there are links to load other sections of the plugin, such as user profiles, individual questions and their responses, and the main question list.
In a nutshell, his code layout is as follows:
main.php
{
require_once 'FileA.php'
require_once 'FileB.php'
require_once 'FileC.php'
require_once 'FileD.php'
/// Etc...
}
None of the files above are ask.php, which is the PHP file used to hold the HTML for the ask-question page. Yet he can still make this ask.php file somehow generate the page when a user clicks on the "Ask a question" button. How does this work? Below is the code for ask.php
<?php
// Some comment about the HTML code below
?>
<div id="ap-ask-page" class="clearfix">
<?php if (ap_user_can_ask()): ?>
<div id="answer-form-c">
<div class="ap-avatar ap-pull-left">
<a href="<?php echo ap_user_link(get_current_user_id()); ?>"<?php ap_hover_card_attributes(get_current_user_id()); ?>>
<?php echo get_avatar(get_current_user_id(), ap_opt('avatar_size_qquestion')); ?>
</a>
</div>
<div class="ap-a-cells clearfix">
<div class="ap-form-head">
<?php _e('Toggle fullscreen', 'ap'); ?>
<ul class="ap-form-head-tab ap-ul-inline clearfix ap-tab-nav">
<li class="active"><?php _e('Write', 'ap'); ?></li>
<?php if(ap_opt('question_help_page') != '') : ?>
<li><?php _e('How to ask', 'ap'); ?></li>
<?php endif; ?>
</ul>
</div>
<div class="ap-tab-container">
<div id="ap-form-main" class="active ap-tab-item">
<?php ap_ask_form(); ?>
</div>
<div id="ap-form-help" class="ap-tab-item">
<?php if(ap_opt('question_help_page') != ''): ?>
<?php ap_how_to_ask(); ?>
<?php endif; ?>
</div>
</div>
</div>
</div>
<?php elseif (is_user_logged_in()): ?>
<div class="ap-no-permission">
<?php _e('You don\'t have permission to ask question.', 'ap'); ?>
</div>
<?php endif; ?>
<?php ap_get_template_part('login-signup'); ?>
</div>
I can provide more info as needed.

ACF not showing after running loops

In the below code, I call fields from Advanced Custom Fields plugin and only the first two show 'home_title' and 'home_content'. After these two I run two different loops to show the latest posts in a given category. After those loops run there are 4 more fields from ACF called. 'donate_title' , 'donate_content' , 'mission_title' , 'mission_content'. Which are not showing up (not pulling any content at all).
If I move these ACF before running the loops they all show up correctly. So I imagine there is a problem with these following the loops but cannot find the reason.
<div class="main-site">
<div class="home-title-1">
<?php the_field('home_title'); ?>
</div>
<div class="home-content-1">
<?php the_field('home_content'); ?>
</div>
<div class="home-boxes-cont">
<div class="box-left">
<?php
query_posts('cat=4&posts_per_page=1');
while (have_posts()) : the_post(); ?>
<div class="bl-img">
</div>
<div class="bl-title">
<?php the_title(); ?>
</div>
<div class="bl-content">
<?php the_excerpt(); ?>
</div>
<?php endwhile; ?>
</div>
<div class="box-middle">
<?php
query_posts('cat=5&posts_per_page=1');
while (have_posts()) : the_post(); ?>
<div class="bm-img">
</div>
<div class="bm-title">
<?php the_title(); ?>
</div>
<div class="bm-content">
<?php the_excerpt(); ?>
</div>
<?php endwhile; ?>
</div>
<div class="box-right">
<div class="br-img">
</div>
<div class="br-title">
<?php the_field('donate_title'); ?>
</div>
<div class="br-content">
<?php the_field('donate_content'); ?>
</div>
</div>
</div>
<div class="mission-title">
<?php the_field('mission_title'); ?>
</div>
<div class="mission-content">
<?php the_field("mission_content"); ?>
</div>
In order to get custom field data from the original post after altering the global post data with your query_posts() calls, you need to reset your post data with the wp_reset_query() function. Place this function after each loop -
<?php while (have_posts()) : the_post(); ?>
...
<?php endwhile; wp_reset_query(); ?>
You are altering the global wp_query variable. When you do that and the the result is not is_single() then you cannot pull any ACF settings any longer.
Either reset the wp_query to its original setting for the page or store the vars in an array before you make any wp_query changes, and retrieve them as needed later in the code.
While I agree with Scriptonomy, you should really just use get_posts(). This is exactly what this function is designed to do... custom loops outside the main loop. You should rarely ever need to modify the global wp_query variable.
If you still want to use the_permalink() and the_title() without passing a post id, then scroll down the page to the section labeled "Access all post data" and you'll see how to use setup_postdata() to make it easier.

Customize search results by category

I have different categories where the post displayed are characterized by different elements, for this reason when I use the search in wordpress the results are not shown very well.
For this reason I would like to change the appearance of the post shown in the search results based on its category.
Example:
My search.php template look like this:
<?php get_header(); ?>
<div id="content">
<div class="padder">
<?php do_action( 'bp_before_blog_search' ); ?>
<div class="page" id="blog-search" role="main">
<?php if (have_posts()) : ?>
<?php bp_dtheme_content_nav( 'nav-above' ); ?>
<?php while (have_posts()) : the_post(); ?>
<?php do_action( 'bp_before_blog_post' ); ?>
<div class="blog-post">
// Here is displayed the blog post style and features
</div><!--blog-post-->
<div class="clear"> </div>
<?php do_action( 'bp_after_blog_post' ); ?>
<?php endwhile; ?>
<?php bp_dtheme_content_nav( 'nav-below' ); ?>
<?php else : ?>
<h2 class="center"><?php _e( 'No posts found. Try a different search?', 'OneCommunity' ); ?></h2>
<?php endif; ?>
<div style="display:inline">
<center><?php wp_pagenavi(); ?></center>
</div>
</div>
<?php do_action( 'bp_after_blog_search' ); ?>
</div><!-- .padder -->
</div><!-- #content -->
<div id="sidebar">
<?php if (function_exists('dynamic_sidebar') && dynamic_sidebar('sidebar-blog')) : ?><?php endif; ?>
<?php if (function_exists('dynamic_sidebar') && dynamic_sidebar('sidebar-ad-blog')) : ?><?php endif; ?>
</div><!--sidebar ends-->
<?php get_footer(); ?>
I tried to achieve the result of displaying based on the post category a different "template" for the post but without success.
I asked the same question on wordpress.stackexchange.com but without success, and there I pointed out a non working solution, hope it can help you in developing the code I need.
https://wordpress.stackexchange.com/questions/141856/different-layout-on-search-page-depending-on-category-post
Thank you very much for any help you will give, and excuse me for my english.
If I understand your question correctly, you could simply use the post_class() function:
<div <?php post_class( 'blog-post' );?> >
// Here is displayed the blog post style and features
</div><!--blog-post-->
This will render like:
<div class="post-3654 post type-post status-publish format-standard hentry category-buildings blog-post">
// Here is displayed the blog post style and features
</div><!--blog-post-->
for posts in the buildings category.
Then your CSS will take care of the rest:
.post.category-buildings {
border: 3px solid red;
}
But I think your setup/idea is only suitable for single category posts.
Ref: From the Codex:
The post_class may include one or more of the following values for the
class attribute, dependent upon the pageview.
.post-id
.post
.attachment
.sticky
.hentry (hAtom microformat pages)
.category-ID
.category-name
.tag-name
.format-name

How I create my own comments.php in Wordpress? Any startingpoint / template to begin?

I have been trying to invert how the wordpress theme I'm using displays comments...without success...
The main thing I need to do is to invert how the comments are rendered:
The name of the person should be AFTER the comment message. I can't get that work. I found this code worked very well, but the problem is that when I implement it, comments show in all the pages while I only want them to be displayed in one of the pages, for this purporse I used Comments Disable Master plugin for wordpess, that works OK. when I use this code, seems that comment disable master is not working anymore and comments display in all the pages...
<div class="clear"></div>
<?php
// Do not delete these lines
if (!empty($_SERVER['SCRIPT_FILENAME']) && 'comments.php' == basename($_SERVER['SCRIPT_FILENAME']))
die ('Please do not load this page directly. Thanks!');
if ( post_password_required() ) { ?>
<div class="help">
<p class="nocomments">This post is password protected. Enter the password to view comments.</p>
</div>
<?php
return;
}
?>
<!-- You can start editing here. -->
<?php if ( have_comments() ) : ?>
<h5 id="comments" class="h5"><?php comments_number('<span>No</span> Responses', '<span>One</span> Response', '<span>%</span> Responses' );?> to “<?php the_title(); ?>”</h5>
<nav id="comment-nav">
<ul class="clearfix">
<li><?php previous_comments_link() ?></li>
<li><?php next_comments_link() ?></li>
</ul>
</nav>
<ol class="commentlist">
<?php wp_list_comments('callback=qs_comments'); ?>
</ol>
<nav id="comment-nav">
<ul class="clearfix">
<li><?php previous_comments_link() ?></li>
<li><?php next_comments_link() ?></li>
</ul>
</nav>
<?php else : // this is displayed if there are no comments so far ?>
<?php if ( comments_open() ) : ?>
<!-- If comments are open, but there are no comments. -->
<?php else : // comments are closed ?>
<!-- If comments are closed. -->
<!--<p class="nocomments">Comments are closed.</p>-->
<?php endif; ?>
<?php endif; ?>
<?php if ( comments_open() ) : ?>
<?php comment_form(); ?>
<?php endif; // if you delete this the sky will fall on your head ?>
I need to invert the output and I don't know how to begin with this. The previous code has a problem because seems to lack of something that makes it work with the disable comment master plugin... Any hint??? Maybe I'm missing a line of code that makes it work...
Where can I found a template to build my comment.php ? Thanks for the help.
With a little edit in the comments.php template by yourself you will begin to understand it.
Also do take a reference to this tutorial : http://code.tutsplus.com/articles/unraveling-the-secrets-of-wordpress-commentsphp-file--net-28
About the template you can take a reference to comment.php of any themes like twenty twelve, thirteen. Take many reference then pick the best one among them and edit for yourself.
Just check the post id on which you want to display the comments.
if ( is_single('your-post-id') ) {
// Your comment code here
}
Doing this will display comments only on the page that you have mentioned in 'your-post-id'

Categories