I'm very new to PHP and have some trouble wrapping my head around it sometimes so please bear with me.
I have a lot of categories and a lot of tags. I started making category-slug.php templates but it'd probably be best for me to just use category.php and tag.php templates. I just can't get them to work unless I add in something like 'category_name' => 'art'. I've also read that querying isn't ideal (I think that's what I'm doing?), but I have had custom development done and I'm not sure if that has or hasn't been left as my only option.
$page_content = "";
if (have_posts()) :
while (have_posts()) : the_post();
$page_content .= get_the_content();
endwhile;
endif;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_type' => 'post', 'posts_per_page' => 6, 'paged'
=> $paged );
And then later on I have this with post title, date, excerpts, etc. to follow.
<?php
$wp_query = new WP_Query($args);
while ( $wp_query->have_posts() ) : $wp_query->the_post();
?>
How do I make the category.php and tag.php pages specific to each unique slug without having to manually make each one?
Category Template Hierarchy, as described in WP codex:
The Template Hierarchy specifies that WordPress will use the first Template file it finds in your current Theme's directory from the following list:
category-slug.php
category-ID.php
category.php
archive.php
index.php
You don't have to do any custom WP_Query to get the posts of category.
if (have_posts()) :
while (have_posts()) : the_post();
the_content();//this is the content of the single post, belonging to this category
the_title();//title of the single post
the_excerpt();//excerpt of the single post
//and so on
endwhile;
endif;
It's the similar scenario for tags as well.
More details on Category Templates and Tag Templates.
Related
I using wp ajax load more plugin While clicking the button it repeating the same previous post. How to fix it. Here I share my code in below:
<?php
$the_query = new WP_Query( array(
'posts_per_page'=>10,//on loading page i show 10 after click load more i want to show other posts
'post_type'=>'post-name',
'category_name' => 'A-E',
'orderby'=> 'title',
'order' => 'ASC',
'paged' => get_query_var('paged') ? get_query_var('paged') : 1)
);
?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
// here I print the following data
<?php
endwhile;
?>
<?php
echo do_shortcode('[ajax_load_more post_type="post-name" posts_per_page="10" category="a-e" button_label="Load More"]');
?>
can anyone fix it?
I just use this to solve my own problem:
while ( $query->have_posts() ) : $query->the_post();
$do_not_duplicate[] = $post->ID; // Store post ID in array
// Other loop actions could go here
endwhile; wp_reset_query();
$post__not_in = ($do_not_duplicate) ? implode(',', $do_not_duplicate) : '';
echo do_shortcode('[ajax_load_more post__not_in="'. $post__not_in .'" post_type="post-name" posts_per_page="10" category="a-e" button_label="Load More"]');
If you want to get the next set of pages with ajax-load-more you must use the offset parameter. The ajax-load-more plugin must get the next posts that WP_Query has. So both WP_Query and ajax-load-more must query the same pages. First alter the shortcode to be same as WP_Query, by adding order and orderby parameter:
[ajax_load_more post_type="post-name" posts_per_page="10" order="ASC" orderby="title" category="a-e" button_label="Load More"]
Then add offset=(get_query_var('paged') ? get_query_var('paged') : 1)*10 like this:
[ajax_load_more post_type="post-name" offset='.((get_query_var('paged') ? get_query_var('paged') : 1)*10).' posts_per_page="10" order="ASC" orderby="title" category="a-e" button_label="Load More"]
Note: Seems like ajax-load-more doesn't know of - so you must separate category or post_type by ,. Unless you have actually a category named a-e
You don't need to create a custom query. You are using a plugin that will generate it for you via their shortcode. As per their documentation, you can just create your settings for the shortcode.
See instructions here https://connekthq.com/plugins/ajax-load-more/docs/shortcode-builder/
Just take the generated shortcode and paste it into your page/post or in your php file with do_shortcode.
The plugin handles the query and the posts per page etc.
I'm running Wordpress 4.1. I have two blog pages on my site, and though I don't really know php, I've done some tinkering and figured out how to modify the page templates so each page only displays posts for a specific category. That bit of code looks like this:
<?php query_posts('cat=2'); ?>
That works fine. Page A displays posts from category 1, and Page B displays posts from category 2.
What I'd like to do is disable post title links for one specific category. In other words, Page A would display posts from category 1 (with standard clickable title links), and while Page B would display posts from category 2 (with non-clickable title links).
I'm an HTML/CSS guy, so really out of my depth here, but if there's a way to modify the loop to achieve this, I'd love to learn how. Thanks in advance for any help.
Yes, you can do this using the category.php theme file. When this page is hit, it loads a specific category requested and the posts that fall into that category.
Your theme and loop may look something like this:
<?php single_cat_title(); ?>
<?php echo category_description(); ?>
if (have_posts()) : while (have_posts()) : the_post();
/// display posts from specific category
endwhile; endif;
Or if you don't want to use that page which is designed for that, you can create your own loop:
query_posts( array ( 'category_name' => 'my-category-slug', 'posts_per_page' => 50 ) );
All together like this:
<?php
/* retrieve unlimited # of posts with an category slug of music */
query_posts( array ( 'category_name' => 'music', 'posts_per_page' => -1 ) );
// set $more to 0 in order to only get the first part of the post
global $more;
$more = 0;
// the Loop
while (have_posts()) : the_post();
the_content( 'Read the full post ยป' );
endwhile;
?>
I am creating a new theme for my blog where I checked some posts as a Sticky post from wp-admin and on front-end I have given some CSS to highlight those sticky post.
Now I want to give link on that highlight area which redirect to particular page having all sticky posts.
I also want to do the same for other post formats as well, like IMAGE, LINK, etc.
Can someone help me on this?
You can do it with custom wordpress template and query.
Create custom page template for each post format lists page Like for sticky posts list, create page template page-sticky.php
Inside this page add custom query with loop of posts something like below :
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_format' => 'sticky', 'posts_per_page' => 10, 'paged' => $paged );
query_posts($args);
if ( have_posts() ) : while (have_posts()) : the_post();
get_template_part( 'content', get_post_format() );
endwhile;
endif;
wp_reset_query();
You can change sticky word with any post format you want. also make sure you put everything in php quote.
In my index.php I use this code to limit the posts per page and it works without any problems:
$showposts = 5;
$do_not_show_stickies = 1;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('category__in' => $cat, 'showposts' => $showposts, 'ignore_sticky_posts' => 1, 'paged' => $paged);
$loop2query = new WP_Query($args);
query_posts($args); if(have_posts()) : while (have_posts()) : the_post(); ?>
<div class="blogpost"> ... </div>
<?php endwhile; endif;
posts_nav_link(); // Navigating the pages with $showposts each. ?>
The same code did not work in category.php so I changed it to the following but it still does not work:
$showposts = 4;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
if (have_posts()) { while (have_posts()) { the_post(); ?>
<div class="blogpost"> ... </div>
<?php } }
else { ?>
<p>There are no blog posts in this category.</p>
<?php } ?>
<?php posts_nav_link(); // Navigating the pages with $showposts each. ?>
I tried to change the line with if(have_posts()) : while (have_posts()) : the_post(); ?> [...] in category.php to make it similar to that line in index.php but nothing I tried worked.
Wordpress has a setting for this, found in the admin area under SETTINGS -> READING -> Blog pages show at most
You can use this instead of custom-modifying your queries. It may make it a little easier to maintain your project down the road.
Use the posts_per_page argument (Codex here)
$args = array('category__in' => $cat, 'posts_per_page' => $showposts, 'ignore_sticky_posts' => 1, 'paged' => $paged);
In you first example, you actually have two queries : new WP_query, then query_posts, so you need to get rid of one of them, as this is redundant. In the second exemple, it is the contrary, you do not have any query (although WordPress might execute one by default, depending on where this page is called). So anyway, there is no point in using $showposts in your 2nd example, as you are not executing a query after...
if (have_posts()) is generally used to treat a default (not visible in your page code) loop from WordPress or to treat a query that you declare just before (generally with query_posts()).
As #Samuel is saying, the argument to use is posts_per_page, but I think you are not there yet and you should first start to learn how to execute a query, so you can start by reading the WordPress codex on query_posts, it will be the best place to go first : http://codex.wordpress.org/Function_Reference/query_posts.
On my homepage I am bringing in some custom post types using this:
<?php $loop = new WP_Query( array( 'post_type' => 'news', 'posts_per_page' => 4 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
etc....
This works fine. But then I just want to bring in a list of blog posts with the default taxonomy. So I try this:
<?php query_posts( 'cat=uncategorized' );?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
And it spits out all the custom post types, but not the one blog post I have (the hello world blog post). I feel like I have done this before without any problems, but can't figure out what I am doing wrong.
I think cat it's used with category id, in that case would be query_posts('cat=1');
If you want to use the name try ('category_name=whatever');
Hope its useful
GL
Do you have wp_reset_query some place after your first query and before the spot where you're trying to include that list?