Wordpress Query - The Loop changing CSS for a div - wordpress

I have built a WP Loop that is limited to "2" articles that it loads. What I'd like to do is the second article in the loop or the bottom one, Not have a border-bottom on it.
How can I do this in the WP Loop?
My PHP Code is :
<div class="span content"> <!-- This is column 1 -->
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<p class=""<?php the_content(); ?>
<?php endwhile; ?>
</div><!-- /.span8 .content -->
CSS for that is :
.article {
border-bottom: 1px dotted #000000;
width: 170px;
}
Thanks

Just do it with simple CSS. Should work down to IE7.
.article {
width: 170px;
}
.article:first-child {
border-bottom: 1px dotted #000000;
}
Note: The :first-child implemtation in IE7 is a bit buggy if your first-child is a text-node like a comment.
:first-child will match Item 1
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
:first-child does not match Item 1 because the comment.
<ul>
<!-- Comment -->
<li>Item 1</li>
<li>Item 2</li>
</ul>
Workarround
<ul><!-- Comment --><li>Item 1</li>
<li>Item 2</li>
</ul>
or just strip the comment.
You could also count your iteration in the while-loop. Thats not an elegant Method but should work for you...
<?php
//Set Counter to 0
$counter = 0;
// Start Loop
if ( have_posts() ) while ( have_posts() ) : the_post();
// Increment Coounter
$counter++;
?>
<div class="span content item-<?php echo $counter; ?>">
<h2><?php the_title(); ?></h2>
<p><?php the_content(); ?></p>
</div><!-- /.span8 .content -->
<?php endwhile; ?>
Each iteration of your WP-Loop will increment the $counter by 1.
First Iteration:
<div class="span content item-1">
Second Iteration:
<div class="span content item-2">
Third Iteration:
<div class="span content item-3">
... and so on.

Related

Having an archive page with categories and tags

I'd like to have an archive (alternative to search) with Categories and Tags listed on one page. Is this possible? Can't seem to find any information on it.
Title/Archive Search<
Browse by category:
cat 1, cat 2 etc.
(Something more specific) Browse by Tags:
tag 1, tag 2 etc.
Something like this maybe?
<?php get_header(); ?>
<div id="container">
<div id="content" role="main">
<?php the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php get_search_form(); ?>
<h2>Browse by month:</h2>
<ul>
<?php wp_get_archives('type=monthly'); ?>
</ul>
<h2>Browse by category:</h2>
<ul>
<?php wp_list_categories(); ?>
</ul>
<h2>Browse by tags:</h2>
<ul>
<?php wp_tag_cloud(','); ?>
</ul>
</div><!-- #content -->
</div><!-- #container -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>

Wordpress Masonry Blog one colomn

I've been trying for hours trying to get this to work. The Masonry js seems to load but I end up getting a single column of my posts on the right. I am using Ebedly for my posts and they show up ok.
Here's my enqueue code
function enqueue_masonry_script() {
wp_enqueue_script( 'jquery-masonry' ); // adds masonry to the theme
}
add_action( 'wp_enqueue_scripts', 'enqueue_masonry_script' );
my css:
.item { width: 25%; }
and my html for the blog:
<?php get_header(); ?>
<div id="content" class="row">
<div class="responsive-title"><h1 class="hobo page-title a">NEWS</h1> </div>
<div id="masonry-wrapper" class="masonry">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="item">
<article <?php post_class(); ?> role="article">
<header>
<?php the_post_thumbnail( 'wpbs-featured' ); ?>
<div class="page-header"><h1 class="h2"><?php the_title(); ?></h1></div>
</header> <!-- end article header -->
<section class="post_content">
<?php the_content( __("Read more »","wpbootstrap") ); ?>
</section> <!-- end article section -->
<footer>
<p class="tags"><?php the_tags('<span class="tags-title">' . __("Tags","wpbootstrap") . ':</span> ', ' ', ''); ?></p>
</footer> <!-- end article footer -->
</article> <!-- end article -->
</div>
<?php if (function_exists('page_navi')) { // if expirimental feature is active ?>
<?php page_navi(); // use the page navi function ?>
<?php } else { // if it is disabled, display regular wp prev & next links ?>
<nav class="wp-prev-next">
<ul class="pager">
<li class="previous"><?php next_posts_link(_e('« Older Entries', "wpbootstrap")) ?></li>
<li class="next"><?php previous_posts_link(_e('Newer Entries »', "wpbootstrap")) ?></li>
</ul>
</nav>
<?php } ?>
<?php else : ?>
<article id="post-not-found">
<header>
<h1><?php _e("Not Found", "wpbootstrap"); ?></h1>
</header>
<section class="post_content">
<p><?php _e("Sorry, but the requested resource was not found on this site.", "wpbootstrap"); ?></p>
</section>
<footer>
</footer>
</article>
</div>
<?php endif; ?>
<!-- end #main -->
You missed out the itemSelector as suggested in masonry documentation. masonry.desandro.com/options.html
To fix this, you may replace your masonry-wrapper div with codes below:
<div id="masonry-wrapper" class="masonry js-masonry"
data-masonry-options='{ "itemSelector": ".item" }'>
There're also other options available like columnWidth where you might want to try it out. Take some time to read the documentation.

Wordpress divide posts into two divs

I want to divide word press posts into two div but its not working with query posts
how do i achive this?
first ten post in one div and other ten posts in Other div.
<div class="col6">
<ul class="que_ul clearfix">
first ten post
</ul>
</div>
<div class="col6 last">
<ul class="que_ul clearfix">
nex ten posts
</ul>
</div>
You can do something like this
<?php
global $wbdb;
$querystr = "
SELECT *
FROM $wpdb->posts
WHERE $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'faq'
LIMIT 0 , 20";
$pageposts = $wpdb->get_results($querystr, OBJECT);?>
<?php $data_name=array(); ?>
<?php foreach($pageposts as $posts):?>
<?php
$data_name[]=$posts->post_title;
?>
<?php endforeach;?>
<div class="col6">
<ul class="que_ul clearfix">
<?php for($i=0;$i<10;$i++): ?>
<?php if(!empty($data_name[$i])):?> <li><?php echo $data_name[$i]; ?></li> <?php endif;?>
<?php endfor;?>
</ul><!-- end que_ul -->
</div><!-- end col-->
<div class="col6 last">
<ul class="que_ul clearfix">
<?php for($i=10;$i<20;$i++): ?>
<?php if(!empty($data_name[$i])):?> <li><?php echo $data_name[$i]; ?></li> <?php endif;?>
<?php endfor;?>
</ul><!-- end que_ul -->
</div><!-- end col6 last -->
take a look here: http://digwp.com/2010/03/wordpress-post-content-multiple-columns/
I have used this in the past and found that
More flexible multiple columns
Is the option that works best.

Different style for each UL in Opencart Category Module

I have the following structure of categories in my Opencart
<ul>
<li class="bu">
PARENT 1
<ul>
<li>
CHILD 1
<ul>
<li> - GRANDCHILD 1</li>
<li> - GRANDCHILD 2</li>
<li> - GRANDCHILD 3</li>
</ul>
</li>
<li>
CHILD 2
<ul>
<li> - GRANDCHILD 1</li>
<li> - GRANDCHILD 2</li>
<li> - GRANDCHILD 3</li>
<li> - GRANDCHILD 4</li>
</ul>
</li>
</ul>
</li>
<li class="bu">
PARENT 2
//....ETC
How can I give Parent, Child and Grandchild different styling (color, weight, decoration)?
I was trying to make it work with JS searching for UL and applying class depending on position, but result is messy and incorrect..
Any ideas? Thanks!
The view file:
<ul>
<?php foreach ($categories as $category) { ?>
<?php if($category['children']) { ?>
<li class="bu">
<?php } else { ?>
<li class="onit" >
<?php } ?>
<?php if ($category['category_id'] == $category_id) { ?>
<a href="<?php echo $category['href']; ?>" class="active" ><?php echo $category['name']; ?></a>
<?php } else { ?>
<?php echo $category['name']; ?>
<?php } ?>
<?php if ($category['children']) { ?>
<ul>
<?php foreach ($category['children'] as $child) { ?>
<li class="onit" >
<?php if ($child['category_id'] == $child_id) { ?>
<?php echo $child['name']; ?>
<?php } else { ?>
<?php echo $child['name']; ?>
<?php } ?>
</li>
<?php } ?>
</ul>
<?php } ?>
</li>
<?php } ?>
</ul>
You could search and modify the php file that generates this form. Than complete the code with some css classes.
Answer for edited question:
If you watch the href you can see that the path part has the same number of variables in the same generation:
Parent: path=205
Child: path=205_66
Grandchild: path=205_66_230
You could get this part of the text, and depending on the variable number do the formating. So condition would look like this:
if(count(explode("_",strstr($child['href'],"path=")))==1)
//Do the formating for parent.
Use strstr() to delete before the path= part. You can leave out this if there aren't '_' characters before path=.
With explode() you can split the string to an array with the '_' character.
Than count() will count the elements of the array.
Compare with 1 for parents, 2 for childs and with 3 for grandchilds.
Otherway I assume by rewriting the code there should be a better way to reach this goal.
I think you could maybe do it like this:
ul li {
color:red;
}
ul li > ul li {
color:blue;
}
ul li > ul li > ul li {
color:green;
}

WordPress query posts into two Divs

I want to display my wordpress posts in a category in two divs. So for example:
<ul id="left">
<li class="post">POST 1</li>
<li class="post">POST 3</li>
<li class="post">POST 5</li>
<li class="post">POST 7</li>
</ul>
<ul id="right">
<li class="post">POST 2</li>
<li class="post">POST 4</li>
<li class="post">POST 6</li>
<li class="post">POST 8</li>
</ul>
So want I need to do is tell the query_posts to somehow start spitting out the first 4 posts oddly and then evenly for each div. I don't want to have two seperate WP_Queries as this is a category.php file and needs to have the default loop. Not quite sure how to do this.
Any help much appreciated.
I have not test this before, this is not the best way but a solution
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php
$count++;
if( $count % 2 ) {
$left_array[] = array( 'content' => get_the_content('more...') );
}
else {
$right_array[] = array( 'content' => get_the_content('more...') );
}
?>
<?php endwhile; ?>
<ul id="left">
<?php
foreach( $left_array as $post ) {
echo '<li class="post">'.$post['content'].'</li>';
}
?>
</ul>
<ul id="right">
<?php
foreach( $right_array as $post ) {
echo '<li class="post">'.$post['content'].'</li>';
}
?>
</ul>
<?php else : ?>
<?php endif; ?>
or the same idea, but another way:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<ul id="left">
<?php
$count++;
if( $count % 2 ) {
}
else {
?>
<li class="post"><?php the_content('more...'); ?></li>
<?php
}
?>
</ul>
<ul id="right">
<?php
$count++;
if( $count % 2 ) {
?>
<li class="post"><?php the_content('more...'); ?></li>
<?php
}
?>
</ul>
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
How about preconstructing the two lists: (I can't recall the WP query syntax, so it's pseudo-PHP:)
<?php
$list1 = array();
$list2 = array();
$i=0;
foreach($query_results as $res) {
if(($i++)&1) $list2[] = $res;
else $list1[] = $res;
}
?>
Now list1 contains the first, third, ... item and list2 contains the second, fourth, ...
You can then print them in the two divs as you wish.
(On a tangent: Does PHP have any succinct way to do what the above code does? Python has the stepped slicing syntax...)
If your goal is to have a two-column list of posts, it would be much easier to just output the posts in one list, and then use CSS to give the visual appearance of two columns using float, for instance:
width: 50%;
float: left;

Resources