Bootstrap columns and Wordpress Posts - php

So I'm converting bootstrap site to Wordpress and post cards don't want to stack horizontally... I know there should be foreach loop which will loop through posts but I don't know how to set it up...
<?php while(have_posts()) {
the_post();
?>
<section id="blog" class="py-3">
<div class="container">
<div class="row">
<div class="col-md-4">
<div class="card-columns">
<div class="card">
<img src="<?php the_post_thumbnail_url('medium')?>" alt="" class="img-fluid card-img-top">
<div class="card-body">
<h4 class="card-title">
<?php the_title(); ?>
</h4>
<small class="text-muted">Written by <?php the_author_posts_link(); ?></small>
<hr>
<p class="card-text"><?php the_excerpt(); ?></p>
</div>
</div>
</div>
</div>
</div>
</section>
<?php } ?>

while is also a loop. Inside the loop, you can just put the repeated code. As I've understood your code than you need to something like this:
<section id="blog" class="py-3">
<div class="container">
<div class="row">
<?php while( have_posts() ) {
the_post();
?>
<div class="col-md-4">
<div class="card-columns">
<div class="card">
<img src="<?php the_post_thumbnail_url('medium')?>" alt="" class="img-fluid card-img-top">
<div class="card-body">
<h4 class="card-title">
<?php the_title(); ?>
</h4>
<small class="text-muted">Written by <?php the_author_posts_link(); ?></small>
<hr>
<p class="card-text"><?php the_excerpt(); ?></p>
</div>
</div>
</div>
<?php } ?>
</div>
</div>
</section>
Also, better to move code for one block to another file for example partials/post.php:
<div class="col-md-4">
<div class="card-columns">
<div class="card">
<img src="<?php the_post_thumbnail_url('medium')?>" alt="" class="img-fluid card-img-top">
<div class="card-body">
<h4 class="card-title">
<?php the_title(); ?>
</h4>
<small class="text-muted">Written by <?php the_author_posts_link(); ?></small>
<hr>
<p class="card-text"><?php the_excerpt(); ?></p>
</div>
</div>
</div>
</div>
And update your current file:
<section id="blog" class="py-3">
<div class="container">
<div class="row">
<?php while( have_posts() ) {
the_post();
get_template_part( 'partials/post' );
} ?>
</div>
</div>
</section>

Related

I have an error in my code that does not allow me to show the results and/or show an error message in php

I have a little problem that might be syntax (since I'm not that good at php). Basically, I'm de-wrapping a code where there will be records where there will be a date (in this case, a foundation date, for example; 20-08-2027). So I created an "if". If there are records with the date, for example, 2027, the records appear. If there is not, then an error message will be displayed saying that there is still no record made with that date. I've tried a few things, but nothing worked. At this point, an error appears. Below will be the error. Please try to help me. It's no use saying more technical things, because I'm not that good at php. Thank you.
Error: "Parse error: syntax error, unexpected 'else' (T_ELSE) in /home/host/public_html/template/year/2027.php on line 300"
2027.php
<section class="content products">
<div class="container">
<h1 class="hidden">Eventos</h1>
<div class="row">
<div class="col-sm-10">
<div class="row grid" id="products">
<div class="releated-products">
<div class="row grid" id="products">
<?php
$sqli=sprintf("SELECT * FROM eventos WHERE YEAR(data) = 2027");
$resu=mysqli_query($con,$sqli);
mysqli_set_charset($con, 'UTF8');
if (mysqli_num_rows($resu)>0) {
while($regi=mysqli_fetch_array($resu)){
$sqli_consulta=sprintf("select * from eventos where id=%d;",$regi['id']);
$resu_consulta=mysqli_query($con,$sqli_consulta);
$regi_consulta=mysqli_fetch_array($resu_consulta);
$linkk='../eventoindividual.php?id='.$regi_consulta['id'];
?>
<div class="col-sm-3 col-xs-6">
<article class="product-item">
<div class="row">
<div class="col-sm-3">
<div class="product-overlay">
<div class="product-mask"></div>
<img src="<?php echo '../admin/documentos/'.$regi['nome_doc']; ?>" style="width:100%">
</div>
</div>
<div class="col-sm-9">
<div class="product-body">
<h3><?php echo $regi['nome']; ?></h3>
<br>
<span class="price">
<ins><span class="amount"><?php echo $regi['preco']; ?></span></ins>
</span>
<div class="buttons buttons-simple">
<i class="fa fa-shopping-cart"></i>Comprar bilhetes
</div>
</div>
</div>
</div>
</article>
</div>
<?php
} else{
?>
<p>Nada!</p>
<?php
}
}
?>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
The reason you are getting the error because you have while loop in your if block that you are not closing.
Check the code below for this fixed version.
<?php
$sqli=sprintf("SELECT * FROM eventos WHERE YEAR(data) = 2027");
$resu=mysqli_query($con,$sqli);
mysqli_set_charset($con, 'UTF8');
if (mysqli_num_rows($resu)>0):
while($regi=mysqli_fetch_array($resu)):
$sqli_consulta=sprintf("select * from eventos where id=%d;",$regi['id']);
$resu_consulta=mysqli_query($con,$sqli_consulta);
$regi_consulta=mysqli_fetch_array($resu_consulta);
$linkk='../eventoindividual.php?id='.$regi_consulta['id'];
?>
<div class="col-sm-3 col-xs-6">
<article class="product-item">
<div class="row">
<div class="col-sm-3">
<div class="product-overlay">
<div class="product-mask"></div>
<img src="<?php echo '../admin/documentos/'.$regi['nome_doc']; ?>" style="width:100%">
</div>
</div>
<div class="col-sm-9">
<div class="product-body">
<h3><?php echo $regi['nome']; ?></h3>
<br>
<span class="price">
<ins><span class="amount"><?php echo $regi['preco']; ?></span></ins>
</span>
<div class="buttons buttons-simple">
<i class="fa fa-shopping-cart"></i>Comprar bilhetes
</div>
</div>
</div>
</div>
</article>
</div>
<?php endwhile; else: ?>
<p>Nada!</p>
<?php
endif;
?>
Move the last } bracket above }else{
<section class="content products">
<div class="container">
<h1 class="hidden">Eventos</h1>
<div class="row">
<div class="col-sm-10">
<div class="row grid" id="products">
<div class="releated-products">
<div class="row grid" id="products">
<?php
$sqli=sprintf("SELECT * FROM eventos WHERE YEAR(data) = 2027");
$resu=mysqli_query($con,$sqli);
mysqli_set_charset($con, 'UTF8');
if (mysqli_num_rows($resu)>0) {
while($regi=mysqli_fetch_array($resu)){
$sqli_consulta=sprintf("select * from eventos where id=%d;",$regi['id']);
$resu_consulta=mysqli_query($con,$sqli_consulta);
$regi_consulta=mysqli_fetch_array($resu_consulta);
$linkk='../eventoindividual.php?id='.$regi_consulta['id'];
?>
<div class="col-sm-3 col-xs-6">
<article class="product-item">
<div class="row">
<div class="col-sm-3">
<div class="product-overlay">
<div class="product-mask"></div>
<img src="<?php echo '../admin/documentos/'.$regi['nome_doc']; ?>" style="width:100%">
</div>
</div>
<div class="col-sm-9">
<div class="product-body">
<h3><?php echo $regi['nome']; ?></h3>
<br>
<span class="price">
<ins><span class="amount"><?php echo $regi['preco']; ?></span></ins>
</span>
<div class="buttons buttons-simple">
<i class="fa fa-shopping-cart"></i>Comprar bilhetes
</div>
</div>
</div>
</div>
</article>
</div>
<?php
} // while()
} else{
?>
<p>Nada!</p>
<?php
}
// } //wrong bracket
?>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
For your own sanity it is a good idea leave a comment after closing bracket of a long code, so you know which block it belongs to.
Compiler can't be wrong, if it says there's syntax error then there is. The bracket before the else is closing the while loop - hence else cannot be used there, close the if condition. Here's the corrected code:
<section class="content products">
<div class="container">
<h1 class="hidden">Eventos</h1>
<div class="row">
<div class="col-sm-10">
<div class="row grid" id="products">
<div class="releated-products">
<div class="row grid" id="products">
<?php
$sqli=sprintf("SELECT * FROM eventos WHERE YEAR(data) = 2027");
$resu=mysqli_query($con,$sqli);
mysqli_set_charset($con, 'UTF8');
if (mysqli_num_rows($resu)>0) {
while($regi=mysqli_fetch_array($resu)) {
$sqli_consulta=sprintf("select * from eventos where id=%d;",$regi['id']);
$resu_consulta=mysqli_query($con,$sqli_consulta);
$regi_consulta=mysqli_fetch_array($resu_consulta);
$linkk='../eventoindividual.php?id='.$regi_consulta['id'];
/* 2 brackets { */
?>
<div class="col-sm-3 col-xs-6">
<article class="product-item">
<div class="row">
<div class="col-sm-3">
<div class="product-overlay">
<div class="product-mask"></div>
<img src="<?php echo '../admin/documentos/'.$regi['nome_doc']; ?>" style="width:100%">
</div>
</div>
<div class="col-sm-9">
<div class="product-body">
<h3><?php echo $regi['nome']; ?></h3>
<br>
<span class="price">
<ins><span class="amount"><?php echo $regi['preco']; ?></span></ins>
</span>
<div class="buttons buttons-simple">
<i class="fa fa-shopping-cart"></i>Comprar bilhetes
</div>
</div>
</div>
</div>
</article>
</div>
<?php
} // while loop
} // if condition
else {
?>
<p>Nada!</p>
<?php
}
?>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
hope this helps. 👍

Only show <php "the_field()" ?> if it has a specifik value

So, i was wondering if there's a way to target a specific value in a <?php the_field('title'); ?> loop.
I'm iterating over a result set of people on a site. If title equals CEO it should display in another row.
<?php
/*
Template Name: Personal
*/
?>
<?php get_header(); ?>
<div id="main" class="fullpage" style="background-image: url(<?php echo get_template_directory_uri(); ?>/img/fullpagebg.png);">
<section class="banner innerBanner">
<div class="container">
<div class="bannerContent">
<h1>Vår <strong>personal</strong></h1>
</div>
</div>
</section>
<section class="padtop0">
<div class="container-fluid">
<div class="productsMain">
<div class="row justify-content-center">
<?php
$loop = new WP_Query( array( 'post_type' => 'Persons') );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="col-xl-3 col-lg-4 col-md-6 wow fadeInUp" data-wow-delay=".2s">
<div class="personalbox" style="background-color: #f3f3f3; background-image: url(<?php echo get_field('bild'); ?>); background-size: <?php echo $background_size; ?>;">
<div class="personalbox-description">
<h4 style="color:#000000 !important;"><strong><?php the_field('Namn'); ?></strong></h4>
<h6 style="color:#000000 !important;;"><?php the_field('title'); ?></h6>
<br>
<h6 style="color:#000000 !important;"><strong>Telefon:</strong> <?php the_field('telefon'); ?></h6>
<h6 style="color:#000000 !important;"><strong>Mailt:</strong> <?php the_field('e-post'); ?></h6>
</div>
</div>
</a>
</div>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
</div>
</div>
</div>
</section>
<?php get_footer(); ?>

how to view data from database with static images in php

When I try to view multiple data from database my static images are also showing multiple time. I try to use foreach and while with i++ but both time I get the same result.
How to view one image but multiple data.
<div class="row">
<?php
// set array
$array = array();
while($row=mysqli_fetch_assoc($result1))
{
// add each row returned into an array
$array[] = $row;
}
// debug:
foreach ($array as $arrays)
{
?>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/address-1.png" alt="">
<h3>Address</h3>
<h4><?php echo $arrays['address']; ?></h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/call.png" alt="">
<h3>Mobile </h3>
<h4><?php echo $arrays['mobile_no']; ?></h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/address-2.png" alt="">
<h3>Phone</h3>
<h4><?php echo $arrays['phone_no'];?> </h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/address-3.png" alt="">
<h3>Email</h3>
<h4><?php echo $arrays['email']; ?></h4>
</div>
</div>
</div>
<?php } ?>
</div>
It's normal because your images are in the foreach.
Remove your first foreach
Make function like
foreach($array as $value){
echo $value;
}
and put them below your images.
example with your code : (but better make functions for all the foreach)
<div class="row">
<?php
// set array
$array = array();
while($row=mysqli_fetch_assoc($result1))
{
// add each row returned into an array
$array[] = $row;
}
// debug:
?>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/address-1.png" alt="">
<h3>Address</h3>
<h4><?php foreach ($array as $arrays)
{
echo $arrays['mobile_no'];
}?></h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/call.png" alt="">
<h3>Mobile </h3>
<h4>//same here</h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/address-2.png" alt="">
<h3>Phone</h3>
<h4>//same here </h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/address-3.png" alt="">
<h3>Email</h3>
<h4>//same here</h4>
</div>
</div>
</div>
</div>
Define the Image field outside the loop:
<div class="row">
<?php
// set array
$array = array();
while($row=mysqli_fetch_assoc($result1))
{
// add each row returned into an array
$array[] = $row;
}
// debug:
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/address-1.png" alt="">
<h3>Address</h3>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/call.png" alt="">
<h3>Mobile </h3>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/address-2.png" alt="">
<h3>Phone</h3>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/address-3.png" alt="">
<h3>Email</h3>
</div>
</div>
foreach ($array as $arrays)
{
?>
<div class="col-sm-3">
<div class="address_item text-center">
<h4><?php echo $arrays['address']; ?></h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item text-center">
<h4><?php echo $arrays['mobile_no']; ?></h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item text-center">
<h4><?php echo $arrays['phone_no'];?> </h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item text-center">
<h4><?php echo $arrays['email']; ?></h4>
</div>
</div>
</div>
<?php } ?>
</div>
Simplest put not the most beatiful solution is to just check if its your "header" line and adding those images at first run.. you can also add the if around your images instead of dublication to much. but you will get the idea
<div class="row">
<?php
// set array
$array = array();
while($row=mysqli_fetch_assoc($result1))
{
// add each row returned into an array
$array[] = $row;
}
// debug:
$i = 0;
foreach ($array as $arrays)
{
?>
if($i == 0){
//first line
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/address-1.png" alt="">
<h3>Address</h3>
<h4><?php echo $arrays['address']; ?></h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/call.png" alt="">
<h3>Mobile </h3>
<h4><?php echo $arrays['mobile_no']; ?></h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/address-2.png" alt="">
<h3>Phone</h3>
<h4><?php echo $arrays['phone_no'];?> </h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<img src="img/soul-icon/address-3.png" alt="">
<h3>Email</h3>
<h4><?php echo $arrays['email']; ?></h4>
</div>
</div>
}else{
<div class="col-sm-3">
<div class="address_item">
<h3>Address</h3>
<h4><?php echo $arrays['address']; ?></h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<h3>Mobile </h3>
<h4><?php echo $arrays['mobile_no']; ?></h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<h3>Phone</h3>
<h4><?php echo $arrays['phone_no'];?> </h4>
</div>
</div>
<div class="col-sm-3">
<div class="address_item">
<h3>Email</h3>
<h4><?php echo $arrays['email']; ?></h4>
</div>
</div>
</div>
}
<?php } ?>
</div>

showing only first post when clicking read more of any post wordpress posts

Here is showing only last created post when i am clicking on readmore button of post.
remember i have called all posts on another template(our_program.php inplace of index.php) all posts are showing well but on single.php here is only only one recent post showing when i am clicking on read more of any post. my single.php code is below
<?php $args = array( 'post_type' => 'post');
query_posts($args);
?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<section class="talent-development" id="telent-development">
<div class="container">
<div class="row">
<div class="section-content-text clearfix">
<div class="col-sm-12">
<h2><?php the_title(); ?></h2>
</div>
<div class="col-sm-5">
<div class="ourProgramImg">
<?php if( has_post_thumbnail()){
the_post_thumbnail();
}
?>
</div>
</div>
<div class="col-sm-7">
<div class="ourProgramContent">
<p>
<?php the_content();?>
</p>
</div>
</div>
</div>
<!--<div class="col-sm-6">
<div class="section-content-img">
<img src="img/sec.jpg" alt="" title="" />
</div>
</div>-->
</div>
</div>
</section>
<?php endwhile; else: ?>
<p> Not post Found </p>
<?php endif; ?>
and post page(permalink) code below
<?php $args = array( 'post_type' => 'post');
query_posts($args);
?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<section class="talent-development" id="telent-development">
<div class="container">
<div class="row">
<div class="section-content-text clearfix">
<div class="col-sm-12">
<h2><?php the_title(); ?></h2>
</div>
<div class="col-sm-5">
<div class="ourProgramImg">
<?php if( has_post_thumbnail()){
the_post_thumbnail();
}
?>
</div>
</div>
<div class="col-sm-7">
<div class="ourProgramContent">
<p>
<?php echo substr(get_the_excerpt(), 0, 400); //the_content();?>
</p>
Read More
</div>
</div>
</div>
<!--<div class="col-sm-6">
<div class="section-content-img">
<img src="img/sec.jpg" alt="" title="" />
</div>
</div>-->
</div>
</div>
</section>
<?php endwhile; else: ?>
<p> Not post Found </p>
<?php endif; ?>

The author doesn't show in single.php

I am in the process of creating a theme and I can't get the author to be displayed on the single.php page. It's displayed lower on the site but doesn't seem to show in the first call under the headline.
<header class="intro-header" style="background-image: url('http://i.imgur.com/ZyZMhQv.jpg')">
<div class="container">
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
<div class="site-heading">
<h1><?php the_title(); ?></h1>
<hr class="small">
<span class="subheading">Posted by <?php the_author(); ?> on <?php the_time('F jS, Y'); ?></span>
<div id="avatar" class="row text-center"><?php echo get_avatar( $post->post_author, 92 ); ?> </div>
</div>
</div>
</div>
</div>
</header>
The second time it's called within the loop it works fine.
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="panel panel-default panel-body">
<?php while(have_posts()) : the_post(); ?>
<p> Posted by <?php the_author(); ?> on <?php the_time('F jS, Y'); ?> </p>
<p> <?php the_content(''); ?> </p>
<?php endwhile; wp_reset_query(); ?>
</div>
</div>
Why doesn't it show up the first time it's called?
as per the codex - it must be within the loop.

Categories