Pulling Wordpress Categories Dynamically - php

I hope someone will be able to help me. I already know how to pull the most recent post in wordpress using this code:
<?php query_posts('posts_per_page=1'); ?>
<?php while (have_posts()): the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php the_excerpt(); ?>
<?php endwhile;?>
This works fine, but I want to extend this functionality. I want to be able to pull posts based on a specific category. For example, the site I'm working on has a page called "Social Media" with information on the topic. It also has a blog category of the same name. I want to know how I can pull the posts from the Social Media section of the blog to the informational Social Media page (and so on - we will have about 10 more categories which will also need their related posts).
Any help would be much appreciated! Thank you!

wp_list_categories can help you in this regard. Code will be similar to the following one:
<ul>
<?php wp_list_categories('orderby=books&include=2'); ?>
</ul>
Detail can be found here. http://codex.wordpress.org/Function_Reference/wp_list_categories

You could do it with code like this:
<?php $social_media_id = get_category( 'social-media', ARRAY_A ); ?>
<?php $social_media = query_posts( 'posts_per_page=1,category=' . $social_media_id['cat_ID'] ); ?>
<?php while (have_posts()): the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php the_excerpt(); ?>
<?php endwhile; ?>
For the get_category part, you could substitute the hard-coded social-media with the $pagename (if it's the same as the category's name).

Related

Get post from a single category with have_posts () WordPress

I am showing the last post of a blog on an external website that I am developing out of wordpress.
<?php
//Include WordPress
define('WP_USE_THEMES', false);
require('./blog/wp-load.php');
//Define quantos posts serão exibidos
query_posts('showposts=3');
?>
<?php while (have_posts()): the_post(); ?>
<li>
<h4><?php the_title(); ?></h4>
<span><?php the_time("d/m/Y"); ?></span>
<?php the_category_ID(); ?>
<?php the_content(); ?>
<div>
« Leia Mais...
</div>
</li>
<?php endwhile;?>
The point is that the blog is separated into two categories and I would like to display only one of them.
Link where I got those functions.
http://codex.wordpress.org/Template_Tags
When you query posts, you can add the conditions for categories as well, like this:
query_posts('showposts=3&cat=CAT_ID'); //replace CAT_ID with the category ID that you want
Since you mentioned that you wanted to show the latest posts, you might need to do this:
query_posts('showposts=3&cat=CAT_ID&orderby=date&order=DESC');

WordPress Posts from category won't display

I use a plugin called "Custom Content Type Manage"
this allows me to create a new content type which essentially mimics posts (all done in the name of 'user friendly')
This aside, I wrote the following:
<h2><?php single_cat_title( '', true ); ?></h2>
<p>
<?php
$page_id = '5536';
$page_data = get_page($page_id);
print apply_filters('the_content', $page_data->post_content);
?>
</p>
<p>
<?php
$category = get_the_category();
$category_id = $category[0]->cat_ID;
print category_description( $category_id );
?>
</p>
<div class="category-list">
<?php wp_nav_menu(); ?>
</div>
<ul class="leaders-container">
<?php if (have_posts()) : while (have_posts()) : the_post();?>
<li class="leader-container">
<?php
$image = get_custom_field('leader_image:to_image_array');
$url = get_custom_field('website_url:raw');
print "<img class='leader-image' src='".$image['0']."'>";
?>
<h2>
<?php the_title(); ?>
</h2>
<?php
print "</h2>";
the_content();
if($url != "#") {
print "<a class='website-button' href='".$url."' target='_blank'>Visit Website</a>";
}
?>
</li>
<?php endwhile; endif;?>
What this was doing, was getting the info from the category, and listing all the posts assigned to the category.
In this case, a post was labeled as a "Leader" and a category was their "congregation", so it was listing all the leaders assigned to the congregations.
This is an example of what it should look like
http://www.ubmsonline.org/?leader=rabbi-binyamin-sheldrake
However, this only works as it is a direct link from the posts in question
The category on the otherhand, which was working, and did list as many leaders assigned to the category, has now stopped working.
http://www.ubmsonline.org/category/ubms-leaders/
As you can see though, its pulling everything across correctly, category description, category title etc, but its just now not showing the posts.
fixed!
It was to do with a setting that changed on the "Custom Content Type Manager" plugin im using. After alot of research, i stumbled upon the exact problem on the "issues" section of the plugin's FAQ's page.
https://code.google.com/p/wordpress-custom-content-type-manager/issues/detail?id=594
Hopefully this might help others.
In wp-content/plugins/custom-content-type-manager/loader.php the filter mentioned below might be commented, so if you uncomment this add_filter this bug will fix.
// Enable archives for custom post types
add_filter('request', 'CCTM::request_filter');
Thanks again for every one's help.
Andrew

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; ?>

Showing Wordpress posts on two pages

I have three pages on my site. Let's call them home, page2, and page3.
My 'home' page is set as a static front page. My 'page2' is set up as the blog page.
What I want is the following:
I want page2 to display blog posts with a certain category (of which ID is known).
AND
I want page3 to display blog posts with a certain category (of which ID is known).
The PHP code to only show posts with a certain category (or actually in my case, show posts excluding two categories) is the following:
<?php query_posts($query_string . '&cat=-3,-8'); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h3><a href="<?php the_permalink() ?>" rel="bookmark"
title="Permanent Link to <?php the_title_attribute(); ?>">
<?php the_title(); ?></a></h3>
<?php the_excerpt('Read the rest of this entry »'); ?>
</div><!-- /.post-->
Now, in my page.php, I have the following code to display posts with one category:
<?php
// BEGIN IF PAGE is newspaper articles page
if ( is_page('newspaper') ) {
//BEGIN POST REGION
query_posts($query_string . '&cat=8'); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h3><?php the_title(); ?></h3>
<?php the_content('Read more »'); ?>
</div><!-- /.post-->
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
<?php
} //end if is_page
?>
But it doesn't show the proper posts on the newspaper page (or page3 in this question). It does, however, work for the articles page (main index.php blog page).
EDIT: I've also tried the following (but it doesn't work). I put this in the index.php file:
<?php
if ( is_page('newspaper') || is_home() ) { // START if is home
?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h3><a href="<?php the_permalink() ?>" rel="bookmark"
title="Permanent Link to
<?php the_title_attribute(); ?>">
<?php the_title(); ?></a></h3>
<!--<p><?php the_time('F jS, Y') ?> <?php //the_author() ?></p>-->
<?php the_excerpt('Read the rest of this entry »'); ?>
</div><!-- /.post-->
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
<?php
} //end if is_home() or is_page()
?>
Again, this shows the posts on the main blog page, but doesn't show any posts on the newspaper page...
The question is therefore simple (I think). How do I show posts on another page OTHER than the main blog page?
Thanks!
Amit
Rather than exclude categories and exclude pages and change the standard Wordpress loop, use a new query, like this:
<?php $my_query = new WP_Query('category_name=mycategory&showposts=1'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<h3><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<?php the_title(); ?></a></h3>
<?php the_excerpt('Read the rest of this entry »'); ?>
<?php endwhile; ?>
This can be used inside the standard WP loop and be used mutiple times in a page/post or page template without conflicting. (Enable php execution to use it in the page/post editor). Function Reference/WP Query « WordPress Codex
This also works well to use page templates to create different pages with blog posts: Page Templates « WordPress Codex, but don't forget that WP also uses category pages, too, depending on your theme: Category Templates « WordPress Codex.
I think you have to make different templates for different page. Check this link http://codex.wordpress.org/Pages
I think this thread answers the question and does what you want.
http://wordpress.org/support/topic/show-only-x-category-posts-on-page?replies=9#post-1053767
Using the string 'newspaper' in is_page('newspaper') is a potential source of the problem. It might be misspelled easily.
Have you ever tried using the page id? Something like
is_page('999')

wordpress sidebar and loop php code distorts each other

I've edited the single.php to suit my needs and it works. I only left in the part of the loop in in which is as follows:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<div class="entry">
<?php the_content('<p class="serif">Read the rest of this entry »</p>'); ?>
<?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?> </div>
</div>
<?php endwhile; else: ?>
<p>Sorry, no posts matched your criteria.</p>
<?php endif; ?>
It only displayes the text, like I want it to.
The problem I get is when I add the following code to be used as the sidebar in the template;
<?php query_posts('showposts=10'); ?>
<?php while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Link to <?php the_title(); ?>">
<?php the_title(); ?></a><br />
<?php endwhile;?>
It should display the title of the last 10 posts. But now the loop also is displaying the latest (full0 10 posts instead of just the one post that belongs to the permalink... I think a variable or so is being reused and needs to be rest.. Note that in the single.php first you get the 'sidebar' code, and then you get the 'loop' code.
So why is wordpress behaving this way?
The reason this happens is because Wordpress is a nightmarish maze of global variables. query_posts() is one of the worst offenders. If you check the documentation for this function, you'll see that they even have to warn you to basically not use it:
Important note
The query_posts function is intended
to be used to modify the main page
Loop only. It is not intended as a
means to create secondary Loops on the
page. If you want to create separate
Loops outside of the main one, you
should create separate WP_Query
objects and use those instead. Use of
query_posts on Loops other than the
main one can result in your main Loop
becoming incorrect and possibly
displaying things that you were not
expecting.
They've added some object oriented stuff that you can use now instead, namely the WP_Query object (why they haven't revamped the "main" pages to get rid of the ridiculous "The Loop" stuff yet is questionable).
You're going to want to do something like this in the sidebar:
<?php
$recentPosts = new WP_Query();
$recentPosts->query('showposts=10');
while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<a href="<?php the_permalink() ?>" rel="bookmark"
title="Link to <?php the_title(); ?>">
<?php the_title(); ?></a><br />
<?php endwhile;?>
Google around about how to use WP_Query if you want more examples.
query('showposts=10');
while ($recentPosts->have_posts()) :
$recentPosts->the_post(); ?>
"
rel="bookmark" title="Link to ">
reading the code u putting in the sidebar, u are trying to get the last 10 titles of posts to show in sidebar , right ? if so u can just use this line :
`<?php wp_get_archives('title_li=&type=postbypost&limit=10'); ?>

Categories