3 column WordPress layout help needed - css

I'm busy trying to put together a 3x3 magazine-style layout for the home page of my blog.
I want the divs containing each post to flow vertically so there aren't big white spaces beneath each row of 3. It seems like the easiest way to do this would be to have three columns and fill each of these (which will stop there from being a big space beneath each post-block) and then place these three columns side-by-side.
The problem is that the Wordpress loop needs to pull posts sequentially. I don't know how to change the order of the Wordpress loop and, even though I've tried some PHP trickery using counts and for loops, I can't seem to get it to work.
Any thoughts or advice on a basic Wordpress loop or CSS way to make it work would be much appreciated as it's driving me crazy!
You can see how it is currently at http://www.unleashreality.com/

This looks like a job for jQuery Masonry.
Check it out at http://masonry.desandro.com/

The direct way using loop manipulation could be simpler assuming your layout is going to fixed one as shown in the mockup image.
<?php
$count = 0;
$column_1 = '';
$column_2 = '';
$column_3 = '';
$ad_block = '<div id="ad-block">Ad code here</div>';
while ( have_posts() ) : the_post();
$count++;
$content = '';
$content .= '<div class="post-block">';
$content .= '<h2>'.get_the_title().'</h2>';
$content .= '<div class="excerpt-block">'.get_the_excerpt().'</div>';
$content .= '<div class="continuebutton">...READ THIS ARTICLE</div>'; // Add appropriate code here..
if($count == 1 || $count == 4 || $count == 6) {
$column_1 .= $content;
} else if($count == 2 || $count == 7) {
$column_2 .= $content;
} else if($count == 3 || $count == 5 || $count == 8) {
$column_3 .= $content;
} else {
// Shouldn't come here...
}
// Insert the ad block in column 2 after adding 1st row
if($count == 2) {
$column_2 .= $ad_block;
}
endwhile;
?>
<div id="col1"><?php echo $column_1;?></div>
<div id="col2"><?php echo $column_2;?></div>
<div id="col3"><?php echo $column_3;?></div>
Adjust the code to work with inner pages.

If you want to do this without Javascript, you'll need to buffer the HTML for each column in your post loop and then output it in one shot once the loop is finished.
Something like the following:
<?php
// Hold the buffered column output
$cols = array("", "", "");
// Keep track of column we're appending to
$currentCol = 0;
// Get the posts
$posts = get_posts();
foreach($posts as $post){
// Run the WP post filters
setup_postdata($post);
// Append the content to the current column
$cols[$currentCol] .= '<div class="item">';
$cols[$currentCol] .= get_the_title();
$cols[$currentCol] .= get_the_content();
$cols[$currentCol] .= '</div>';
// Increment the current column and make sure we haven't
// gone over our total columns
if(++$currentCol >= count($cols)){
$currentCol= 0;
}
}
?>
<div id="col1"><?php echo $cols[0]; ?></div>
<div id="col2"><?php echo $cols[1]; ?></div>
<div id="col3"><?php echo $cols[2]; ?></div>

Related

Show WooCommerce Parent Attributes Only

I'm trying to create 3 rows of buttons to select. Think sorting boxes. AT&T has it on this page. Where you see iPhone, Samsung and then sorting. I'm trying to do the same thing. AT&T's example page
The first row will be sub categories of parent categories. That part I was successful in completing. However, I'm struggling to call to all the parent attributes.
<?php
global $product;
$product_attributes = $product->get_attributes();
if(!empty($product_attributes)){
$product_attributes = array_keys($product_attribute);
$count = 0;
echo '<a class="archive-attributes" href="#">';
foreach ($product_attributes as $product_attribute){
$attribute_name = ucfirst( str_replace('pa_', '',
$product_attributes) );
echo $attribute_name . '';
if($count != count($product_attribute)) echo '';
else echo '';
}
echo '</a>';
}
?>
I only need the parent for this row. When one of these is clicked the children of that attribute will display below it. What am I doing wrong?

How do i change column layout in bootstrap 4 based on Odd / Even childs

I'm a Dutch ICT student asking for help,
I know how to change the column layout based on the width of the screen but I want to change the layout for odd and even objects.
I want to accomplish the following:
So I want my layout to be:
col[7]col[5]
col[5]col[7]
col[7]col[5]
col[5]col[7]
I can't do this manually because I linked it with WordPress, so when I post a new item I want it to automatically get the right column layout.
My code looks like this:
Please try below solution hope this work for you.
With the using Modulo, for that, you need to declare $i=0; variable in above while loop also set $i++; at the bottom for increment $i value.
Please use below code.
<?php
$i=0;
while( $qry->have_posts() ): $qry->the_post();
if ($i % 2 == 0)
{
echo '<div class="col-md-7">7</div>';
echo '<div class="col-md-5">5</div>';
}
else
{
echo '<div class="col-md-5">5</div>';
echo '<div class="col-md-7">7</div>';
}
$i++;
?>
<?php endwhile; ?>

How to Display A Line of Text if WordPress Category is empty?

I am trying to display a line of text if a specific category (in this case: advisories) is empty, but the code I am trying to use does not display anything.
<?php
if ( in_category( 'advisories' )->count > 0 ) {
echo test;
}
?>
Try this one,
Solution
$custom_terms = get_terms();
foreach ($custom_terms as $term) {
if ($term->count != 0 && $term->name == "Uncategorized") {
echo $term->name . " - blank" . '<br>';
}
}
Screenshot
Hope this will helps you.
For more example, please visit
get_terms Wordpress
WordPress to test for an empty term or category
You can try this one
$category = get_category($id);
$count = $category->category_count;
if( $count > $something ) {
// stuff
}

Wordpress - Retrieve permalinks of posts in a php generated html code

Ok, so i have created a post slider that contains 10 posts.
I have a php code that generate the slider and make each of these posts to have a specific class (small, middle, large image)
Problem:
I can't retreive the post link so it can be clickable.
$post->post_title
How can i add the:
$post->post_title
The code looks like this:
if($index === 0){
$output .= "<div class=\"page page-one\">";
}
$itemClass = $index < 2 ? "item-size-medium" : "item-size-small";
$img = get_the_post_thumbnail_url($post, 'large');
$output .= <<<HTML
<div class="item $itemClass">
<div class="item-img">
<img src="$img">
</div>
<div class="item-title">
<h1>
$post->post_title
</h1>
</div>
</div>
HTML;
if(($index !== 0 && ($index%5) === 0) || 1 == 2){
$output .= "</div>";
}
What's up Dragos. Assuming you're inside a loop (you referenced $post), you can use permalink or likely it'll be within the $post object if looping over a WP_Query. Reference

How to display WordPress RSS feed your website?

Hello i have a website and a blog, i want to display my self hosted wordpress blog on my website.
I want to show only 3 post on my website.
I want to automatically check for any new post everytime when i reload my website, so that the recent three gets displayed only.
I want to show the complete title of my wordpress blogpost but specific letters of description.
Also the description should end up with a word not some piece of non-dictionary word ending with "..."
How this can be done, i have heard that it can be done through RSS.
Can somebody help me?
To accomplish this you need to read the RSS of the blog, from RSS you need to read the Title and the description, after reading the whole description and title you need to trim the description to your desired number of letters. After that you need to check weather the description last word has been completed or not and then you need to remove a the last word if not completed and put the "...".
First we will make a script to trim the description and to put "..." in last:-
<?php
global $text, $maxchar, $end;
function substrwords($text, $maxchar, $end='...') {
if (strlen($text) > $maxchar || $text == '') {
$words = preg_split('/\s/', $text);
$output = '';
$i = 0;
while (1) {
$length = strlen($output)+strlen($words[$i]);
if ($length > $maxchar) {
break;
}
else {
$output .= " " . $words[$i];
++$i;
}
}
$output .= $end;
}
else {
$output = $text;
}
return $output;
}
Now we will define the variables in which we store the values:-
$xml=("http://your-blog-path/rss/");
global $item_title, $item_link, $item_description;
$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);
$x=$xmlDoc->getElementsByTagName('item');
Now, we will make an array and store values in it. I am only taking 3 because you have asked it the way. You can change it to anything (The number of post you want to show, put that in the loop)
for ($i=0; $i<3; $i++)
{
$item_title[$i] = $x->item($i)->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
$item_link[$i] = $x->item($i)->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue;
$item_description[$i] = $x->item($i)->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;
}
?>
Now echo all these values, Link is the value where your user will click and he will be taken to your blog:-
FIRST RECENT POST:
<?php echo $item_title[0]; ?>
<?php echo substrwords($item_description[0],70); ?>
SECOND RECENT POST:
<?php echo $item_title[1]; ?>
<?php echo substrwords($item_description[1],70); ?>
THIRD RECENT POST:
<?php echo $item_title[2]; ?>
<?php echo substrwords($item_description[2],70); ?>
Hope this can solve your problem. By the way Nice question.
Click here for the original documentation on displaying RSS feeds with PHP.
Django Anonymous's substrwords function is being used to trim the description and to insert the ... at the end of the description if the it passes the $maxchar value.
Full Code:
blog.php
<?php
global $text, $maxchar, $end;
function substrwords($text, $maxchar, $end='...') {
if (strlen($text) > $maxchar || $text == '') {
$words = preg_split('/\s/', $text);
$output = '';
$i = 0;
while (1) {
$length = strlen($output)+strlen($words[$i]);
if ($length > $maxchar) {
break;
} else {
$output .= " " . $words[$i];
++$i;
}
}
$output .= $end;
} else {
$output = $text;
}
return $output;
}
$rss = new DOMDocument();
$rss->load('http://wordpress.org/news/feed/'); // <-- Change feed to your site
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 3; // <-- Change the number of posts shown
for ($x=0; $x<$limit; $x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$description = substrwords($description, 100);
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<p><strong>'.$title.'</strong><br />';
echo '<small><em>Posted on '.$date.'</em></small></p>';
echo '<p>'.$description.'</p>';
}
?>
You can easily put this in a separate PHP file (blog.php) and call it inside your actual page.
Example:
social.php
<h3>Latest blog post:</h3>
<?php require 'blog.php' ?>
Also, this code is plug-n-play friendly.
Why not use the Wordpress REST API to retrieve posts -
API URL is : https://public-api.wordpress.com/rest/v1/sites/$site/posts/
where $site is the site id of your wordpress blog
or else simply use this plugin -
http://www.codehandling.com/2013/07/wordpress-feeds-on-your-website-with.html

Resources