permalink for multiple category - wordpress

hi i had 15 pages for 2 categories.. example category A and category B.... while doing so i have displayed title and content... but permalink is creating for category A not for category B... how can i code for to get a permalink for second category...
$cat = get_the_category();
$id= $cat[0]->term_id;
$id1= $cat[1]->term_id;
$args = array('posts_per_page' => 15,
'cat'=> $id1,
'orderby'=> 'post_date',
'order'=> 'ASC',
'paged' => $paged,
'post_type'=> 'post',
'post_status' => 'publish' );
query_posts( $args );
if ( have_posts() ) while ( have_posts() ) : the_post();
$i++;
?>
<li><?php the_title();?></li>
<?php
endwhile;
wp_reset_query();
?>
but the permalink is creating for $id...

if you are using template or page to display this two categories post. You have to run two loop for every category. If you are trying to use shortcode or something. it will be different.
$cat = get_the_category();
$cat_01= $cat[0]->term_id;
$cat_02= $cat[1]->term_id;
$args_for_cat_01 = array('posts_per_page' => 15,
'cat'=> $cat_01,
'orderby'=> 'post_date',
'order'=> 'ASC',
'paged' => $paged, //I don't know what is this $paged??
'post_type'=> 'post',
'post_status' => 'publish');
$post_from_cat_01 = get_posts( $args_for_cat_01 );
foreach ( $post_from_cat_01 as $post ) : setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
//Do whatever you want here. Use divs or anything.
</li>
<?php endforeach;
wp_reset_postdata();?>
</ul>
As above like this. Create another args for your second loop.
$args_for_cat_02 = array('posts_per_page' => 15,... change the cat => cat_02 ..ect
Then use the loop as above.
$post_from_cat_02 = get_posts( $args_for_cat_02 );
foreach ( $post_from_cat_02 as $post ) : setup_postdata( $post ); ?>
Remember to change the variables. I just use for more explanation.
If you need my help. Please find me any of social network by searching yeshansachithak.

Related

How can I get the latest post from a category in WordPress?

I am designing the category page. I have a loop that displays the child categories for the current category. For each child category, I would like to display a link to the latest article. Currently, the link is the same for all child categories, even when the displayed article is not in that category. What am I doing wrong?
<?php
$cat_id = get_query_var('cat');
$categories = get_categories(array( 'parent' => $cat_id));
if(count($categories) > 0):
foreach($categories as $cat):
$args = array(
'numberposts' => 1,
'offset' => 0,
'category' => $cat->cat_ID,
'orderby' => 'ID',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true );
$the_query = new WP_Query( $args );
$the_query->the_post();
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
$recent['title'] = get_the_title();
$recent['id'] = get_the_ID();
wp_reset_postdata();
endwhile;
endif;
wp_reset_postdata(); ?>
<div class="media category-list">
<div class="media-body">
<div class="details">
<h3><?php echo $cat->name; ?></h3>
<p><?php echo $cat->description; ?></p>
</div>
<dl>
<dt>Article Total:</dt><dd><?php echo $cat->count; ?></dd>
<dt>Last Article:</dt><dd><?php echo substr($recent["title"], 0, 48).'...'; ?></dd>
</dl>
</div>
</div>
<?php endforeach;
endif; ?>
Looks like you are using get_posts arguments in WP_Query.
category and numberposts are not valid arguments for WP_Query
Both belongs to get_posts and internally converted to cat and posts_per_page
So when you pass these arguments in WP_Query it does not work. But when you pass WP_Query arguments in get_posts it works ;)
So the updated arguments structure is
$args = array(
'posts_per_page' => 1,
'offset' => 0,
'cat' => $cat->cat_ID,
'orderby' => 'ID',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true
);
Also please update your query with this
$the_query = new WP_Query( $args );
//$the_query->the_post();
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
$recent['title'] = get_the_title();
$recent['id'] = get_the_ID();
// wp_reset_postdata();
endwhile;
endif;
wp_reset_postdata();
the_post should be called only once and after when you are sure that query has posts.
wp_reset_postdata hold the data for whole query. So need to do this at end of while loop not inside of the while loop.

How to Display Terms for all posts in Current Archive or Query in wordpress

I have custom post types with select option.In select option I got all the taxonomy categories and on change of select it displayes all posts of post type but when I go to single page it displayes only this current post not all from this category. I tried this code
$term = $wp_query->queried_object; but it displayes only the current post of this category not all.
How can I get all posts from the current taxonomy category.
Seems I got the solution.Here is my final code
$terms = wp_get_post_terms( $post->ID, 'course_type' );
if($terms){
// post has course_type terms attached
$course_terms = array();
foreach ($terms as $term){
$course_terms[] = $term->slug;
}
$original_query = $wp_query;
$wp_query = null;
$wp_query = new WP_Query( array(
'post_type' => 'courses',
'tax_query' => array(
array(
'taxonomy' => 'course_type',
'field' => 'slug',
'terms' => $course_terms, //the taxonomy terms I'd like to dynamically query
'posts_per_page' => '-1'
),
),
'orderby' => 'title',
'order' => 'ASC'
) );
if ( have_posts() ): ?>
<ul>
<?php while (have_posts() ) : the_post(); ?>
<li><? php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php endif;
$wp_query = null;
$wp_query = $original_query;
wp_reset_postdata();
} // end if($terms)

WordPress not showing more than 10 posts

I am showing posts by shortcode into posts/pages of WordPress, and want to show an infinite list of posts, but it is showing only 10 posts.
Here is my code; please guide me what is wrong with my query.
$args = array( 'post_type' => 'post', 'cat' => '2', 'meta_key' => 'issue_of_article', 'meta_value' => $issue, 'posts_per_page' => -1, 'orderby' => 'artcle_category', 'order' => 'ASC');
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
$loop->the_post();
<h3><?php the_title(); ?></h3>
endwhile;
}
Just add this in your argument
'posts_per_page' => -1
then you are done.
One more thing: you can change the default setting from admin to something other than 10. Go to the WordPress admin - Settings - Reading. There is an option like "Blog pages show at most". Type there the number of posts you want as the default.
Go to settings menu in the admin page
Settings -> Reading
Change the value for Blog pages show at most.
It will work.
Stupid question maybe but why do you call $loop->the_post(); twice ? Isn't it the source of the problem ? (each loop calls 2 posts at a time)
You can decide how many posts to show in the loop:
<?php wp_reset_query(); ?>
<?php
$loop = new WP_Query(
array(
'post_type' => 'resource',
'order_by' => 'post_id',
'order' => 'ASC',
'post_status' => 'publish',
'posts_per_page' => 100
)
);
?>
<?php while ($loop -> have_posts()): $loop -> the_post(); ?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<?php endwhile; ?>
'posts_per_page' => -1
or
'posts_per_page' => 1000
Both of these should work.
At first - post WP questions at Wordpress.Stackexchange.com
The good way is to add in functions.php:
add_action('pre_get_posts','myfunc');
function myfunc($query){
if ($query->is_main_query() && $query->is_archive){
$query->set( 'posts_per_page', 1000);
}
return $query;
}

Cant' Display Custom Post Type Title Base on Taxonomy Terms

Trying to list all Custom Post Type titles based on filtered Custom Taxonomy Terms I am getting the list of all post titles instead of getting the list of Queried post. Here is the code I am using:
<?php
$loop = new WP_Query(
array(
'post_type' => 'photos',
'technique' => 'zevar',
'post_child' => 0,
'posts_per_page' => 10
)
);
?>
<?php
while ( $loop->have_posts() ) : $loop->the_post();
?>
<?php the_title(); ?>
<?php endwhile; ?>
As you can see I have a Custom Post Type called "photos" and Custom Taxonomy registered as "technique". Under Taxonomy "technique" I have some terms which one of them is "zevar". Can you please let me know what I am doing wrong here?
Your taxonomy query is not correct. check following code.
<?php
$loop = new WP_Query(
array(
'post_type' => 'photos',
'tax_query' => array(
array(
'taxonomy' => 'technique',
'field' => 'slug',
'terms' => 'zevar'
)
),
'posts_per_page' => 10
)
);
?>
<?php
while ( $loop->have_posts() ) : $loop->the_post();
?>
<?php the_title(); ?>
<?php endwhile; ?>
Reference on how to use taxonomy parameter https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

Fix wordpress query (add if have)

<ul>
<?php
global $post;
$args = array( 'numberposts' => 12, 'orderby' => 'date', 'year' => '2012', 'order' => 'ASC', 'document_language' => 'english');
$myposts = get_documents( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li><?php the_title(); ?></li>
<?php endforeach;
?>
</ul>
How can I put the into if have posts query? It's basically custom post type query but its using document revisions plugin (so get_posts is get_documents)?
Many thanks!
If you just want to check if the query returned any posts. You can still do it with your code.
Here is example:
<?php
global $post;
// This is your query
$args = array( 'numberposts' => 12, 'orderby' => 'date', 'year' => '2012', 'order' => 'ASC', 'document_language' => 'english');
$myposts = get_documents( $args );
if (count($myposts) > 0 ) : // If the query returned results
?>
<ul>
<?php foreach( $myposts as $post ) : setup_postdata($post); // for each post in array ?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>

Resources