I have created two custom post types, 'offers' and 'brands'. I have made the relationship using an advanced custom fields relationship field 'related_brand'.
Each brand uses a single post and the related posts are shown on the same page, however, when using post_class on the related posts I just noticed they are displaying the brands post classes and not the related posts classes, which are different.
How can I pull in the correct post_class for the related posts bearing in mind they are two separate post types.
The following pulls in the related posts.
$offers = get_posts(array(
'post_type' => 'offers', // name of custom post type
'meta_query' => array(
array(
'key' => 'related_brand', // name of custom field
'value' => '"' . get_the_ID() . '"',
'compare' => 'LIKE'
)
)
));
Watered down code that displays the related posts.
<?php if ($offers) : ?>
<?php foreach ($offers as $offer) : ?>
<div <?php post_class(); ?>> <!--Displaying the incorrect post classes-->
<div>
<a href="<?php the_permalink($offer->ID); ?>">
<?php echo get_the_post_thumbnail($offer->ID,); ?>
</a>
</div>
<div>
<a href="<?php the_permalink($offer->ID); ?>">
<?php echo get_the_title($offer->ID); ?>
</a>
</div>
<?php endforeach; ?>
<?php endif; ?>
In his case he is taking the post_class with the id of the current loop and not that of foreach but of the current loop of the tag.
To get it right you should use post_class with two arguments post_class ('', $ post_id); where the second is the post id. In your case the code should be + - like this:
<?php if ($offers) : ?>
<?php foreach ($offers as $offer) : ?>
<div <?php post_class('', $offer->ID ); ?>> <!--Displaying the incorrect post classes-->
<div>
<a href="<?php the_permalink($offer->ID); ?>">
<?php echo get_the_post_thumbnail($offer->ID,); ?>
</a>
</div>
<div>
<a href="<?php the_permalink($offer->ID); ?>">
<?php echo get_the_title($offer->ID); ?>
</a>
</div>
<?php endforeach; ?>
<?php endif; ?>
Related
For a special purpose, i've to create a custom post type and display it like a carousel by indicating IDS of each custom post by it's ID on my shortcode.
The problem is that when i'm using it, the last id of custom shortcode is not displayed in on the correct place, sometimes in first, sometime in last but never in the correct place( for exemple I set 3527 ID on the 4 place, its shows this prodect on the last position.
I tried to show the array of the id and it seems that there isn't any problem with (array modification etc.) the Ids are in the correct place on Array
Here is the code :
//Carousel Shortcode Produits By ID
function carousel_products_ids_shortcode( $atts ) {
$c=extract(shortcode_atts( array(
'ids'=>''
), $atts ));
$args = array(
'post_type' => 'pp_produit',
'posts_per_page' => -1,
'publish_status' => 'published',
'order'=>'ASC'
);
if(!is_null($ids) && $ids!=""){
$args['post__in']=explode(",",$ids);
print_r($args['post__in']);
}
ob_start();
$query = new WP_Query($args);
if ( $query->have_posts() ) { ?>
<div class="carousel-custom-products">
<div class="owl-carousel owl-custom-products">
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="single-produit">
<div id="produit-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php if(has_post_thumbnail()) { ?>
<div class="img-container">
<a class="thumb-produit" href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
</div>
<?php }else{ ?>
<div class="img-container">
<a href="<?php the_permalink(); ?>" class="thumb-produit">
<img src="<?php echo get_stylesheet_directory_uri()."/img/defaultpp.jpg" ?> ">
</a>
</div>
<?php } ?>
<div class="produit-single-content">
<h3><?php echo (strlen(get_the_title())>30)? substr(get_the_title(),0,30)."..." : substr(get_the_title(),0,30); ?></h3>
<?php the_excerpt(); ?>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
</div>
<?php $myvariable = ob_get_clean();
return $myvariable;
}
}
add_shortcode( 'carouselproductsids', 'carousel_products_ids_shortcode' );
Here is the shortcode :
[carouselproductsids ids="3477,3479,3481,3527,3487,3483,3485,3489,3491,3496,3493"]
For example, the ID 3257 is on the last position when the carousel is generated !
the wp_query will fetch the posts from db according to "order" arg you passed so it will check if the post id is in the array or not but it will not preserve the order of array
if you want to show the posts in specific order you can either pass some custom field (like meta key or someting) to posts to get them in the specific order or you can run loop on post_id array calling wp_query on each id
Evening all.
I need a little help. I have the following code which brings in a list of portfolios, and places them on a page in a grid format. At present the code brings in all the portfolio types (of which there are 3, divided into different categories). Is there a way I can edit the code so that I can bring in just one category type?
I have tried the following:
<?php query_posts( array( 'post_type' => 'myportfoliotype', 'category_name' => 'news','paged' => $paged, 'posts_per_page' => 12));
but it displays a blank page.
If I remove:
'category_name' => 'news',
it returns a page will all the categories listed. I have checked the category name and all is correct but I'm clearly missing something.
I would be greatful if anyone could help.
Many thanks
The full code is:
<?php /* Template Name: Modular Gallery */ ?>
<?php get_header(); ?>
<div id="folio-wrap">
<ul id="portfolio-list" class="centerrow">
<?php query_posts( array( 'post_type'=>'myportfoliotype', 'category_name' => 'news','paged' => $paged, 'posts_per_page' => 12)); if ( have_posts() ) : while ( have_posts() ) : the_post();?>
<?php $large_image=w p_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), 'fullsize', false, '' ); $large_image=$ large_image[0]; $another_image_1=g et_post_meta($post->ID, 'themnific_image_1_url', true); $video_input = get_post_meta($post->ID, 'themnific_video_url', true); ?>
<li id="post-<?php the_ID(); ?>" class="centerfourcol item_full">
<span class="imgwrap">
<?php the_post_thumbnail('folio',array('title' => "")); ?>
<a class="hoverstuff-zoom" rel="prettyPhoto[gallery]" href="<?php if($video_input) echo $video_input; else echo $large_image; ?>"><i class="icon-zoom-in"></i></a>
<a class="hoverstuff-link" href="<?php the_permalink(); ?>"><i class="icon-link"></i></a>
</span>
<div style="clear:both"></div>
<h3><?php echo short_title('...', 6); ?></h3>
<?php echo themnific_excerpt( get_the_excerpt(), '290'); ?>
</li>
<!-- #post-<?php the_ID(); ?> -->
<?php endwhile; endif; ?>
</ul>
<div class="clear"></div>
<div class="pagination">
<?php pagination( '«', '»'); ?>
</div>
<?php wp_reset_query(); ?>
</div>
</div>
<?php get_footer(); ?>
Your code should work if "news" is your category SLUG, not necessary the name.
But on some older versions of wordpress there is a bug with category_name in query_posts, try it with cat_name
i'm trying to built a custom archive page.
there is a dropdownmenu for the year, whichs displays the post count (correctly). but when i pick one year, one post (the newest) is missing.
this is my dropdown
<select name="archive-dropdown" onChange='document.location.href=this.options[this.selectedIndex].value;'>
<option value=""><?php echo esc_attr( __( 'Jahr' ) ); ?></option>
<?php wp_get_archives( array( 'type' => 'yearly', 'format' => 'option', 'show_post_count' => 1 ) ); ?>
and there is my loop
<?php while(have_posts()) : the_post(); ?>
<div <?php post_class('post-programm'); ?>>
<a href="<?php the_permalink(); ?>">
<div class="entry-content">
<h3><?php the_title(); ?></h3>
</div>
</a>
</div>
<?php endwhile; ?>
i also tried a query_posts, but in this case i get all the posts, not only for one particular year.
<?php query_posts('posts_per_page=-1');
if(have_posts()) : while(have_posts()) : the_post(); ?>
thanks in advance for any help!
Is your one missing post a custom post type? wp_get_archives doesn't work with custom posts.
I have a blog, and I have added one custom post type 'Movies'. and I have link it to my single-movies.php. and added this code:
<div id="container">
<?php
$args = array( 'post_type' => 'movies', 'posts_per_page' => 1, 'name' => $_GET['movie'] );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h1>
<a title="<?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark">
<?php the_title(); ?>
</a>
</h1>
<div class="entry-content">
<?php the_content(); ?>
</div>
</div>
<?php endwhile; ?>
</div>
<?php include(TEMPLATEPATH . '/sidebar_single.php'); ?>
<?php get_footer(); ?>
But Problem is, I'm clicking on any movie, its showing me only same movie, which i have added last on custom field Movies (i.e Avatar Movie, if I click on stargate movie, Its showing me 'Avatar' Movie link. and same others)
Please help me and change this code, if I did anything wrong here.
when you are in single-movies.php
it means you are already on the right path like /movies/avatar
so doing a simple loop
<?php while (have_posts()) : the_post(); ?>
<?php the_title(); //.... ?>
<?php endwhile; ?>
gives you all details of the avatar movie, you don't need to query, because the query is already made by the time you arrive to a SINGLE Movie page!
/movies/avatar means:
post type = movies
post name = avatar
also if you have pretty links, $_GET['movie'] it normally contains nothing.
it is prefered to use the $wp->query_vars['post_name'] to get the movie name
try just putting the loop and see what you get, if not dump the $_GET and $wp to see where you have the movie name.
you have a problem with the loop.
I would say to use a foreach...look at this example, maybe it will help you. it is exactly the same as you want to achieve.
<?php
$cat_id = $category->cat_ID; // YOU CAN CHANGE THIS OR REMOVE
global $post;
$args = array( 'category' => $cat_id, 'numberposts' => -1 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : start_wp();
?>
<li class="subcat-post-title">
<span class="trigger">
<a class="ficha" href="<?php the_permalink() ?>">
<?php echo the_title(); ?>
</a>
<?php endforeach; ?>
I would like to list all post on one page, but I would like the posts to be grouped by tags. Is this possible in Wordpress?
Basically this way:
Bikes:
Posted 2/8/2011
Some post title 1
Tag bikes
Posted 1/8/2011
Some post title 2
Tag bikes
Cars:
Posted 5/8/2011
Some post title 5
Tag cars
Posted 29/7/2011
Some post title 6
Tag cars
Boats:
Posted 30/7/2011
Some post title 4
Tag boats
Is this possible to do? It should dynamic so that I can create new tags from WP admin and they would show up automatically.
Loop though the tags using get_tags() and use get_posts with the 'tag_in' argument.
ie.
<?php foreach(get_tags() as $term){ ?>
<?php $posts = get_posts( array( 'posts_per_page' => -1, 'post_type' => 'project', 'tag__in' => $term->term_id ) ); ?>
<?php if($posts) : ?>
<h3><?php echo $term->name; ?></h3>
<?php foreach($posts as $post) : ?>
<?php setup_postdata($post); ?>
<div class="item col-sm-12">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?> <br/>
</a>
<a class="button" href="<?php the_permalink() ?>">Read More</a>
</div>
<?php endforeach ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php } ?>
It depends. Are you trying to show posts for all tags, or just listing all posts by specific tags you've already decided on?
If you know the tags you want to display, here is how you would list posts by tag.
<?php $bikePosts = new WP_Query('tag=bikes');
while ($bikePosts->have_posts()) : $bikePosts->the_post(); ?>
<h2>Bikes:</h2>
<p>Posted <?php the_time('j/m/Y'); ?> <?php the_title(); ?>
<?php endwhile;
//reset post data for next tag
wp_reset_postdata();
?>
More info: http://codex.wordpress.org/Class_Reference/WP_Query#Tag_Parameters