HI I have added a loop code to a WP page to display a list of thumb of posts.
It works fine, but it shows each post I have published, even if isn't in the categories choosen. It's a problem because some posts shouldn't be showed!
Could you help me please?
<?php $posts = get_posts('category=Products&numberposts=-1');
foreach($posts as $post) : setup_postdata($post);
?><li><div class="fotoBoxContent"><a class="fotoBox" href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); the_title(); ?></a></div></li>
<?php endforeach; ?>
Posts of category "Products" are showed, posts of categories child of "Products" are showed too, but are showed posts of other categories including "uncategorized".
PS: is printed, below the title of the page, "cat : 11,10,13,14,16,9,12, | " there is method to avoid this printing? thanks to everyone
Is "Products" the actual slug of the category you're trying to grab? I would double-check that. You want the category's slug, not its display name.
The category has to be the ID not the name. Also, try setting up the get_posts() using an arguments array instead of doing it inline:
<?php
$args = array(
'category' => '*ID*',
'numberposts' => -1
);
$posts = get_posts($args);
foreach($posts as $post) : setup_postdata($post); ?>
<li>
<div class="fotoBoxContent">
<a class="fotoBox" href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); the_title(); ?>
</a>
</div>
</li>
<?php endforeach; ?>
From WordPress Codex - Note: The category parameter needs to be the ID of the category, and not the category name.
http://codex.wordpress.org/Template_Tags/get_posts
Related
I am very new to wordpress theme development and I have created a custom post and custom taxonomy . I have created a image acf on taxonomy now I want to show all the taxonomy term and their acf fields on front-page . I can get all term by using get_terms() function but I don't know how to get acf field of that taxonomy.
$terms = get_terms(array(
"taxonomy" => "categories",
"hide_empty" => false ));
foreach($terms as $term):
echo $term->name;
endforeach;
I want term name and image acf field of that term on front-page.php . Any advice will be helpful and thanks in advance.
Please try this code
<?php
$terms = get_the_terms(get_the_ID(), "categories");
if (!empty($terms)): ?>
<ul>
<?php foreach ($terms as $term): ?>
<li class="<?php echo $term->slug; ?>">
<img src="<?php the_field("image_field_name", $term); ?>" />
</li>
<?php endforeach; ?>
</ul>
<?php endif;
?>
For more details you can check this documentation.
I am looking to feed a custom post type onto a custom template. I basically want a few sub sections with custom (seperate) post types on the backend panel feed out onto a page, like a news, blog, testimonials kind of thing.
I think have the right idea but I believe I am going around this wrong. Please forgive me, as I do not normally work on wordpess. I basically want (let's say the news page) to feed all the posts in the custom post type "news". I have a blog page linked to a template that looks like so -
<?php
/*
Template Name:News*/
?>
<?php get_header(); ?>
<ul id="News">
<?php global $post; query_posts( 'post_type=newst&orderby=ID&order=desc' ); while (have_posts()) : the_post(); ?>
<li>
<div class="fea_del">
<h1><?php the_title(); ?></h1>
<p><?php the_field('post_content',$post->ID); ?></p>
<a <?php $p=get_permalink( $post->ID ); ?> href="<?php echo $p; ?>"class="entire_job">SEE ENTIRE ARTICLE</a>
</div>
</li>
<?php endwhile; wp_reset_query(); ?>
</ul>
<?php get_footer(); ?>
I have the [display-posts] plugin in my blog page and just have [display-posts] in the body in hopes it would just feed in all the posts in blog. I've been banging my head against this for a while with no success, I don't work in wordpress much so I'm a bit in the dark here.
You have to pass the $args array to WP_Query, try:
$args = array( 'post_type' => 'news', 'order' => 'desc' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
//rest of you code
endwhile;
I want to have a page that shows all posts, separated by category. The idea is to get the categories, and then iterate through all posts for each category. The problem is complicated by the fact that I want to iterate through all posts of a given custom type, using a custom taxonomy as the categories. (Running Wordpress 3)
In my functions.php, my custom post type is registered as "video" and the custom taxonomy as "video_types".
In my custom page template that is supposed to show all videos arranged by category, this is the code that isn't returning any posts (and they're there, I checked):
<?php
$categories = get_categories(array(
'taxonomy' => 'video_types'
));
foreach ($categories as $cat):
?>
<section id="<?php $cat->slug ?>" class="video-category">
<?php
query_posts(array(
'cat' => $cat->cat_ID,
'posts_per_page' => -1
));
?>
<h2><?php single_cat_title(); ?></h2>
<p class="description"><?php echo category_description($cat->cat_ID); ?></p>
<?php while (have_posts()) : the_post(); ?>
<?php
$category = get_the_category();
echo $category[0]->cat_name;
?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<article class="video">
<h3><?php the_title(); ?></h3>
<p>
<?php the_content() ?>
</p>
</article>
<?php endwhile; ?>
</section>
<?php endforeach; ?>
Jeez, once you figure out that each item of a custom taxonomy is called a term (not immediately obvious in the wordpress docs for the noob), its all much simpler to search for. This solution is easier to understand without all the custom query stuff.
<?php
// A term is an item of a taxonomy (e.g. "Promotional" could be a term for the taxonomy "video_type")
// ...so $categories could be $terms and it would still make sense
$categories = get_terms('taxonomy_name');
foreach( $categories as $category ):
?>
<section class="category-<?php echo $category ?>">
<h2><?php echo $category->name; // Print the cat title ?></h2>
<p class="description"><?php echo $category->description ?></p>
<div class="<?php echo $category->post_type ?>-list">
<?php
//select posts in this category (term), and of a specified content type (post type)
$posts = get_posts(array(
'post_type' => 'custom_post_type_name',
'taxonomy' => $category->taxonomy,
'term' => $category->slug,
'nopaging' => true, // to show all posts in this category, could also use 'numberposts' => -1 instead
));
foreach($posts as $post): // begin cycle through posts of this category
setup_postdata($post); //set up post data for use in the loop (enables the_title(), etc without specifying a post ID)
?>
// Now you can do things with the post and display it, like so
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h3><?php the_title(); ?></h3>
<?php
// Getting custom field data example
echo get_post_meta($post->ID, 'field_key', true);
?>
<?php the_content() ?>
</article>
<?php endforeach; ?>
</div>
</section>
<?php endforeach; ?>
And then any gaps in understanding can be filled by searching the functions above in the wordpress codex. In the above code, for my specific application, custom_post_type_name would be video, and taxonomy_name would be video_type (or video_types, I forget).
You might try another approach. Try using get_posts to get your posts sorted by your custom taxonomy, set up a variable that is initially an empty string (called $current_cat or something), and with each loop of the results, check the taxonomy and compare it with $current_cat - if different, print out a header for the new category and then the entry, if the same, print out an entry as usual.
A clear issue with your code (I believe) is that you're not properly querying your custom taxonomy. You should be using simply taxonomy_name => 'value' within your query, a custom taxonomy won't be touched by cat in a query.
Let me know if you need more detail.
edit: More detail!
// get a list of categories, in this case your custom taxonomy (your_taxonomy_name)
$querystr = "SELECT terms.* FROM $wpdb->term_taxonomy tax LEFT JOIN $wpdb->terms terms ON tax.term_id = terms.term_id WHERE tax.taxonomy = 'your_taxonomy_name'";
$categories = $wpdb->get_results($querystr, OBJECT);
foreach( $categories as $category ): // begin a loop through those terms (categories in your custom taxonomy)
echo '<div class="category-header"><h3>'.$category->name.'</h3>'; // print the cat title
echo '<p class="category-description">'.strip_tags(term_description($category->term_id,'your_taxonomy_name')).'</p></div>'; // cat description
$posts = get_posts( array( 'your_taxonomy_name' => $category->name, 'post_type' => 'your_post_type' ) ) //select posts in this category, and of a specified content type
foreach($posts as $post) : // begin cycle through posts of this category
setup_postdata($post); //set up post data for use in the loop (enables the_title(), etc without specifying a post ID)
[ ... ] // do things with your post (display it)
endforeach;
endforeach;
That should do it - and this may be helpful for using get_posts.
I have a custom post type with multiple taxonomy types. The issue is focused around just one of them.
I need to display all custom posts that have a taxonomy from featured-vendors checked. Right now, there is just one "featured" , but there may be more in the future, such as "highlight" or "sponsor" or something alone those lines.
But, for now, I just need to go through all the "vendors" custom post type, find ones with "featured" checked inside of the "featured-vendors" taxonomy.
I have some some posts out there that state it's not possible, but they were either from 2.8 or from the very first of this year and I know WordPress has released at least one update since then.
Thanks in advance!!
Query Custom Post Types by Taxonomy Term
This sample will assume:
the custom post type was registered with the taxonomy and the taxonomy was registerd with 'query_var' =>true and 'hierarchial' => true
The "checked" term will be the parent and any new ones can be added later on as children.
The Code:
<?php query_posts( array( 'featured-vendors' => 'checked' ) ); ?>
<?php if( is_tax() ) {
global $wp_query;
$term = $wp_query->get_queried_object();
$title = $term->name;
} ?>
<div class="my-custom-post">
<h3><?php echo($title); ?></h3> //this will show the name of the post type
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="entry"> <h3><?php the_title(); ?></h3>
<?php the_excerpt(); ?> //shows the excerpt
</div><!--/end entry-->
</div><!--/end post-->
<?php endwhile; else: ?>
<p><?php _e('No Post Found','your_theme_text_domain'); ?></p>
<?php endif; ?>
i'm trying to create a portfolio website using wordpress,
each post has view costum fields, one of which is called type - with the value of "featured" or "not-featured"
now when user clicks on the post title - they go the the single.php to see the entire post, here i would love to display all featured thumbnails
i tried this
<?php while ( have_posts() ) : the_post() ?>
<?php if(get_post_meta($post->ID, 'type', true) == "featured") {; ?>
<h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( __('Permalink to %s', 'your-theme'), the_title_attribute('echo=0') ); ?>" rel="bookmark">
<img src="<?php echo get_post_meta($post->ID, 'intro_thump', true); ?>" alt="Icon for Post #<?php the_ID(); ?>" />
</a></h2>
<?php }; ?>
<div class="entry-content">
</div><!– .entry-content –>
<?php endwhile; ?>
(THIS CODE IS SIMILAR TO THE CODE I USE AT INDEX.PHP AND THERE IT DOES WORK, HERE AT SINGLE.PHP IT DOES NOT WORK)
but this does not display all the thumbnails (only the current posts' thumbnail (is it's a feature post))
this is my first attempt of trying to create a theme from blank so i'm not sure what the error could be
thanks for your help
The code in your question only loops through the posts returned by the query made for the current view, in the case of a single post view that is one post. You want to perform a new query to retrieve all the posts that have the required meta value:
<?php
query_posts(array("meta_key" => "type", "meta_value" => "featured"));
if (have_posts()) : while (have_posts()) : the_post();
?>
<!-- Display thumbnails -->
<?php endwhile; endif; ?>