Wordpress: Use different loops - php

I'm making an online magazine. Right now I'm trying to show the posts I'm posting. I've managed to do that, but what I want is that if a post is categorized in "Streetstyle", the loop will be completely different then if it's categorized in "Photography". I've mangaged to do this with one category, how can I do the same with other categories, and style it another way? I tried using the same code as <?php if (is_category( 'Streetstyle' ) || in_category( 'Streetstyle' ) ) { ?>, but then the theme just gets bugged and the posts shows up twice. Do you know of a better way to do this?
This is my code:
<?php query_posts('posts_per_page=9' . '&orderby=date');
while ( have_posts() ) : the_post(); ?>
<?php if (is_category( 'Streetstyle' ) || in_category( 'Streetstyle' ) ) { ?>
<div <?php post_class('pin streetstyle'); ?>>
<a href="<?php the_permalink(); ?>">
<div class="heading">
<h1>Fashion</h1>
</div>
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?>
<div class="heading">
<h1><?php the_title(); ?></h1>
</div>
</a>
</div>
<?php } else { ?>
<div <?php post_class('pin'); ?>>
<h1>
<?php the_title(); ?>
</h1>
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
the_content('Les mer <br><br>'); ?>
</div>
<?php } ?>
<?php endwhile;
// Reset Query
wp_reset_query(); ?>

If you have a lot of categories I would recommend using switch, it will save you time and your code will look a lot cleaner.
http://php.net/manual/en/control-structures.switch.php

I've slimmed this down a bit, but this is probably more what you're after
<?php query_posts('posts_per_page=9' . '&orderby=date');
while(have_posts()) : the_post(); ?>
<?php if(is_category('Streetstyle') || in_category('Streetstyle')) : ?>
<div <?php post_class('pin streetstyle'); ?>>
<a href="<?php the_permalink(); ?>">
<div class="heading">
<h1><?php the_title(); ?></h1>
</div>
</a>
</div>
<?php if(has_post_thumbnail()) the_post_thumbnail(); ?>
<?php else : ?>
<div <?php post_class('pin'); ?>>
<h1><?php the_title(); ?></h1>
<?php
if(has_post_thumbnail()) the_post_thumbnail();
the_content('Les mer <br><br>');
?>
</div>
<?php endif; ?>
<?php endwhile; wp_reset_query(); ?>

Related

How do i only display posts with a certain category?

So I'm building a website for my new business.
An I want to only run the following code if the post have a category of 'certain-category'.
How do I do this? I tried a number of things. but they do not work..``
<article id="post-<?php the_ID(); ?>" <?php post_class(''); ?>>
<?php if ( has_post_thumbnail() ) : ?>
<div class="entry-thumb">
<a href="<?php the_permalink(); ?>" class="H1-posts" title="<?php the_title(); ?>" >
<?php the_post_thumbnail('moesia-thumb'); ?>
</a>
</div>
<?php endif; ?>
<?php if (has_post_thumbnail()) : ?>
<?php $has_thumb = ""; ?>
<?php else : ?>
<?php $has_thumb = ""; ?>
<?php endif; ?>
<div class="post-content <?php echo $has_thumb; ?>">
<header class="entry-header">
<?php the_title( sprintf( '<h1 class="entry-title">', esc_url( get_permalink() ) ), '</h1>' ); ?>
<?php if ( 'post' == get_post_type() ) : ?>
<?php endif; ?>
</header><!-- .entry-header -->
<div class="entry-summary">
<?php if ( (get_theme_mod('full_content') == 1) && is_home() ) : ?>
<?php the_content(); ?>
<?php else : ?>
<?php the_excerpt(); ?>
<?php endif; ?>
</div><!-- .entry-content -->
Use has_category()
if(has_category('certain-category', get_the_ID())):
// Do something
endif;
WordPress now has a native block which does this for you if you want the easy route the block is called Post and Page Grid it allows you to select what category of posts or pages it will show and you can select what information is shown e.g. Title, thumbnail, excerpt etc

How to target a specific query from functions.php in Wordpress

I'm trying to use offset on a specific WP_Query on a page that has a total of 3 Loops with the code described here.
For now, this works but I can only target all three loops. What (I think) I have to do is change this part:
//Before anything else, make sure this is the right query...
if ( ! $query->is_posts_page ) {
return;
}
To something like this:
//Before anything else, make sure this is the right query...
if ( ! $query->$THELOOPIWANTTOTARGET ) {
return;
}
Is there a way to do this?
The query:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$mainloop = new WP_Query(array('posts_per_page' => 3, 'paged' => $paged));
?>
<div class="full padding center isotope">
<?php if ( $mainloop->have_posts() ) : while ( $mainloop->have_posts() ) : $mainloop->the_post(); ?>
<article class="post">
<?php the_post_thumbnail(); ?>
<div class="textwrap">
<h2><?php the_title(); ?></h2>
<?php if ( get_field('bron')): ?>
<?php if ( get_field('bron')): ?><a href="<?php the_field('bron_link'); ?>" target="_blank" class="source"><?php endif; ?>
<?php the_field('bron'); ?>
<?php if ( get_field('bron')): ?></a><?php endif; ?>
<?php endif; ?>
<?php the_excerpt(); ?>
</div>
<div class="readmore">
<div class="fa fa-share share" id="<?php the_ID(); ?>">
<div class="share_block"><div class="addthis_sharing_toolbox" data-url="<?php the_permalink(); ?>"></div></div>
</div>
Lees verder
</div>
</article>
<?php endwhile; ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</div>

Marketpress Productlist loop fetching only one product

this is my mp_productlist.php
<?php get_header();?>
<div class="content_mid">
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="post thin">
<a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<h2><?php the_title(); ?></h2></a>
<?php if ( function_exists( 'add_theme_support' ) ) the_post_thumbnail(); ?>
<h3><?php mp_product_price(true) ?></h3><?php mp_buy_button(true, 'single')?>
</div>
<?php endwhile;?>
<?php else : ?>
<?php endif; ?>
<!--end post-->
</div>
<?php get_footer();?>
and this is what I'm getting http://angelov.ga/old/store/products/
it's only fetching one products instead of the two test products I have
thanks in advance
Nevermind, I just did a simple workaround and categorized everything in the category ALL and I have subcategories for each supplement thing and changed the BROWSE PRODUCTS thing to /category/all

How do I remove the most recent post from the sidebar in Wordpress?

On both the front page and the blog page - the sidebar shows the most recent post, which I find doesn't look very good duplicated against the same post expanded on the main page.
This is my code for the sidebar:
<div class="blog-sidebar">
<?php query_posts('showposts=5'); ?>
<?php while (have_posts()) : the_post(); ?>
<div class="blog-sidebar-feature">
<?php if ( has_post_thumbnail() ) { ?>
<div class="blog-sidebar-image"><?php the_post_thumbnail('medium'); ?></div>
<?php
}
?>
<div class="blog-sidebar-content">
<p class="date"><?php the_time('F j, Y') ?></p>
<h3 <strong><?php
foreach((get_the_category()) as $category) {
echo $category->cat_name . ' ';
}
?></strong></h3>
<h2 <p><a href="<?php the_permalink() ?>" rel="bookmark" title=""><?php the_title();
?></a></p></h2><?php echo get_excerpt(166); ?>
</div>
</div>
<?php endwhile;?>
<br />
<?php wp_pagenavi(); ?>
</div>
and the relevant code for how the blog appears on the home page:
<div class="blog-sidebar">
<div class="blog-sidebar-feature">
<?php query_posts('orderby=date&order=DESC&showposts=2'); ?>
<?php while (have_posts()) : the_post(); ?>
<?php if ( has_post_thumbnail() ) { ?>
<div class="blog-sidebar-image"><?php the_post_thumbnail('medium'); ?></div>
<?php
}
?>
<div class="blog-sidebar-content">
<p class="date"><?php the_time('F j, Y') ?></p>
<h3 <strong><?php
foreach((get_the_category()) as $category) {
echo $category->cat_name . ' ';
}
?></strong></h3>
<h2 <p><a href="<?php the_permalink() ?>"
rel="bookmark" title=""><?php the_title(); ?></a></p></h2><?php echo get_excerpt(166); ?>
</div>
<?php endwhile;?>
</div>
</div>
<div id="connect">
<?php query_posts('page_id=1');
while (have_posts()): the_post();
the_content();
endwhile;
wp_reset_query(); ?>
</div>
Is there any way to remove only the most recent post from the sidebar when it appears in full on the main container? Thanks in advance for any help.
UPDATE V2
So you do want recent posts, just not the post currently showing in the main content.
UPDATE V3:
This should work now. I had to change arguments of query_posts to array to make it work.
Try it now:
<?
global $wp_query;
$skip_posts=array();
if (is_single()) //only exclude posts when single post is shown
$skip_posts[]=$wp_query->post->ID;
?>
<?php query_posts( array( 'showposts'=>5,'post__not_in'=>$skip_posts)); ?>
<?php query_posts('posts_per_page=5&offset=1'); ?>
Thanks to 850010 for all the help, I went back and had a look at the offset rule and the 'array' wasn't needed. Deceptively simple.

How to view three different category of post in three different columns in wordpress?

I am really new in 'WordPress' and current facing a problem that is I am using FUNDA theme and try to show the three different category of post in three different column depending on category ID but can not find the post for a specific category.
I try this
<?php
$data = cats_to_select();
$cat_id = $data[1][value];
global $cat_id;?>
<?php print_r($cat_id);?>
<?php if($cat_id==3):?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<?php if ( function_exists("has_post_thumbnail") && has_post_thumbnail() ) { the_post_thumbnail(array(260,200), array("class" => "alignleft post_thumbnail")); } ?>
<h2 class="title"><?php the_title(); ?></h2>
<div class="postdate"><img src="<?php bloginfo('template_url'); ?>/images/date.png" /> <?php the_time('F jS, Y') ?> <img src="<?php bloginfo('template_url'); ?>/images/user.png" /> <?php the_author() ?> <?php if (current_user_can('edit_post', $post->ID)) { ?> <img src="<?php bloginfo('template_url'); ?>/images/edit.png" /> <?php edit_post_link('Edit', '', ''); } ?></div>
<div class="entry">
<?php the_content('<strong>Read more »</strong>'); ?>
</div>
</div><!--/post-<?php the_ID(); ?>-->
<?php endwhile; ?>
<div class="navigation">
<?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } else { ?>
<div class="alignleft"><?php next_posts_link('« Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries »') ?></div>
<?php } ?>
</div>
<?php else : ?>
<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>
<?php get_search_form(); ?>
<?php endif; ?>
<?php endif; ?>
Can any one help me? Thanks in advance.
<?php query_posts('cat=3'); ?> // here, 3 will be replaced with desired
// category id, or you may add array
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<?php if ( function_exists("has_post_thumbnail") && has_post_thumbnail() ) { the_post_thumbnail(array(260,200), array("class" => "alignleft post_thumbnail")); } ?>
<h2 class="title"><?php the_title(); ?></h2>
<div class="postdate"><img src="<?php bloginfo('template_url'); ?>/images/date.png" /> <?php the_time('F jS, Y') ?> <img src="<?php bloginfo('template_url'); ?>/images/user.png" /> <?php the_author() ?> <?php if (current_user_can('edit_post', $post->ID)) { ?> <img src="<?php bloginfo('template_url'); ?>/images/edit.png" /> <?php edit_post_link('Edit', '', ''); } ?></div>
<div class="entry">
<?php the_content('<strong>Read more »</strong>'); ?>
</div>
</div><!--/post-<?php the_ID(); ?>-->
<?php endwhile; ?>
That code only shows one category. You would need to add an array of the category ids and possible use a for loop to multiply the code, instead of the if statement. Maybe something like that.
Then make a bit of css that would divide the content into different boxes? I'm not sure what you mean by three columns. Also not sure what the FUNDA theme is.
It would be easier if you could give us a better idea of what you're using. Link?

Categories