This is code from my wordpress homepage template. There are 3 same code, like this one bellow. The question is how can I put different categories to show different news when I post. At the moment code is same on my homepage and it show only category named World, it's the first category created. How can I setup the rest of code with different categories.
Currently it's like this : http://zaslike.com/files/os8hvxg38oendfwcvrz6.png
I need to put other categories instead same three.
http://justpaste.it/e0nh - Here is the code of homepage template.
I believe you are asking for this:
<!-- Your Query -->
<?php // The Query
query_posts( array ( 'category_name' => 'change-to-category-name', 'posts_per_page' => 1 ) ); ?>
<?php // The Loop
while ( have_posts() ) : the_post();?>
// YOUR HTML GOES HERE
<?php endwhile; ?>
<?php // Reset Query
wp_reset_query(); ?> // DON'T FORGET THIS
Use this code at the parts of the site that display your categories change the categories name to the ones you want to display and you are good to go.
Good luck welcome to SO.
Related
I’m trying to use blogs(posts) with two different designs
First, for non-loggedin(guest) users which access blogs from website (Example URL – Click Here)
Second is for logged in users which access blogs from their frontend dashboard
For the first case, I am using single.php and
For the second case, I’m using custom template in user’s dashboard and fetching posts using post ids.
QUERY
——————–
So the question is how to post comments to post using post id i.e for the second case?
E.g post url is: http://yourdomain.com/blogs/?pid=23
Here is attached video which highlights the query
Unable to display content. Adobe Flash is required.
Further, is there any other way to achieve the above scenario i.e. two different designs
Let me know in case of more clarification.
You can use a normal loop in your custom query for logged in users
for example
<?php
$args = array(
'post__in' => array(1,2,4,6),
'post_type' => 'post',
);
// the query
$the_query = new WP_Query( $args ); ?>
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
// put your code here
<?php endwhile; ?>
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
this way you should not be worry about getting comments by post id (the loop will handle the id for you)
I am creating a Lyrics website with 3.9.1 wordpress version and I want to create a post for each Album and its songs with internal links.
What am doing right now is creating a post of albums and pages for songs. But if I do that, I am not able to give tags for pages ( songs ) for SEO.
So I want to create even songs with postings so that I can add tags to them but I don't want songs to be listed on my home page unless the visitor clicks on the link in the album.
How can I fix this?
Tags to Pages
If you want to add Tags for Pages you can by doing this:
add_action( 'init', 'tags_for_pages' );
function tags_for_pages() {
register_taxonomy_for_object_type( 'post_tag', 'page' );
}
Just Posts
But if you wanna do all the things just with posts, I think you should:
1) Create 2 Categories: Albums and Songs
2) Create The Albums Post and Add the Album Category to it
3) Create the Songs Post and Add the Song Category to it
4) Add this code to your functions.php
add_action( 'pre_get_posts', 'modify_home_query' );
function modify_home_query( $query ) {
if ( !is_admin() && $query->is_home() && $query->is_main_query() ) {
$query->set( 'category_name', 'albums' );
}
}
and in your home page will be only posts with the category albums, you will have to paste the links in the content of the Album Post, like you would if it was a page.
Best Choice
But I think you should search for Custom Post Types, and how to create it.
You could create a Custom Post Type Albums and a Custom Post Type Songs and link them using plugins like Advanced Custom Fields, Posts 2 Posts and / or CPT-Onomies
Post Type Generator: http://generatewp.com/post-type/
You can make your homepage query only look for albums.
For this to work, you need to set posts categories "Album" or "Song".
Then, you need to find your theme home page php file, like a index.php, and look for your current query. It looks like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
//some code
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
Then add this before
<?php query_posts('cat_name=album'); ?>
and this after
<?php wp_reset_postdata(); ?>
This will only work if you use categories!!
Normally if I was doing more than one loop on a page, aside from the main wordpress loop, I'd just use wp_query, however when the author.php template is being used I can't see how I could use that, since I'd have to pass some args.
Pulling posts from categories or by date etc is all easy enough with wp_query, even getting posts by author ID can be done, but it needs to be generic, ie get posts from the current authors page.
Now, using the same loop as my category page I can easily generate posts on the author.php, but I need a second loop and I just can't figure out how to do it.
First loop would be pulling one random post and it's featured image, second would be getting the archive of that authors posts.
Any ideas?
Does this give you a list of the author's posts? If so, it should be easy to modify the arguments for WP_Query to get exactly what you want.
See here for all the options: http://codex.wordpress.org/Class_Reference/WP_Query
<?php
$new_loop = new WP_Query( array(
'post_type' => 'post',
'author' => get_the_author_meta('ID')
) );
?>
<?php if ( $new_loop->have_posts() ) : while ( $new_loop->have_posts() ) : $new_loop->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; else: ?>
Sorry, nothing was found.
<?php endif; ?>
<?php wp_reset_query(); ?>
I'm using query_posts on my home page to fetch articles from multiple post types. I'm using following code to do so.
<?php query_posts( array('post_type' => array('post','page','custom_post1','custom_post2','custom_post3','custom_post4','custom_post5','custom_post6')));
if (have_posts()) : while (have_posts()) : the_post(); // begin the Loop ?>
/*html code goes here*/
<?php endwhile; endif; ?>
It fetches the posts rightly, but the problem is: it does not fetch old posts. So for example I have 1 post and its publish date is 02/july/2012, this post won't be displayed on my home page. But as soon as I update the date to 02/Aug/2012 it starts showing up on Home page.
So is there any code I can add in query_posts that will enable older posts to be fetched too.
Wordpress by default fetch 10 posts or number of posts set by user from admin.
query_posts accept argument that return number of record.
query_posts( 'posts_per_page=5' );
query_posts( 'posts_per_page=50' );
I have a WordPress site, where on the main page I list the content from more categories.
My question is, is there a plugin where I can paginate the results from a category? I mean something like $this->plugin_paginate('category_id'); or smth?
Best Regards,
If you're using the standard Wordpress loop, even with a query_posts for a category, pagination is automatic with the usual posts_nav_link. Are you trying to paginate for more than one query and more than one category on the same page?
Edit 11/20: I use this in several different places on one page to show the latest post in a category:
<?php
$my_query = new WP_Query('category_name=mycategory&showposts=1');
while ($my_query->have_posts()) : $my_query->the_post();
?>
<?php the_title(); ?>
<?php endwhile; ?>
That link then goes to a category page that paginates for that category: Category Templates « WordPress Codex
I don't know how to paginate different categories on the same page. Must be possible. Maybe ask in the Wordpress forums.
This sounds like something that a simple, well-formed query_posts() call can do. I doubt that you'll even need to rely on a plugin. :)
I'm going to assume that you're familiar with the query_posts() function, so let's go ahead and use this example as a base:
// let's get the first 10 posts from category ID 3
query_posts('posts_per_page=10&cat=3');
while(have_posts()):the_post();
// do Wordpress magic right here
endwhile;
Now, to get the 11th to 20th posts from category 3 (that is, the NEXT 10 posts), we'll want to use the [offset] parameter of query_posts():
// let's get the next 10 posts from category ID 3
query_posts('posts_per_page=10&cat=3&offset=10');
while(have_posts()):the_post();
// do Wordpress magic right here
endwhile;
For most purposes, that should be sufficient. However, you did mention that you plan on paginating category post lists from the main page alone? I'm assuming that you mean that you have multiple category post lists on your main page, and all these are paginated independently.
With something like that, it looks like you'll have to work a bit with Javascript to do the work for you, along with what I illustrated above.
I believe you could do something like this:
<?php
if(isset($_GET['paged'])){
$page = $_GET['paged']-1;
}else{
$page = 0;
}
$postsPerPage = 5;
$theOffset = $page*$postsPerPage;
?>
<?php query_posts(array('posts_per_page' => $postsPerPage, 'cat' => CATEGORIES HERE, 'offset' => $theOffset)); ?>
Hope this will help you :)
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 5,
'paged' => $page,
);
query_posts($args);?>
?>