WordPress Get all the Category's Post - wordpress

I have Developed WordPress Site Successfully, but i have doubt in Getting all the post by
Categories.
My Code is like this
new WP_Query("cat=54,71,72&order=ASC");
Default it is getting the first category id and the Post.
Thanks

If you want to get ALL your categories instead of a selected few, you don't need to query by category.
new WP_Query("order=ASC");
If you want to query a particular category, but you're not sure what's the category ID number, query it by category slug
new WP_Query("category_name=your-category-slug&order=ASC");

You can also use this code as well, just need to change the number of post, like as of now, below listed code is use for display 5 post only, so you have to change this limit.
<?php query_posts('showposts=5'); ?>
<ul>
<?php while (have_posts()) : the_post(); ?>
<li><?php the_title(); ?></li>
<?php the_content();?>
<?php endwhile;?>
</ul>

Related

Wordpress multiple category display with custom link

I am using <?php the_category(); ?> to display the categories of my post.
It was displaying but my problem is the permalink it produce.
the category link give this for example: http://samplesite.com/category/funpage/
what I want is to remove the category in the link and just produce: http://samplesite.com/funpage/
also, I am using custom post type UI to create post.
anyone can help me please?
You can use get_the_category() function and get all categories for that post and display using loop.
<?php
foreach((get_the_category()) as $category) {
?>
<a href='url you want to pass add here '><?php echo $category->name; ?></a>
<?php
}
?>

Categories with post from child categories on front page

I am developing my first WordPress site and I am using the Customizr theme. I need to build a front page, that shows the latest posts from 2 categories - News and Events and all their child categories. So my category tree looks like this:
Articles
-News
--First news child category
--Second news child category
-Events
--First events child category
--Second events child category
and I want the front page to show the category News and the category Events with all the posts in their child categories packed in 2 containers with the headlines News and Events respectively. I am a total newbie in WordPress and I've searched for days for a solution, but I think I'm searching with the wrong keywords, because I didn't found what I need. I've created a child theme, and I'm currently trying to make an index.php with The Loop to achieve that. I've also tried making a static front page which uses a different template.
So which is the right way to do that? Can you please at least give me some pointers? Thank you!
If i understood corectly you can achieve expected result with Category Parameters for WP Query. Then you can loop over posts like this:
<?php
$the_query = new WP_Query( "category_name=news,events"); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Also, to customize your front page better make a new template (home.php), do your job there and then asssign it to an empty page and make that page your front page from Settings->Reading

How to display list categories in wordpress integration magento

Can help somebody. I spent several hours to find solution but without results
I tried to display the list of categories on homepage wordpress blog thru following code
<?php $category = Mage::registry('wordpress_category') ?>
<?php if ($category): ?>
<?php echo $category->getId() ?>: <?php echo $category->getName() ?>
<?php endif; ?>
But the method
Mage::registry('wordpress_category')
always return null.
I found that, i should probably be using the Fishpig_Wordpress_Block_Category_View. But i dont know where i should put it.
The following code will retrieve the current category when viewing a category page in your blog:
<?php Mage::registry('wordpress_category') ?>
This is not what you need. To view a list of categories, you could create a custom collection using the following:
<?php $categories = Mage::getResourceModel('wordpress/post_category_collection') ?>
A better way would be to use the category widget block:
<block type="wordpress/sidebar_widget_categories" name="wp.categories" template="wordpress/sidebar/widget/categories.phtml" />
You can create this in PHP using the following code:
<?php echo Mage::getSingleton('core/layout')
->createBlock('wordpress/sidebar_widget_categories')
->setTemplate('wordpress/sidebar/widget/categories.phtml')
->toHtml() ?>
The above code uses the default template, however, feel free to use your own custom template.

Wordpress: only show posts for specific category IF post = comments allowed

I have a page that displays certain posts from a certain category, in this case category 33:
<?php $top_query = new WP_Query('cat=33'); ?>
<?php while($top_query->have_posts()) : $top_query->the_post(); ?>
How Can I specify that the posts returned should only be ones that have comments enabled?.
I have tried wrapping it in:
<?php if(comments_open()) : ?>
Hover that needs to be used within the loop :(
Thanks in advance
In the wordpress database the post status is saved in the $wpdb->posts table in the comment_status column. Try passing this variable to WP_Query also like this:
<?php $top_query = new WP_Query( array('cat'=>33, 'comment_status'=>'open'); ?>
<?php while($top_query->have_posts()) : $top_query->the_post(); ?>
this should do it.

WordPress Rewind Posts

I have the following code: http://pastebin.com/ng99augD
It basically spits out five posts for the five authors I have specified in the query posts in each chunk of code and then rewinds the posts prior to the next query. This is far from ideal and would like to make the code more efficient without having to repeat all the template code for the posts over and over. I also want them to be in a random order instead of coming out in the order they have been written as I don't want any particular author to be at the top. Thanks.
I'm not sure why the use of rewind_posts() is necessary. You are creating a new query using by your use of query_posts() each time. rewind_posts() rewinds the query for re-use.
If your performance isn't suffering, it may be okay to run 5 queries to get your five authors. To avoid rewriting the same template code each time you have at least two options. Create a php file with the template code in it, and include it each time you need it:
<?php include('author_posts.php');?>
Or, better yet, create a function in your functions.php or in a plugin file that accepts your query results and outputs the template.
<?php $posts = query_posts('posts_per_page=1&author=author1');
if(function_exists('my_authors')){my_authors($posts);}
?>
Then in your functions.php or plugin:
function my_authors($posts){
//template code here, using $posts->ID, $posts->post_content, etc
}
The third option, which cleans up your code and the number of queries would be to use a category to display posts assigned to it here. You are making 5 queries to display five posts. To use one query to display five posts and sort randomly like you want to, edit each of the five posts and assign them to a new category, lets say:'author sampler'. Then in your template file:
<?php $posts = get_posts('category=author sampler'&order_by=rand&order=asc');
foreach($posts as $post):?>
<?php setup_postdata($post);
//your posted template code follows:
?>
<article id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?><?php edit_post_link('Edit', ' (', ')'); ?></h2>
<p class="p-cat">In: <?php the_category('|') ?></p>
<p class="p-author">
<span class="name">
<?php the_author_posts_link(); ?></span> <span class="avatar"><?php echo get_avatar( $email, $size = '32' ); ?>
</span>
</p>
<?php //etc ?>
<?php endforeach;?>

Resources