Postitioning a set of images - css

I have some PHP which will output a set of images.If I write it this way
foreach( $data as $inform ) {
{if (isset($inform['file1'])) {
echo '<img src="'.$inform['file1'].'"><br><br>';
}
}
It will display the 4 different images in the array $inform['file1'] with 2 line breaks in between each, but if I want to position them on the page like this
foreach( $data as $inform ) {
if ( isset( $inform['file1'] ) ) {
echo '<div style="position:absolute; top:400px;"><img src="'.$inform['file1'].'"><br><br>
</div>';
}}
Then it displays all 4 images on top of each other, but in the right location.
I've tried styling it in css and nothing works. Can anybody help a newbie learn this?
Thanks in advance.

It's nothing to do with your foreach loop. You're literally telling the images to go on top of each other, so that's what they are doing. If you would like to add spacing to the top, put a container div around the images and do a margin-top: 400px;.

Your biggest problems here are:
You haven't explained what you want to achieve, and
You are mixing in PHP which is making a relatively simply problem seem harder to solve
The styling and positioning of elements should be managed through CSS (preferably not inline CSS).
Your PHP should be:
if( count( $data ) ){
echo '<div class="file1_container">';
# If there are elements in the $data array
foreach( $data as $inform ){
if( isset( $inform['file1'] ) ){
echo '<img src="'.$inform['file1'].'" />';
}
}
echo '</div>';
}else{
# No elements in the $data array
}
Your CSS could then be something like:
.file1_container {
position:absolute;
left:0;top:400px;right:0;
text-align:center;
}
This will position a DIV containing all the images at 400px from the origin, and the IMGs within it will be on the same horizontal line, aligned to the centre of the DIV.

Your practice is bad, but if you must:
foreach($data as $inform){
if(isset($inform['file1'])) {
echo "<div><img src='{$inform['file1']}' /></div><br /><br />";
}
}
Of course, this would add two breaks after the last one as well. You cannot have multiple items in different places using the same position:absolute, since they will all be relative to the last position:relative. Regardless, you should use CSS.
#PHP
foreach($data as $inform){
if(isset($inform['file1'])) {.
echo "<div class='someClass'><img src='{$inform['file1']}' /></div";
}
}
/*CSS*/
.someClass{
margin-top:10px;
}

Related

Add text upper left corner Wordpress Storefront

This started out as a Question, but I sort of made it work. I am sharing it for anyone who needs to know. Or maybe there is a better way?
I'd like to show a welcome message on the home page. Should be placed in the upper left corner.
This is what I did:
add_action( 'storefront_header', 'pfn_userinheader',1);
function pfn_userinheader() {
?>
<span class="pfn_great_header2"> Hello! blablabla</span>
<?php
}
Now the text is placed where the secondary menu. More like top, right of middle. I have no idea why just there. Anyway, I would like it to be placed in the top left corner.
This is my css:
.pfn_great_header2{
position: absolute !important;
left: 0;
top:0;
width: 10% !important;
color: #0071A1;
}
It works. Is it a good way of doing this?
I have something similar on my site but it displays a different message if the customer is already logged in.
If you wanted to do this, you could use something like this:
add_action( 'storefront_header', 'pfn_userinheader',1);
function pfn_userinheader() {
if ( is_user_logged_in() ) {
echo '<span class="pfn_great_header2">Welcome back <?php $current_user = wp_get_current_user(); echo $current_user->user_firstname; ?>!</span>';
}
elseif {
echo '<span class="pfn_great_header2"> Hello! blablabla</span>';
}
}

Style based on category

I can't quite seem to get the hover to change colour based on the category the post is tagged in.
I've added both body_class() and post_class() to the header.php as well as adding a function that adds the current post's category back to the body class -
add_filter('body_class','add_category_to_single');
function add_category_to_single($classes) {
if (is_single() ) {
global $post;
foreach((get_the_category($post->ID)) as $category) {
$classes[] = 'category-'.$category->slug;
}
}
return $classes;
}
I'm able to change the hover on all the posts by using this styling -
.portfolio .portfolio-item:hover > .hover-item{
background-color:#000000;
}
but when I add the category class nothing happens. For example -
catergory-classes .portfolio-item:hover > .hover-item{
background-color:#000000;
}
Where am I going wrong?
Here is my site. Any help gratefully appreciated
i think you for get to put a point at the first of css code
your css is
catergory-classes .portfolio-item:hover > .hover-item{
background-color:#000000;
}
but correct is
.catergory-classes .portfolio-item:hover > .hover-item{
background-color:#000000;
}
I think you missed a dot (.)
.catergory-classes .portfolio-item:hover > .hover-item{
background-color:#000000;}
And if the .catergory-classes and .portfolio-item needs to be applied on the same tag , then it should be (no spaces b/w 2 classes)
.catergory-classes.portfolio-item:hover > .hover-item{
background-color:#000000;}
You shouldn't be setting a body class, since this will be the category of the front page itself (and won't change for each item). Instead, consider adding classes to the portfolio items (within The Loop). For example:
<?php
if ( have_posts() ) : while ( have_posts() ) : the_post();
$categories = get_the_category();
$category = ! empty( $categories ) ? 'category-' . $categories[0]->name : '';
?>
<li class="portfolio-content">
<div class="portfolio-item <?php echo $category; ?>">
<!-- Additional Content -->
</div>
</li>
You can then target these categories with:
.portfolio-item.category-somecategory:hover > .hover-item { }

Is it adivsable to use DOMDocument on Wordpress content?

I'd like to tinker with the content Worpress generates before it gets displayed.
A lot of examples I saw so far use regex to change the content even though it is bad practise and not advisable to parse html with regex.
My idea now is to use DOMDocument to parse and change the HTML. Is that a good idea or can this break the integrity of the content in any way?
Here is some example Wordpress content filter I created (it scales the images down half the size for retina displays):
function fancyContentParser( $content ) {
$dom = new DOMDocument();
$dom->loadHTML($content);
$images = $dom->getElementsByTagName('img');
foreach ($images as $image) {
$width = $image->getAttribute('width')/2;
$height = $image->getAttribute('height')/2;
$style = 'width: '.$width.'px; height: '.$height.'px';
$image->setAttribute('style', $style);
}
return $dom->saveHTML();
}
add_filter( 'the_content', 'fancyContentParser' );
The answers to this question are probably going to be primarily opinion-based...but I don't see anything wrong with it. However, why wouldn't you just change the width and height attributes, rather than overriding them with style?
foreach ($images as $image) {
$image->setAttribute( 'width', $image->getAttribute('width')/2 );
$image->setAttribute( 'height', $image->getAttribute('width')/2 );
}
Additionally, you can likely accomplish this without DOMDocument...and just use CSS (which is what you're proposing in your question by using a style attribute anyways). You can use the transform property:
img {
-webkit-transform: scale(0.5);
transform: scale(0.5);
}

Placing empty div for placing ads in layout

On pageload, following code to list items in box
{foreach from=$results key=k item=video_data name=loop}
<div class="list"><ul class="pic-list">
<li class="Li01" style="visibility: visible; opacity: 1;">...</li></u1>
</div>
{/foreach}
It display data perfectly...now i like to have insert empty div to include ad.
I like to display 3 blocks of elements, each block will have one big unit for ad, and 8 video elements.
please check attached picture for better understanding.
Can anyone please help me to rearrage layout as in img.
Thanks
I would have a for loop iterate 4 times, and nest a foreach loop inside it.
For the ad, you can add it in the foreach loop after throwing in the ul. Below is a rough example (I'm assuming your data is coming in through a multi-dimensional array):
$results_array = [whatever your array output consists of];
for($i = 0; $i < 4; $i++){
echo '<ul class="pic-list"><li class="ad"></li>';
foreach($results_array[$i] as $j => $item){
echo '<li class="video"></li>';
}
echo '</ul>';
}

Bootstrap continual thumbnail list into grid

I want to make a long list of elements, images for example that will be added into the grid which width is span12 and every elements width should be span3, that makes 4 elements in 1 row.
The problem is, when I list all the elements or I make a php function that lists them Boostrap thinks I want to put it in one row, but I don't. I want to make a new row every 4 elements or so... I want to be able to add as many <li> as I want and not worry about where another row should be.. What should I do ?
This is what happened to me, the 5th image has wrong margin on the left side. I want to make it in the same position as images in the first row.
Screenshot here
I use this php logic to echo out pictures
$dir = "../img";
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if (preg_match("/.jpg/", $file)) {
echo "<li class='span3'>
<a href='$dir/$file' class='thumbnail'>
<img src='$dir/$file'>
</a>
</li>
";
}
}
closedir($dh);
}
}
And I have no idea on how to post a new row each 4th picture or so.
Row is marked as <div class='row-fluid'>
That's a common problem, and I think there's no elegant solution. Consider one of these approachings.
Solution 1 Review your iteration logic
If refactoring your PHP logic is an option, try to render a row each 4 elements (I can't suggest an specific code without viewing your current logic).
EDIT
The code for a while loop could be as follows:
<?php
$count = 1;
echo '<div class="row">';
while (your_condition) {
echo '<div class="span3">your_content</div>';
if ($count % 4 == 0) {
echo '</div>';
echo '<div class="row">';
}
$count++;
}
echo '</div>';
Solution 2 Hack the fifth element's margin
Correct the margin-left of the affected elements, so you don't need to respect the native Bootstrap markup.
.thumbnails .span4:nth-child(4n+1) {
margin-left: 0;
}
Unfortunately, that code is not compatible with Internet Explorer, so maybe you'll have to find a JavaScript or jQuery solution.

Resources