Why my wordpress loop modifies my html code? - php

for the first time I'm facing a problem that I can't explain. I'm creating a simple wordpress loop for a gallery but, I don't know why, the loop change my html code inside the loop.
At first, this is what I wrote :
<?php query_posts('posts_per_page=9');
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
?>
<?php
$post_image_id = get_post_thumbnail_id($post_to_use->ID);
if ($post_image_id) {
$thumbnail = wp_get_attachment_image_src( $post_image_id, 'large', false);
if ($thumbnail)
(string)$thumbnail = $thumbnail[0];
} else {
$thumbnail = './assets/no-picture.jpg';
}
?>
<article class="col-lg-4 col-sm-6">
<a href="#Modal1" data-toggle="modal" class="gallery-box">
<img src="<?php echo $thumbnail; ?>" class="img-responsive" alt="Image">
<div class="gallery-box-caption">
<div class="gallery-box-content">
<div>
<h2><?php the_title(); ?></h2>
<h6 class='wide-space'><?php the_category(); ?></h6>
<i class="icon-lg ion-ios-play-outline"></i>
</div>
</div>
</div>
</a>
</article>
<?php endwhile; ?>
<?php endif; ?>
But when it's launched online, the code inside the loop become this :
<article class="col-lg-4 col-sm-6">
<a href="#Modal1" data-toggle="modal" class="gallery-box">
<img src="https://labelleequipeproductions.com/wp-content/uploads/2020/08/lessines-terre-de-developpement.jpg" class="img-responsive" alt="Image">
</a>
<div class="gallery-box-caption">
<div class="gallery-box-content">
<a href="#Modal1" data-toggle="modal" class="gallery-box">
</a>
<div>
<a href="#Modal1" data-toggle="modal" class="gallery-box">
<h2>Lessines, Terre de développement</h2>
</a>
<h6 class="wide-space">
<ul class="post-categories">
<li>
Corporate
</li>
</ul>
</h6>
<i class="icon-lg ion-ios-play-outline"></i>
</div>
</div>
</div>
</article>
Are there someone who could help me on this ? I never seen this before and I often use this kind of loop.

Related

Page_navi plugin create but its not working

page / 2 / is going but the homepage always appears help
<div class="row">
<div class="col-md-8">
<?php $query = new WP_Query('post_type=homee');?>
<?php if ( $query->have_posts() ) : while ($query->have_posts() ) : $query->the_post();?>
<div class="card mt-4">
<a href="<?php the_field('homecard-link');?>" class='card-header-h'><h3
class="card-title m-2">
<div class="card-footer text-muted ">
<i class="fas fa-table text-warning"></i>
Post Tarihi <?php the_field('homepost-history');?>
</div>
<a href="<?php the_field('homecard-link');?>"><img src="<?php
the_field('homeimage');?>" class="card-img-top img-thumbnail">
<div class="card-body">
<p class="card-text"><?php the_field('homecard-text');?></p>
<a class="more" href="<?php the_field('homecard-link');?>"><button
type="button" class="btn btn-dark more"> Devamını Oku
</div>
<?php endwhile; ?>
<?php else : ?>
<p><?php esc_html_e( 'Üzgünüz, Sayfa Bulunamadı' ); ?></p>
<?php endif;
wp_pagenavi(array( 'query' => $query )); `this problem`
wp_reset_query(); `this problem`
;?>
Please try and replace wp query
<?php $query = new WP_Query('post_type=homee', 'paged' => get_query_var( 'paged' ));?>

How to add posts in the home page same as blog page with me selecting static page for each?

I'm new to WordPress, and I've created a custom template, I'm facing a problem when I try to fetch the posts to the front page (homepage).
In (dashboard->Reading) I set my homepage displays as a Static page and selected Home page as (home) and Post page as (posts).
In the index.php the code that's showing the posts in Posts page is :
<section class="recent-posts" id="recent-posts">
<div class="container">
<div class="title text-center">
<h1 class="title-blue">posts</h1>
</div>
<div class="row">
<?php
while(have_posts()) {
the_post();
?>
<div class="col-lg-6">
<div class="single-rpost d-sm-flex align-items-center" data-aos="fade-right"
data-aos-duration="800">
<div class="post-thumb">
<img class="img-fluid" src="<?php echo get_the_post_thumbnail_url(get_the_ID()); ?>" alt="post-1">
</div>
<div class="post-content text-sm-right">
<time datetime="<?php the_time('l, jS/n/Y'); ?>">
<?php the_time('l, jS/n/Y'); ?>
</time>
<h3>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h3>
<p class="post-excerpt">
<?php echo wp_trim_words(get_the_excerpt(), 10); ?>
</p>
<p>
<!-- <a href="#"> -->
<?php echo get_the_category_list(' ') ?>
<!-- </a> -->
</p>
<a class="post-btn" href="<?php the_permalink(); ?>">
<i class="fa fa-arrow-left"></i>
</a>
</div>
</div>
</div>
<?php
}
wp_reset_query();
?>
</div>
<!-- Posts Navigation -->
</div>
</section>
<!-- Paginate through pages -->
<div class="pagination">
<?php echo paginate_links(); ?>
</div>
It works fine in posts page but when I copy the same code in page-home.php file it doesn't work and fetches no posts at all.
Sorry I'm a beginner, but I need help.
Thanks.
Try this
<?php
global $paged, $wp_query, $wp;
$args = wp_parse_args($wp->matched_query);
if ( !empty ( $args['paged'] ) && 0 == $paged ) {
$wp_query->set('paged', $args['paged']);
$paged = $args['paged'];
}
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('paged='.$paged.'&showposts=10&cat='.get_option('prototype_blog_cat'));
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<div class="col-lg-6">
<div class="single-rpost d-sm-flex align-items-center" data-aos="fade-right"
data-aos-duration="800">
<div class="post-thumb">
<img class="img-fluid" src="<?php echo get_the_post_thumbnail_url(get_the_ID()); ?>" alt="post-1">
</div>
<div class="post-content text-sm-right">
<time datetime="<?php the_time('l, jS/n/Y'); ?>">
<?php the_time('l, jS/n/Y'); ?>
</time>
<h3>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h3>
<p class="post-excerpt">
<?php echo wp_trim_words(get_the_excerpt(), 10); ?>
</p>
<p>
<!-- <a href="#"> -->
<?php echo get_the_category_list(' ') ?>
<!-- </a> -->
</p>
<a class="post-btn" href="<?php the_permalink(); ?>">
<i class="fa fa-arrow-left"></i>
</a>
</div>
</div>
</div>
<?php endwhile; ?>
<span >
<?php echo paginate_links( array(
'prev_text' => '<span>Previous</span>',
'next_text' => '<span>Next</span>'
)); ?>
It should solve the problem.

WordPress advanced custom fields, loop though repeater

What I have
I have a repeater setup in Advanced custom fields and bootstrap slider in my code.
What I trying to achieve
I would like to add a image and text in my WordPress repeater and have it populate my slider. so that each new repeater row has its own image and text.
I have the image working, but not the text so at the moment every slide has its own image, but for some reason I have all the text displaying for each slide.
bellow is the layout of my ACF repeater
bellow is my current slider
so I have 3 images that show on the correct slides but as you can see my content is showing all not just the content related for the slide
My code
<?php
$sliderImages = get_field('section_8_slider');
$count = 0;
$section = "section_8_";
$order = array();
?>
<div class="col-lg-8 mb-3">
<div class="carousel-inner">
<?php foreach ($sliderImages as $imgNumber => $image): ?>
<div class="carousel-item<?php if ($imgNumber === 0) : ?> active<?php endif ?>">
<img src="<?= $image['image']['url'] ?>" alt="<?= $image['image']['alt'] ?>">
<?php if (have_rows($section . 'slider')):
while (have_rows($section . 'slider')):
the_row(); ?>
<?php
foreach ($sliderImages as $i => $row)
if ($sliderImages ): ?>
<div class="carousel-text-block">
<h4 class="mb-1"> <?php echo $row['content_title']; ?></h4>
<p class="small m-0"> <?php echo $row['content']; ?></p>
</div>
<?php endif; ?>
<?php endwhile;
endif; ?>
</div>
<?php endforeach ?>
</div>
<a class="carousel-control-prev" href="#carousel_03" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only"></span>
</a>
<a class="carousel-control-next" href="#carousel_03" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only"></span>
</a>
</div>
You are looping each field 2 times. You could use get_sub_field() in order to simplify your code, try something like this:
<div class="carousel-inner">
<?php if (have_rows('section_8_slider')):
while (have_rows('section_8_slider')): the_row();
$image = get_sub_field('image');
$content_title = get_sub_field('content_title');
$content = get_sub_field('content');?>
<div class="carousel-item">
<img src="<?= $image['url'] ?>" alt="<?= $image['alt'] ?>">
<div class="carousel-text-block">
<h4 class="mb-1"> <?= $content_title;?></h4>
<p class="small m-0"> <?= $content;?></p>
</div>
</div>
<?php endwhile;
endif; ?>
Hope it helps
I have managed to solve it myself, i was close, please see answer bellow
<div class="carousel-inner">
<?php foreach ($sliderImages as $imgNumber => $image): ?>
<div class="carousel-item<?php if ($imgNumber === 0) : ?> active<?php endif ?>">
<img src="<?= $image['image']['url'] ?>" alt="<?= $image['image']['alt'] ?>">
<div class="carousel-text-block">
<h4 class="mb-1"> <?php echo $image['content_title']; ?></h4>
<p class="small m-0"> <?php echo $image['content']; ?></p>
</div>
</div>
<?php endforeach ?>
</div>

Wordpress conditionally load class when comments author matches author of the post

I want to be able to conditionally load a class containing the word 'AUTHOR' in the comments section, next to the name of the person who was the author of the post. I know it has something to do with class bypostauthor, what I've got so far is:
<?php if $class == 'bypostauthor' ?>
<div class="author">AUTHOR</div>
<? endif; ?>
More info:
In comment section of my wordpress post page, the class .bypostauthor exists whenever a the author of post page comments on there post page, giving this for one of the comments in the comment section ->
<li class="comment byuser comment-author-admin bypostauthor odd alt thread-odd thread-alt depth-1 clearfix" id="li-comment-28">
<div class="comment-block" id="comment-28">
<div class="comment-inside-block">
<div class="comment-info">
<div class="comment-author vcard clearfix">
<img alt="" src="http://0.gravatar.com/avatar/6dbfd7e6bd95881c5736647c8736dd89?s=32&d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G" class="avatar avatar-32 photo" height="32" width="32">
<div class="comment-meta commentmetadata">
<cite class="fn">Trent Aughey</cite>
<div style="clear:both;"></div>
<a class="comment-time" href="http://trentaughey.com/test/#comment-28"> 1 day ago </a> </div>
</div>
<div class="clearfix"></div>
</div>
<div class="comment-text">
<p>comment – 2</p>
<p class="reply">
<a class="comment-reply-link" href="/test/?replytocom=28#respond" onclick="return addComment.moveForm("comment-28", "28", "respond", "570")">Reply</a> </p>
</div>
</div>
</div>
<ul class="children">
<li class="comment byuser comment-author-admin bypostauthor even depth-2 clearfix" id="li-comment-29">
<div class="comment-block" id="comment-29">
<div class="comment-inside-block">
<div class="comment-info">
<div class="comment-author vcard clearfix">
<img alt="" src="http://0.gravatar.com/avatar/6dbfd7e6bd95881c5736647c8736dd89?s=32&d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G" class="avatar avatar-32 photo" height="32" width="32">
<div class="comment-meta commentmetadata">
<cite class="fn">Trent Aughey</cite>
<div style="clear:both;"></div>
<a class="comment-time" href="http://trentaughey.com/test/#comment-29"> 1 day ago </a> </div>
</div>
<div class="clearfix"></div>
</div>
<div class="comment-text">
<p>HELLO</p>
<p class="reply">
</p>
</div>
</div>
</div>
When a non-author of the post page leaves a comment on the post the following code is used
<li class="comment even thread-even depth-1 clearfix" id="li-comment-20">
<div class="comment-block" id="comment-20">
<div class="comment-inside-block">
<div class="comment-info">
<div class="comment-author vcard clearfix">
<img alt="" src="http://0.gravatar.com/avatar/6dbfd7e6bd95881c5736647c8736dd89?s=32&d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G" class="avatar avatar-32 photo" height="32" width="32">
<div class="comment-meta commentmetadata">
<cite class="fn">Trent Aughey</cite>
<div style="clear:both;"></div>
<a class="comment-time" href="http://trentaughey.com/test/#comment-20"> 1 month ago </a> </div>
</div>
<div class="clearfix"></div>
</div>
<div class="comment-text">
<p>Test, Test</p>
<p class="reply">
<a class="comment-reply-link" href="/test/?replytocom=20#respond" onclick="return addComment.moveForm("comment-20", "20", "respond", "570")">Reply</a> </p>
</div>
</div>
</div>
I'm trying to accomplish having the word 'AUTHOR' next to the author of post name whenever they comment on there page. Considering bypostauthor class appears whenever page author post a comment on his page I tried to use the following code to make a div class conditionally appear whenever the post author comments ->
<li <?php comment_class('clearfix'); ?> id="li-comment-<?php comment_ID() ?>">
<div class="comment-block" id="comment-<?php comment_ID(); ?>">
<div class="comment-inside-block">
<div class="comment-info">
<div class="comment-author vcard clearfix">
<?php echo get_avatar( $comment->comment_author_email, 32 ); ?>
<div class="comment-meta commentmetadata">
<?php printf(__('<cite class="fn">%s</cite>', 'playne'), get_comment_author_link()) ?><?php if $class == 'bypostauthor' ?>
<div class="author">AUTHOR</div>
<? endif; ?>
<div style="clear:both;"></div>
<a class="comment-time" href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ) ?>"> <?php echo themeblvd_time_ago_1(); ?> </a><?php edit_comment_link(__('(Edit)', 'playne'),' ','') ?>
</div>
</div>
<div class="clearfix"></div>
</div>
<div class="comment-text">
<?php comment_text() ?>
<p class="reply">
<?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
</p>
</div>
</div>
<?php if ($comment->comment_approved == '0') : ?>
<em class="comment-awaiting-moderation"><?php _e('Your comment is awaiting moderation.', 'playne') ?></em>
<?php endif; ?>
</div>
My code fix does nothing, can anyone tell me what I'm doing wrong please.
You can filter the comment author name instead of adding something to core files. Your change will be overwritten on a WordPress update.
add_filter( 'get_comment_author', 'your_prefix_filter_the_post_author_name' );
function your_prefix_filter_the_post_author_name( $author ) {
$classes = get_comment_class();
if (in_array( 'bypostauthor',$classes) ) {
$output = '<div class="author-tag"><p>AUTHOR - ' . $author . '</p></div>';
return $output;
}
return $author;
}
I was able to fix this problem using the following code:
function author_tag() {
$classes = get_comment_class();
if(in_array('bypostauthor',$classes)) {
$output = '<div class="author-tag"><p>AUTHOR</p></div>';
} /*else {
$output = '<div class="author-tag"><p>NOT AUTHOR</p></div>';
}*/
return $output;}
Placed the function as follows:
<li <?php comment_class('clearfix'); ?> id="li-comment-<?php comment_ID() ?>">
<div class="comment-block" id="comment-<?php comment_ID(); ?>">
<div class="comment-inside-block">
<div class="comment-info">
<div class="comment-author vcard clearfix">
<?php echo get_avatar( $comment->comment_author_email, 32 ); ?>
<div class="comment-meta commentmetadata">
<?php printf(__('<cite class="fn">%s</cite>', 'playne'), get_comment_author_link()) ?>
<?php echo author_tag(); ?>
<div style="clear:both;"></div>
<a class="comment-time" href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ) ?>"> <?php echo themeblvd_time_ago_1(); ?> </a><?php edit_comment_link(__('(Edit)', 'playne'),' ','') ?>
</div>
</div>
<div class="clearfix"></div>
</div>
<div class="comment-text">
<?php comment_text() ?>
<p class="reply">
<?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
</p>
</div>
</div>
<?php if ($comment->comment_approved == '0') : ?>
<em class="comment-awaiting-moderation"><?php _e('Your comment is awaiting moderation.', 'playne') ?></em>
<?php endif; ?>
</div>
Hope this helps anyone who had similar issues.

Unwanted <a> tags in Wordpress

I have a problem with the way WordPress creates code.
As shown below, my articles should be wrapped inside an <a> tag. WordPress creates a few extra wraps and I don't know how to get rid of them.
Does anyone has an idea?
This:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article class="post-thumb">
<a href="<?php the_permalink(); ?>">
<?php if ( has_post_thumbnail() ) {the_post_thumbnail();} ?>
<div class="post-thumb-caption">
<h2><?php the_title(); ?></h2>
</div>
<div class="post-thumb-caption-hover">
<div class="post-thumb-caption-hover-center">
<img id="menu-icon" class="icons svg" src="icons/menu21.svg"></img>
<h2><?php the_title(); ?></h2>
<h3><?php the_category(', ') ?></h3>
</div>
</div>
</a>
</article>
<?php endwhile; ?>
<?php post_navigation(); ?>
<?php else : ?>
<h2><?php _e('Nothing Found','html5reset'); ?></h2>
<?php endif; ?>
Should result in this:
<article class="post-thumb">
<a href="#">
<img src="3a.jpg" id="image">
<div class="post-thumb-caption">
<h2>Titel</h2>
</div>
<div class="post-thumb-caption-hover">
<div class="post-thumb-caption-hover-center">
<img id="menu-icon" class="icons svg" src="icons/menu21.svg">
<h2>Titel</h2>
<h3>Categorie</h3>
</div>
</div>
</a>
</article>
But WordPress creates this:
<article class="post-thumb">
<a href="http://localhost/sm/titel/">
<img width="300" height="300" src="http://localhost/sm/wp-content/uploads/2014/08/3a.jpg" class="attachment-post-thumbnail wp-post-image" alt="3a"> <div class="post-thumb-caption">
<h2>Titel</h2>
</div>
</a><div class="post-thumb-caption-hover"><a href="http://localhost/sm/Titel/">
</a><div class="post-thumb-caption-hover-center"><a href="http://localhost/sm/Titel/">
<img id="menu-icon" class="icons svg" src="icons/menu21.svg">
<h2>Willem en Henk</h2>
</a><h3>Ontbijt, Snacks</h3>
</div>
</div>
</article>
Change
<a href="<?php the_permalink(); ?>">
to
<a href="#">
and it should fix that problem.

Categories