Display posts that belong to certain category - Wordpress - php

I'm having trouble getting this to work. Can someone provide a quick snippet for category template that displays posts that belong to a category called 'Product A'. I've been using the trial and error method for the past 3 hours with no luck.
Thank you!
Here's what I've been playing around with -
<?php
/*
Template Name: yadayada
*/
?>
<?php get_header(); ?>
<?php get_sidebar(); ?>
<?php query_posts('cat=32&showposts=5'); ?>
<div class="post">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<div class="post-description">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
</div>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
</div>

You can used the WP_Query class.
One way I've done it before is by first creating a category name of Product-A and making the slug 'product-a' all lower case.
Then instantiate a new instance of the class. Pass in the parameter of 'category_name=product-a' You do no pass in the category name with this parameter, but rather the slug name. once you do that you should be able to use the WP_Query as follows:
<?php $my_query = new WP_Query( 'category_name=product-a' ); ?>
<?php if ($my_query->have_posts() ) : ?>
<?php while ( $my_query->have_posts()) : $my_query->the_post() ?>
<article <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<div class="product-excerpt"><?php the_content(); ?> </div>
</article>
<?php endwhile; ?>
<?php else : ?>
<h2>Not Found</h2>
<?php endif; ?>
pretty much everything is the same as the regular loop but instead of just
<?php if(have_post()) : while(have_post()) : the_post() ?>
You would used object notation to refer to this particular query.
<?php if($my_query->have_post()) : while($my_query->have_post()) : $my_query->the_post() ?>
hope it helps.

First get your Product A category id; (if you use, your cat id in your custom query it 's gonna work perfectly instead of category name.)
<?php
query_posts('cat=1');
while (have_posts()) : the_post();
the_content();
endwhile;
?>

Related

WordPress Advanced Custom Fields Relationship Single Page

I have a custom post type for "Clubs" and custom post type for "Events by Clubs". How can I get the name of the club on my single event page (not the while loop)?
I have searched and searched, but all I can find is to get it in a loop.
Below is the code for the single event page.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<section class="entry-content">
<ul>
<li>Club Name: { get Club Name from Club post type relationship } </li>
<li>Other detail: <?php the_field('blah_blah'); ?>
<li>.....other details.....</li>
</ul>
<?php the_content(); ?>
</section>
<?php endwhile; endif; ?>
You need to set up the postdata for the $club post object in order to access it:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$club = get_field('club_field_name');
if($club):
$post = $club;
setup_postdata($post);
$club_name = get_the_title();
wp_reset_postdata();
endif; ?>
<h1><?php the_title(); ?></h1>
<section class="entry-content">
<ul>
<li>Club Name: <?php echo $club_name; ?> </li>
<li>Other detail: <?php the_field('blah_blah'); ?>
<li>.....other details.....</li>
</ul>
<?php the_content(); ?>
</section>
<?php endwhile; endif; ?>
A couple of points:
You must remember to call the wp_reset_postdata(); function after you call setup_postdata($post); as otherwise other bits of the page won't work (as you're overriding the main page loop)
If you want to link to the club page you can move my piece of code to within the <li> tag and then use the_permalink(); as usual to output a link to the page (I've done it outside of your code to keep things clear).
Give me a shout if you're still stuck.

php if statements inside an if statement

I can't seem to get this to work. I have an if statement opening with <?php if (have_posts())...etc.. Then, below that I have a conditional statement determining whether or not the post is in a certain category <?php if (is_category())...etc.. This is the part I can't get right. What am I doing wrong?
<?php get_header();?>
<section id="content">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="article-wrapper">
<article id="post-<?php the_ID(); ?>">
<time datetime="<?php the_time('c'); ?>"><?php the_time('F j, Y'); ?></time>
<?php if (is_category('news')) { ?>
<h3><?php the_title(); ?></h3>
<?php the_excerpt(); ?>
<p class="read-more">Read more</p>
<?php } ?>
<?php if (is_category('podcasts')) {
$custom = get_post_custom($post->ID);
$buzzsprout_code = $custom["buzzsprout_code"][0];
echo do_shortcode($buzzsprout_code);
echo '<p class="read-emails">View emails and comment on this episode</p>';
} ?>
</article>
</div>
<?php endwhile;endif; ?>
<div id="pagination">
<?php my_paginate_links(); ?>
</div>
</section>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
From wordpress docs:
is_category() tests to see if you are displaying a category archive, but you are displaying a single post, so this will always return false
in_category() tests to see if a given post has a category in that specific category and must be used in The Loop, but you are trying to figure out what the category is, before you get to the loop.
The short try in_category() instead.

featured post same as post content

I have a wordpress site I am creating.
on the post pages, I have text widget with PHP allowed, where I have a custom loop:
<?php $my_query2 = new WP_Query("showposts=1&cat=9,10,11,18,19&orderby=rand"); ?>
<?php if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink() ?>">
<div class="home-widget-thumb">
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail('home-thumb');
}
?>
</div>
<h2><?php the_title(); ?></h2></a>
<div class="body">
<?php echo get_the_excerpt(); ?>
</div><!--body-->
</br>
<span class="more-link">
[more]
</span>
<?php endwhile; ?>
<?php else : ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
For some reason, whatever blog post you are on is the same as in the loop I created.
See an example here:
http://counselingandtraining.com/play-therapy/
The loop for the post is not modified.
Can anyone tell me why this is happening?
Let me know if I can provide further info.
Thanks in advance for your time.
Chris
It looks to me like the problem is that you are still using the base query.
You can change this by adding your variable $my_query2 in the code like this:
<?php if ($my_query2->have_posts()) : ?><?php while ($my_query2->have_posts()) : $my_query2->the_post(); ?>
This will make all the functions like the_title(), the_content(), etc work as intended since they will be set to $my_query2.

PHP codes for wordpress

Hello guys so I am making a car review wordpress magazine and I am having issues with php codes as I am not a great programmer. Actually on this page Memes I would like the social plugins to be below each picture and not on the top furthermore I would like it to display the post date and the author of the post. Some formatting for the pictures size etc would be great too. Below is the code I am using
<?php /*
Template Name: ListPostsInCategoryThatHasSameNameAsPage
*/ ?>
<?php get_header(); ?>
<div id="content" class="archive <?php if(get_option('colabs_layout_settings')=='two-col-right'){echo 'right';}else{?>left<?php }?>">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div <?php post_class(); ?> id="post_<?php the_ID(); ?>">
<span id="map"><?php _e('Home','colabsthemes');?> » <?php the_title(); ?></span>
<h2 class="title"><?php the_title(); ?></h2>
<div class="entry" style="padding-top:15px;">
<?php the_content(__('<p>Read the rest of this page »</p>','colabsthemes')); ?>
<?php echo colabs_share();?>
<?php wp_link_pages(array('before' => __('<p><strong>Pages:</strong>','colabsthemes'), 'after' => '</p>', 'next_or_number' => 'number')); ?>
</div>
</div>
<?php endwhile; else: endif; ?>
<?php query_posts('category_name='.get_the_title().'&post_status=publish,future');?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?>
<?php endwhile; else: endif; ?>
</div>
</div>
<div id="sidebar" class="<?php if(get_option('colabs_layout_settings')=='two-col-right'){echo 'left';}else{?>right<?php }?>">
<?php get_sidebar(); ?>
</div>
<div> <?php get_footer(); ?> </div>
Finally for the review part I would like to do something like Autotest/review but I don't know which code to use to show these kind of square articles. Please help me out.
For adding the author and date of the post, you can add:
<p>Written by:
<?php get_the_author(); ?></p>
get_the_author() will display the author's name as is set in their "Display name publicly as" field in their user profile (Administration > Users > Your Profile).
To also display the date:
<p>Written by:
<?php get_the_author(); ?> on
<?php get_the_date(); ?></p>
I don't see where you're using the social sharing links on the page you linked to - did you remove them?
For more information:
http://codex.wordpress.org/Function_Reference/get_the_author
http://codex.wordpress.org/Function_Reference/get_the_date

putting php function in template works but putting concatenated function in functions.php doesn't?

Here are two functions I'm working with (line 1 and line 33): http://pastebin.com/GWCJGS1i
If you notice around line 123, I have included the concatenated function . fb_comment_count() .. For some reason, this produces an incorrect comment count. It just shows zero no matter how many comments there are.
However, if I insert <?php echo fb_comment_count(); ?> into a page template, it works fine. Why does this happen? How can I get the correct comment count to show up with the concatenated function?
Here is the page template:
<?php get_header();?>
<section id="content">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article class="post" id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<section class="entry">
<?php echo fb_comment_count(); ?>
<p class="attachment"><?php echo wp_get_attachment_image( $post->ID, 'medium' ); ?></p>
<div class="caption">
<?php if ( !empty($post->post_excerpt) ) the_excerpt(); // this is the "caption" ?>
</div>
</section>
</article>
<?php comments_template(); ?>
<?php endwhile; else: ?>
<p>Sorry, no attachments matched your criteria.</p>
<?php endif; ?>
</section>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Two possible causes:
The global $post variable is not set when you call your function from functions.php but it is when inside the template.
The call to get_posts() is trashing the $post global.

Categories