echo html and wordpress php variable in string - php

I am trying to echo html and php in a string but I get a syntax error with the code below.
$post_title .= '
<div id="show_neil" class="box five columns" data-target="#member_neil">'.
'<h4 class="name">'.
'<img src="' . get_bloginfo('template_directory') . '/img/team/neil.png">'.
.get_the_title($post_id).
'</h4>'.
'</div>
';
How do I echo this?

Try to use get_bloginfo('template_directory') instead of bloginfo('template_directory').

Related

How can I add a horizontal line in php after some text?

I do know how to add horizontal line in html by using <hr> but I am not sure how to do in php.
My code:
echo '<div class="grid">';
echo '<p>' . $row["title"] . '</p>';
echo '</div>';
I want a line here like (________________) on the website after the $row["title"]
I tried doing this:
echo <hr style="width:50%; text-align:left;margin-left:0">;
But it doesn't seem to work. How can I add the horizontal line? Also, I would like to style it with a color, so something like a colored horizontal line such as yellow or any color?
Try the changes below
Change this:
echo '<div class="grid">';
echo '<p>' . $row["title"] . '</p>';
echo '</div>';
To this:
echo '<div class="grid">' . '<p>' . $row["title"] . '</p>' . '</div>';
echo '<hr/>';
This should work depend on your parent container width.
More how to use this break you can find here: HTML <hr> Tag

while looping w3-quarter in w3-row-padding

Here is the problem that while looping the php in while loop in w3-row-padding of w3 responsive layout . The layout breaks
Here is the source code
<?php
$r=0;
while($r<ceil($fetch_row_count/4))
{ ?>
<div class="w3-row-padding w3-padding-16 w3-center" style="clear:both" id="food">
<?php
while($row=mysqli_fetch_array($res))
{
?>
<div class="w3-quarter">
<img src="admin/uploads/<?php echo $row['image']; ?>" alt="noodles" style="width:50%">
<h3><?php echo $row['title']; ?></h3>
<p><?php echo $row['description']; ?> </p>
</div>
<?php
}
$r++;
}
?>
</div>
Thanks for reply and comments in advance
That bottom div was not being added for each of your padded containers.
The way the code is written you are adding a padded container and then adding your w3-quarter div for each of your result sets and then repeating that a bunch of times with what looks like to me the same set of data in each one.
What you are probably trying to accomplish is just making one padded div and then echo out your result set with the w3-quarter divs inside of it.
Here is your original way with the bottom div corrected:
<?php
$r=0;
while($r<ceil($fetch_row_count/4)) {
echo
'<div class="w3-row-padding w3-padding-16 w3-center" style="clear:both" id="food">';
while($row=mysqli_fetch_array($res)){
echo
'<div class="w3-quarter">' .
'<img src="admin/uploads/' . $row['image'] . '" alt="noodles" style="width:50%">' .
'<h3>' . $row['title'] . '</h3>' .
'<p>' . $row['description'] . '</p>' .
'</div>';
}
$r++;
echo
'</div>';
}
?>
Here is the way I think you are trying to do it: (Just guessing)
<?php
echo
'<div class="w3-row-padding w3-padding-16 w3-center" style="clear:both" id="food">';
$r = 0;
while($row=mysqli_fetch_array($res)){
echo
'<div class="w3-quarter">' .
'<img src="admin/uploads/' . $row['image'] . '" alt="noodles" style="width:50%">' .
'<h3>' . $row['title'] . '</h3>' .
'<p>' . $row['description'] . '</p>' .
'</div>';
$r++;
//I would not actually try to control how many results you add like this.
//I would get the results from a better formatted query.
if($r < ceil($fetch_row_count/4)){
break;
}
}
echo
'</div>';
?>

getting 6 pics on one line

I am trying to have multiple lines of pictures with 6 pics in each line. Currently there are 4 pics on each line and I am not sure how to make it so I have 6. Any help would be very appreciated!
Here is my code:
<?php
$pageTitle = "Bright Punch Love";
$section = "home";
include('inc/products.php');
?>
<?php include('inc/header.php'); ?>
<div id="top"><img src="top.png" width="1337"></div>
<div class="shirts">
<ul class= "shirts-section">
<?php
foreach($products as $product_id => $product) {
echo "<li>";
echo '<a href="shirt.php?id=' . $product_id . '">';
echo '<img src="' . $product["img"] . '" width= 200 alt="' . $product["name"] . '">';
echo '<p id="details">$30 View Details</p>';
echo "</a>";
echo "</li>";
}
?>
</ul>
</div>
<?php include('inc/footer.php'); ?>
<div id="bottom-index"><img src="bottom-index.png" width="1337"></div>
I would suggest to use % instead of px. Also you use the width tag. Try to avoid using css in your html code. Use class="..." instead and include your css file in the head tag. I make the file now in the head, because it is easier for you. But I would recommend to make an file just for the css.
<head>
<style type="text/css">
.picture{
width : 15%;
}
</style>
</head>
echo '<img src="' . $product["img"] . '" class=\"picture\" alt="' . $product["name"] . '">';
Use PHP's array_chunk function to short your answer. Just Try.
$products_new = array_chunk($products, 6);
foreach($products_new as $product_id => $product) {
//Your code
}

adding a php custom field into a php code

I'm using a PHP code on my wesbiste to get thumbnails of vimeo videos.
here is the code :
<?php
echo '<img src="' . get_vimeo_thumb('43096888', 'thumbnail_large') . '" class="image_page_texte_et_medias" style="cursor:pointer" >';
?>
I'm using a ACF custom field with the video ID
<?php the_sub_field('vimeo'); ?>
What I'm trying to do is to replace the ID in my first php code ('43096888') by :
<?php the_sub_field('vimeo'); ?>
can anybody help me with this,
thanks a lot,
aren't you allowed to use variables as arguments? something like:
<?php
$foo = the_sub_field('vimeo');
echo '<img src="' . get_vimeo_thumb($foo, 'thumbnail_large') . '" class="image_page_texte_et_medias" style="cursor:pointer" >';
?>
<?php
echo '<img src="' . get_vimeo_thumb(the_sub_field('vimeo'), 'thumbnail_large') . '" class="image_page_texte_et_medias" style="cursor:pointer" >';
?>
<?php
echo '<img src="' . get_vimeo_thumb(the_sub_field('vimeo'), 'thumbnail_large') . '" class="image_page_texte_et_medias" style="cursor:pointer" >';
?>
You can simply replace the string '43096888' with your function the_sub_field('vimeo') if that's something what you want.

Output text/link with variables inside

How can i make this output properly? Im using the right variables but i think im doing something wrong with quotations or what not.
<?php echo '<img src="' SITE_URL . 'img/' . $code '" title="Created by website.com" />' ?>
You need to join all the pieces of the string with ., the concatenation operator. You're missing several.
<?php echo '<img src="' . SITE_URL . 'img/' . $code . '" title="Created by website.com" />' ?>
echo also accepts multiple arguments, so you may use commas instead of periods:
<?php echo '<img src="', SITE_URL, 'img/', $code, '" title="Created by website.com" />' ?>
<?php echo '<img src="'. SITE_URL . 'img/' . $code .'" title="Created by website.com" />' ?>
You can do it multiple ways, inline with the HTML
<img src="<?= $SITE_URL ?>" title="..."/>
Or by echoing the whole string in PHP
<?php
echo '<image src="' . $_SITE_URL . '" title="..."/>';
?>
If you do it in PHP, you need to make sure you use quotations correctly, escape characters as needed, and join all strings with a .

Categories