Show specific post ID - Query posts / Array - php

I've got this command to show the sub posts from that category but I want to be able to just show 1 post by ID but still under the specific category.
Here's the code I have:
<?php query_posts( array('cat' => $category_id,'posts_per_page'=>'1')); ?>
<li>
<?php while (have_posts()) : the_post(); ?>
<div class="sponsor-thumb"><?php the_post_thumbnail( 'category-thumb' ) ?></div>
<?php the_title(); ?>
</li>
<?php endwhile;?>
Please help! :)

I'm a bit unsure on what you want.
If you want to show a single post and you have the id of that post, just add 'p' => 123 to the query_posts array.
<?php query_posts( array('cat' => $category_id,'posts_per_page'=>'1', 'p' => 123)); ?>
Replace 123 with the id of the post you want to retrieve.

Related

WordPress displaying custom post type posts by custom taxonomy

I have a custom post type logie and it has a custom taxonomy with now 3 options.
My goals is to display every post per taxonomy in a different bootstrap row like this:
Taxonomy title 1
Post Post Post
Taxonomy title 2
Post Post Post
Taxonomy title 3
Post Post Post
So every Post is a col and the title and posts are in a row
This is what my code looks like now. The titles work but to get the posts it is a bit tricky. I get no errors which is kinda annoying...
<div class="container-full">
<?php foreach ($cat as $catVal):
$postArg = array('post_type'=>'logie','posts_per_page'=>-1,'order'=>'desc',
'tax_query' => array(
array(
'taxonomy' => 'logietype',
'field' => 'term_id',
'terms' => $catVal->term_id
)
));
$getPost = new wp_query($postArg);
global $post;
?>
<div class="row">
<h2><?php echo $catVal->name; ?></h2>
<?php if($getPost->have_posts()): ?>
<?php while ( $getPost->have_posts()):$getPost->the_post(): ?>
<div class="col-md-4">
<?php echo $post->post_title; ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
<?php endforeach; ?>
</div>
Anyone can help me out thanks a lot!!
You can use a simplier argument query:
$postArg = array(
'post_type'=>'logie',
'posts_per_page'=>-1,
'order'=>'desc',
'logietype' => $catVal->term_id
);
By the way, I suggest to use the more native
<?php the_title(); ?>
which can be hooked if needed.

How to write a template to list all posts with a custom taxonomy term

I have used the CPT (Custom Post Types) plugin to create a custom post type called Resources, and a custom taxonomy called Resource Categories. Within that taxonomy I have two categories/terms: blogs and books.
I have already figured out which file to edit: taxonomy.php
But I can't figure out how to write the template. Here is what I have so far:
<?php get_header(); ?>
<section class="content">
<div class="page-title pad group"><h2><?php single_term_title(); ?></h2></div>
<div class="pad group">
<?php if ((category_description() != '') && !is_paged()) : ?>
<div class="notebox">
<?php echo category_description(); ?>
</div>
<?php endif; ?>
<?php if ( have_posts() ) : ?>
<div class="post-list group">
<?php $i = 1; echo '<div class="post-row">'; while ( have_posts() ): the_post(); ?>
<?php get_template_part('content'); ?>
<?php if($i % 2 == 0) { echo '</div><div class="post-row">'; } $i++; endwhile; echo '</div>'; ?>
</div><!--/.post-list-->
<?php get_template_part('inc/pagination'); ?>
<?php endif; ?>
</div><!--/.pad-->
</section><!--/.content-->
<?php get_sidebar(); ?>
<?php get_footer();
What Works:
The page is showing up when I go to resource-categories/books. Good!
The title of the category "Books" and the category description show up. Good!
What Doesn't Work:
There is no list of items from the category. It's just blank. I originally copied the code from the archive.php, which works just fine on Search pages, Author Lists, I think Category Archives, too...
What's wrong? I'm a php n000000b so I'm having a hard time translating tutorials/answers on the internet to solve this problem.
Use your custom WP_QUERY
$args = array(
'post_type' => 'resources',
'tax_query' => array(
'taxonomy' => 'resource-categories',
'field' => 'id',
'terms' => get_queried_object()->term_id,
);
);
$query = new WP_QUERY( $args );
while ( $query->have_posts() ):
$query->the_post()
..
endwhile;
Your issue is not your code, that is correct and should work out of the box.
Check the following:
Make sure that the public parameter for both your custom post type and custom taxonomy is set to true in register_post_type() and register_taxonomy()
Resave/reflush your permalinks by simply visting the permalinks setting page in back end.
This should solve your issue. If it does not, then you have serious conflict or bad filter in a plugin or in your theme.
Just a note, NEVER EVER run a custom query because something in the main query needs to be changed or is not working in the main query. Fix the issue properly and correctly, and do not hide it ;-)

Retrieve posts by category name issue in WP Query

So I've created a custom page template for my "Topics" Page.
What I want to do is add in some PHP to the custom page template my Topics page uses, to retrieve permalinks for the 3 most recent posts from a chosen category.
E.g.
(From post category 1)
--> Permalink for post 1
--> Permalink for post 2
--> Permalink for post 3
My code so far is as follows:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<ul>
<?php
$category_posts = new WP_Query('cat=consumer-trust&showposts=3');
while ($category_posts ->have_posts()) : $category_posts->the_post();?>
<li>
<a class="yourclass" href="<?php the_permalink(); ?>" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?> </a></li>
<?php endwhile; ?>
The problem, however, is that changing the cat in the WP_Query doesn't seem to make any difference. I've tried numbers and category names, and neither works.
Can anybody advise? This code will appear three times on the intended page, for three different categories.
use
query_posts( array ( 'category_name' => 'cat_name','meta_key'=>'_pr_date', 'orderby' => 'meta_value','order'=>'DESC') );
Thanks for all the help, I've managed to find an answer:
<?php global $post; // required
$args = array('category' => 18); // include category 18
$custom_posts = get_posts($args);
foreach($custom_posts as $post) : setup_postdata($post);
// put here what you want to appear for each post like:
//the title:
the_title();
endforeach;
?>

PHP advanced custom fields

This may be a basic question but i cant seem to find a correct solution.
In advanced custom fields I have set up a Field Group CD, in CD there are three fields, title, info, author and the group shows if the category = CD
Therefore when i make a new post with category CD I fill these three fields. There are 10 Posts in the CD categories.
Now the issue I am having is displaying all the posts on a page.
Here is the code I tried
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php query_posts( array( 'posts_per_page' => -1, 'cat' => '6', 'CD' => ( get_query_var('CD') ? get_query_var('CD') : 1 ), ));
if (have_posts()) {
while (have_posts()) {
the_post();
get_post_meta();
} // end while
} // end if
?>
<?php endwhile; endif; ?>
This returned an error Warning: Missing argument 1 for get_post_meta(), called in /Volumes/shared/Digital/_Websites/londonconchord/wp-content/themes/conchord/t-disc.php on line 25 and defined in /Volumes/shared/Digital/_Websites/londonconchord/wp-includes/post.php on line 1792
I tried a different attempt
<p><?php query_posts( 'cat=6' );
the_field('title');
the_field('info');
the_field('author'); ?></p>
I had more luck here as I was printing some information, however only the 1st post in the category and it kept repeating, i wanted all 10 posts and no repeat.
I think im close just looking for those final pointers
Thanks
My solution (Finally,) Hope this can help others
<?php
$args = array('cat' => 6);
$category_posts = new WP_Query($args);
if($category_posts->have_posts()) :
while($category_posts->have_posts()) :
$category_posts->the_post();
?>
<div class="article">
<div class="articleinfo">
<h3><?php the_field('title'); ?></h3>
<?php the_field('author'); ?>
<?php the_field('label'); ?>
</div>
<img src="<?php the_field('cd_image'); ?>" height="200" width="200" alt="cd-image" />
</div>
<?php
endwhile;
else:
?>
Oops, there are no posts.
<?php
endif;
?>
Loops through all posts and pulls the ACF i needed
Seems get_post_meta() has variables and your are missing it.
Like get_post_meta($var) expected but your are just calling get_post_meta(). Hope it helps.
You're not querying properly I don't think.
You need to grab the field
get_field('YOUR FIELD NAME') );
Then loop through and grab what you need as a subfield.
the_sub_field('YOUR SUB FIELD');
Example
<?php if(get_field('YOUR FIELD NAME')): while(has_sub_field('YOUR FIELD NAME')): ?>
<img class="logo" src="<?php the_sub_field('logo'); ?>" />
<h1><?php the_sub_field('title'); ?></h1>
<?php endwhile; endif; ?>
For an example. Hope this helps... let me know.
I couldn't get ether of the solutions above to work, thank you though guys for your input,
here is my solution so far
<h3><?php the_field('title', 475); ?></h3>
<?php the_field('info', 475); ?>
<?php the_field('author', 475); ?>
</div>
<img src="<?php the_field('cd_image', 475); ?>" height="200" width="200" alt="" />
Then I just repeated this and changed the id from 475 to others, now ive pulled in all the posts however the downfall is that any new posts i have to add in this code again.
Can i use a wp query to pull these 4 fields in a variable and then print that variable, then loop through the category until all posts are printed?

Archives.php not working in wordpress

Its been a while since ive done wordpress templating and for some reason i cant get the archives.php to work. i have this:
<?php
/* Template Name: Archives */
get_header(); ?>
<div id='content'>
<?php get_sidebar('left'); ?>
<div id='column2-wide'>
<?php if (have_posts()): while (have_posts()): the_post(); ?>
<?php if ( in_category(16) ) { ?>
<h2><?php the_title(); ?></h2>
<div class="post">
<?php echo the_content(); ?>
</div>
<?php } ?>
<?php endwhile; endif; ?>
</div><!-- column2 -->
<?php get_footer(); ?>
Then created a page in the admin and chosen the Archives template to be used from the dropdown.
However the posts just dont seem to show. Am i missing something? The very same code works in the index.php file. It seems its just not working when im trying to display posts in a page.
It could well be im missing a file as I started developing the theme using a skeleton theme by Kennethreitz which can be found here:
https://github.com/kennethreitz/wordpress-theme-skeleton/
Any help would be appreciated.
Thanks for reading.
fl3x7
EDIT--> Also ive removed the category check so it should just list all posts but instead what it does is just echo the title of the current page if that helps
I'm assuming that "16" is the category ID? According to the WordPress docs for in_category, if you're querying by ID it should be passed as an integer.
$category
(mixed) (required) One or more categories specified by ID
(integer), name or slug (string), or an array of these
With your current code, the in_category check is failing every time since it is checking for an association with the category named "16." Try this instead:
if ( in_category(16) ) {
...
}
Thanks to the help provided by Hans. This is what I did:
query_posts( array ( 'category_name' => 'foo', 'posts_per_page' => 10 ) );
if (have_posts()): while (have_posts()): the_post(); ?>
<h2><a href='<?php the_permalink(); ?>'><?php the_title(); ?></a></h2>
<div class="post">
<?php echo the_excerpt(); ?>
</div>
<?php endwhile; endif; ?>

Categories