960gs different classes on teasers posts - wordpress

I'm having hard time trying to use teasers post in my wordpress theme (based on 960gs), as you can see here http://img17.imageshack.us/img17/794/schermata20110420a15045.png what I got till now is one "featured" post and three teasers post with thumbnails that will probably be six (so it'll have seven posts displaied in the homepage). The problem is that to do so I have to assign a class "grid_2 alpha" to the teasers post and I don't know how to assign this class to just the first teaser on the left, lefting the other ones with no alpha or omega class and putting the omega class to just the last teaser post (the seventh).
If can help, here's the code I'm using for the loop:
<?php $firstClass = 'firstpost'; ?>
<?php /* Start loop */ ?>
<?php while (have_posts()) : the_post(); ?>
<?php if (function_exists('yoast_breadcrumb')) { if (is_page() && $post->post_parent) { yoast_breadcrumb('<p id="breadcrumbs">','</p>'); } } ?>
<div class="post <?php echo $firstClass; ?>">
<?php $firstClass = 'grid_2 alpha'; ?>
<img src="<?php echo get_post_meta($post->ID, "Thumbnail", true);?>" width="140" height="100" style="padding-bottom:20px;" />
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php wp_link_pages(array('before' => '<nav id="page-nav"><p>' . __('Pages:', 'roots'), 'after' => '</p></nav>' )); ?>
</div>
<?php endwhile; // End the loop ?>
also I would like to know how I can add some text above the teaser section under the first featured post. Sorry for the too many questions and for my bad english, as you can understand I'm not a developer but I searched for one week and couldn't find anything helpful for my problems. Thanks in advance for any help, I really appreciate it.

<?php $count = 0; ?>
<?php /* Start loop */ ?>
<?php while (have_posts()) : the_post(); ?>
<?php $count++; ?>
<?php if ($count == 1):
$class = "firstpost';
elseif ($count == 2):
$class = "grid_2 alpha";
elseif ($count == $wp_query->post_count):
$class = "grid_2 omega";
else:
$class = "grid_2";
endif;
?>
<?php if (function_exists('yoast_breadcrumb')) { if (is_page() && $post->post_parent) { yoast_breadcrumb('<p id="breadcrumbs">','</p>'); } } ?>
<div class="post <?php echo $class; ?>">
<img src="<?php echo get_post_meta($post->ID, "Thumbnail", true);?>" style="padding-bottom:20px;" />
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php wp_link_pages(array('before' => '<nav id="page-nav"><p>' . __('Pages:', 'roots'), 'after' => '</p></nav>' )); ?>
</div>
<?php endwhile; // End the loop ?>

Related

Need to Remove Duplicate Posts in a Foreach Loop in WordPress code

I am currently looping an open position (post type) and cities in a state (position-location). The problem that I'm having is the position locations if there are more than two are duplicating the open position:
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php $termsCo = get_the_terms($post->ID, 'position-location' );
foreach ($termsCo as $termCo) :
$locationCo = $termCo->name;
$parentCo = $termCo->parent;
if ($parentCo === 95) : ?>
<a class="ui grid segment" href="<?php echo get_permalink(); ?>">
<div class="twelve wide column">
<?php echo the_title(); ?>
</div>
<div class="four wide column">
<?php foreach ($termsCo as $location) : ?>
<?php if ( $location->name === 'Fort Collins' || $location->name === "Western Colorado" ) : ?><span class="comma"><?php echo $location->name; ?></span><?php endif; ?>
<?php endforeach; wp_reset_postdata(); ?>
</div>
</a>
<?php endif; endforeach; ?>
<?php endwhile; wp_reset_postdata(); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
See visual here: https://web.jub.com/open-positions/
You have two foreach loops (one in line 4 and one in line 13). If I understand correctly, the problem is that the first loop should only enter the if part once per post ID. Have you tried a break statement in the end of your if?
Namely line 18:
<?php break; endif; endforeach; ?>

Wordpress post object getting page title rather than object title

I have an ACF repeater, which repeats post objects. I am changing the postdata to the post object rather than the page so that i can get the title and thumbnail image. This works perfectly for the first one, however the subsequent objects pull the correct thumbnail but the title is pulled from the page title.
Heres the code:
<?php if( have_rows('service_repeater') ): ?>
<?php while ( have_rows('service_repeater') ) : the_row(); ?>
<?php $post_object = get_sub_field('service'); ?>
<?php if( $post_object ): ?>
<?php $post = $post_object; ?>
<?php setup_postdata( $post ); ?>
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
<a href="<?php the_permalink(); ?>" class="service">
<div class="background" style="background-image: url('<?php echo $url; ?>');"></div>
<div class="content">
<h3><?php the_title(); ?></h3>
<p><?php the_field('read_more_text'); ?></p>
</div>
</a>
<?php unset($post_object, $post); ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php endwhile; ?>
Any help would be greatly appreciated!
Ok, so i removed the <?php unset($post_object, $post); ?> and it works now. Im concerned that this will give me trouble later down the line with variables however.

Custom first post on home page wordpress

I need to change this code so the first post displays with the class "first" and also has the firsttop class along with firstbottom. The rest of the inside pages eg page/2/ , page/3/ and so on will not have this class. I have tried many things please help not sure whats wrong here.
<?php
if (have_posts()) :
$count = 0;
while (have_posts()):
the_post();
if (get_post_type() == 'post'):
?>
<?php
if (!is_single() && $count == 0):
?>
<div class="firsttop<?php echo !is_home() ? "notop" : "" ?>"></div>
<?php endif; ?>
<article class="post <?php echo !is_single() ? "preview" : "" ?> <?php echo $count == 0 ? "first" : "" ?> <?php echo !is_home() ? "full" : "" ?>">
<p class="byline">
<?php the_time('F j, Y'); ?>
</p>
<h3>
<?php the_title(); ?>
</h3>
<div>
<?php the_content('<span class="more">Read More...</span>'); ?>
</div>
<?php include ('post-info.php'); ?>
</article>
<?php
if (!is_single() && $count == 0):
?>
<div class="firstbottom<?php echo !is_home() ? "nobottom" : "" ?>"></div>
<?php endif; ?>
<?php
endif;
$count++;
endwhile;
endif;
?>
Have you tried to use a filter for this?
Read more about wordpress add_filter here http://codex.wordpress.org/Function_Reference/add_filter
You can make a new plugin or add it to your functions.php file in your theme directory.
This guy did it with post_class, but it might not work for you if you didn't use the post_class in your posts loop page. http://wpsnipp.com/index.php/functions-php/add-class-to-first-post-in-the-loop/
Here's a snippet of his code:
add_filter( 'post_class', 'wps_first_post_class' );
function wps_first_post_class( $classes ) {
global $wp_query;
if( 0 == $wp_query->current_post )
$classes[] = 'first';
return $classes;
}

Group Posts by Category in Monthly Archive

I am trying to group the posts by Category in a Monthly archive but don't know how to achieve that.
Can someone here help me please.
Regards
This is the php code I am using in archive.php
<?php
$terms = get_terms("publicationcat");
$count = count($terms);
if ( $count > 0 ){
foreach ( $terms as $term ) {
echo "<div class=\"publication-area\">";
echo '<h3 class="term-heading">' . $term->name . '</h3>';
echo "<div class=\"publication-txt\">";
echo "<ul>";
while ( have_posts() ) : the_post();
echo "<li><a target=\"_blank\" href=\"" . wp_get_attachment_url(get_post_meta($post->ID, 'document_file_id', true)) . "\">".$post->post_title."</a></li>";
endwhile;
echo "</ul>";
echo "</div>";
echo "</div>";
}
}
?>
The only problem is that its displaying same post titles for all the terms..
When you get to looping through the post in each category, you are not finding only the posts related to that category, and are instead looping through EVERY post. The below (untested, so comment if you get errors) should sort your problem.
Please note however that you may still get some repetition, if a post is assigned to more than one category.
Check each post to ensure it is in the desired category. Good point - only one query. Bad point - loops through all posts for every cat.
<?php
$terms = get_terms('publicationcat');
if(!empty($terms)) : foreach($terms as $term) :
?>
<div class="publication-area">
<?php sprintf('<h3 class="term-heading">%1$s</h3>', $term->name); ?>
<div class="publication-txt">
<?php if($my_query->have_posts()) : ?>
<ul>
<?php if(in_category($term->slug)) : ?>
<?php while($my_query->have_posts()) : $my_query->the_post(); ?>
<?php $link = wp_get_attachment_url(get_post_meta($post->ID, 'document_file_id', true)); ?>
<li><a target="_blank" href="<?php echo ($link !== '') ? $link : ''; ?>">
<?php the_title(); ?>
</a></li>
<?php endwhile; ?>
<?php endif; ?>
</ul>
<?php endif; ?>
</div>
</div>
<?php
endforeach;
endif;
?>
Re-query the posts and output all that are grabbed. Good point - only pulls only the posts required for the category being looped. Bad point - lots of queries, so many queries could slow your site down.
<?php
$terms = get_terms('publicationcat');
if(!empty($terms)) : foreach($terms as $term) :
?>
<div class="publication-area">
<?php sprintf('<h3 class="term-heading">%1$s</h3>', $term->name); ?>
<div class="publication-txt">
<?php
$args = array(
'cat' => $term->term_id
);
$my_query = new WP_Query($args);
?>
<ul>
<?php if($my_query->have_posts()) : while($my_query->have_posts()) : $my_query->the_post(); ?>
<?php $link = wp_get_attachment_url(get_post_meta($post->ID, 'document_file_id', true)); ?>
<li><a target="_blank" href="<?php echo ($link !== '') ? $link : ''; ?>">
<?php the_title(); ?>
</a></li>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
</ul>
</div>
</div>
<?php
endforeach;
endif;
?>

Wordpress: category page not for post's

Please at first let me explain my question. I use wordpress to create web sites for flash games, so I don't have certain page for post's. I add each game by
<code>
post-new.php?post_type=game
</code>
and u can see it's not the regular post for wordpress.
I try to use this code from codex:
<code>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$i = 0;
$loop = new WP_Query(array('post_type' => 'game', 'post_per_page' => 5 ));
while ($loop->post_type()) : $loop->game();
?>
</code>
<code>
<?php if ( in_category('') ) { ?>
<div class="post-cat-three">
<?php } else { ?>
<div class="post">
<?php } ?>
<h2><?php the_title(); ?></h2>
<small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>
<div class="entry">
<p>Category: <?php single_cat_title(); ?></p>
</div>
<p class="postmetadata">Posted in <?php the_category(', '); ?></p>
</code>
I think it really have to works for posts, but in such case I try to change post for games, try many ways, but don't sucseed yet.
Could anyone tell me what I have change in this code?
i think that promblem in the begining with 'have post' and 'the loop'.
Thanks.
I hope this will help. This is from my WordPress custom post type (loop):
<?php query_posts('post_type=clients&showposts=1000');
if (have_posts()) : while (have_posts()) : the_post();
$nameofclient = get_post_meta($post->ID,'name_of_client',true);
$clientcompany = get_post_meta($post->ID,'company_of_client',true);?>
<div <?php post_class();?> id="ka-<?php the_ID(); ?>">
<h2 class="categorytitle"><?php the_title(); ?></h2>
<?php the_content(); ?><p class="ats_autors">/ <?php if($nameofclient): echo '<span class="client">'.$nameofclient.'</span>'; endif; if($clientcompany): if($nameofclient){echo ', ';} echo '<span class="client-company">'.$clientcompany.'</span>'; endif; ?></p></div><?php endwhile; endif;wp_reset_query();?>
Just found one mistake: in_category('') MUST be filled with category ID in slug. http://codex.wordpress.org/Function_Reference/in_category#Parameters
Correct would be in_category('some-game-cat-slug')
plus worth to read http://new2wp.com/noob/query_posts-wp_query-differences/

Resources