I want to list all post from a custom post type by category so I came up with this code (see below) but them I see this uncategorized category I want to remove. I tried in the query cat=$cat_id&exclude="1"&post_type=locations but it's not working. Any ideas? Thank You
<div class="entry-content">
<?php
$cats = get_categories();
foreach ($cats as $cat) {
$cat_id= $cat->term_id;
echo "<h2>".$cat->name."</h2>";
query_posts("cat=$cat_id&exclude="1"&post_type=locations");
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php ?>
<?php the_title(); ?>
<?php echo '<hr/>'; ?>
<?php endwhile; endif; ?>
<?php } ?>
<?php wp_reset_postdata(); ?>
</div><!-- .entry-content -->
This is quite old, but I stumbled across it, so in case it is relevant to anyone, here is what I believe the answer is:
Since you are specifying each category by ID, you are trying to exclude the category that you are specifying when it has a term ID of 1. So in the first iteration of the loop, you are saying
cat=1&exclude=1&post_type=locations
Since you are specifying exactly what you are excluding, I believe the "inclusive" query value outweights the "exclusive" query value. If this is the case, you can instead do it like this (and even save a query!):
$cats = get_categories();
foreach ( $cats as $cat ) {
$cat_id = $cat->term_id;
if ( $cat_id == 1 )
continue; // skip 'uncategorized'
echo "<h2>".$cat->name."</h2>";
query_posts("cat=$cat_id&post_type=locations");
?>{the rest}<?php
}
Please note, I don't recommend the use of query_posts, and instead get_posts() or new WP_Query should be used instead. Further reading:
https://developer.wordpress.com/2012/05/14/querying-posts-without-query_posts/
`query_posts( 'cat=-3' );`
// 3 is category id you want to exclude
see more ref. about query_posts QUERY POSTS
$args = array('exclude' => array(1));
$categories = get_categories($args);
foreach($categories as $category) {
echo $category->name;
}
Please try this I Hope this works. Thank you.
The problem are your quotes
try this:
query_posts("cat=$cat_id&exclude=1&post_type=locations");
Hope this helps you.
TRY THIS ;
Go to Appearance then click on Widget,
Once widget opened, Click on Primary Widget area,
You will see "Category"
Just click on,Hold your left mouse button and move to
"Inactive widget " Group.
When you go back your website, You will not see Uncategorized anymore. :-))
Related
I want to display a list of categories after the first post in the Loop of index.php (this is the template my WP theme uses to display posts).
I've searched around on the web and found some code (see below) which is supposed to do as I want - inject a list of category titles as links between a list of posts in the Loop.
However, it is not working as expected. It only shows one category title, not all of them. Interestingly, it displays the title of the first post's category (the post that comes before the custom code), but no others.
My Loop code, including the custom code I inserted, is as follows:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php get_template_part('content'); ?>
// START CUSTOM CODE
<div>
<?php
if( $wp_query->current_post == 0 ) {
$categories = get_the_category();
$separator = ' ';
$output = '';
if($categories){
foreach($categories as $category) {
$output .= ''.$category->cat_name.''.$separator;
}
echo trim($output, $separator);
}
}
?>
</div>
// END CUSTOM CODE
<?php endwhile; ?>
Hoping someone can help.
Thanks,
Mekong
It is a little unclear to me from your question, but it seems like you want a list of all categories, correct? I think the line "$categories = get_the_category();" is getting categories for the current (in this case first) post only.
If you want a list of all categories that exist in your blog/website try 'get_categories', https://developer.wordpress.org/reference/functions/get_categories/
Try this code, small change in your code...
<?php if (have_posts()) : $i = 1; while (have_posts()) : the_post(); ?>
<?php get_template_part('content'); ?>
<div class="categories">
<?php
if( $i == 1){
$categories = get_categories( array(
'orderby' => 'name',
'parent' => 0
) );
foreach ( $categories as $category ) {
printf( '%2$s<br />',
esc_url( get_category_link( $category->term_id ) ),
esc_html( $category->name )
);
}
}
?>
</div>
<?php $i++; endwhile; ?>
I'm currently working on my first WordPress theme and I want to create a "Highlighted Post" section on my index.php. There are a lot of scripts for this but all seem to apply a permanent filter over the entire index.php. That is problematic since i want to show the whole posts on my index.php below. Is there a way to apply this special filter only for this section and not for the whole index.php?
If you could give us more info what determines this "Highlighted" posts we might be able to be more specific in solving this.
But in the mean time I'll guess it's two latest posts which means you could have two queries in the index.php
First one would be for "Highlighted" ones:
<?php
$args = array( 'numberposts' => 2 );
$lastposts = get_posts( $args );
foreach($lastposts as $post) : setup_postdata($post);
?>
<h2 class="news"><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<?php endforeach; ?>
This will give you excerpt of your post in this query.
What you could do then is limit excerpt to whatever length you'd like and add read more link if you want :)
function newExcerpt($more) {
global $post;
return '... Read more';
}
add_filter('excerpt_more', 'newExcerptReadMore');
function customExcerptLength( $length ) {
return 20;
}
add_filter( 'excerpt_length', 'customExcerptLength', 999 );
And for other posts you could do another query but not using first two posts like this:
$count = 0;
$lastposts = get_posts();
foreach($lastposts as $post) : setup_postdata($post);
if($count > 1)
?>
<h2 class="news"><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
$counter++;
<?php endforeach; ?>
This will just loop through the posts and skip first two posts.
If you have some other term or category or something else that determine Highlighted posts then you can use that in and pass it as an argument to get_posts($args).
I'm looking for a possibility to add a custom html block/post to the woocommerce "shop page" inside the products grid, as a product.
What I mean.. I have a grid of products on the "shop" page (archive-product) and I want to create a special post/page/html block with some text information, that will be inserted into the products grid as a one of "product", but with no price, with no title and unclickable. I've attached the screenshot of the final result I want to have, it's really self explaining - here it is exactly what I'm looking for.
As an idea probably I can create a special product with specific slug or title and the corresponding script with pre_get_posts hook will find this post/product and modify it to look like I need. I'm looking for some code/ideas how to insert this specific block/page/post into the archive-product page on some position in the grid. Thanks!
Thanks for help, guys! I've implemented the functionality I was looking for. I've found the corresponding loop in archive-product.php and as was suggested by JapanGuy, I've added a simple "if i equal let's say 5 then echo < li>[Custom block]< /li>" .
The original snippet from archive-product.php:
<?php woocommerce_product_loop_start(); ?>
<?php woocommerce_product_subcategories(); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
Modified code with inserted custom block:
<?php woocommerce_product_loop_start(); ?>
<?php woocommerce_product_subcategories(); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
if ($i == 5) {
echo "<li>[Custom block]</li>";
}
$i++;
?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
Im such simple way I can add any content to the created [Custom block] and have an usual products grid with extra custom designed block. I'm not very experienced programmer, so probably my code is not perfect, but it works. Thanks!
Edit: Previous code was wrong, changed it here
$i=0;
while ($row = mysqli_fetch_array($query))
{
if ($i == 2) {
echo "Cusom block";
}
echo "<p> Product block " . $row['column'] . " </p>";
$i++;
}
Creating WordPress Custom Post Archives : Hope this meets your requirement.
Custom Post Archives list your custom content. You probably already know the standard WordPress archives. So you can follow this to display both together .
Ref here : https://wp-types.com/documentation/user-guides/creating-wordpress-custom-post-archives/
I've a weird problem, some posts appears in categories where they are not in.
When I look in my backoffice and filter by categories, some post appears there but they are not checked in.
The resultat is that in the front office they appear too.
This is my category.php (but I don't think it's the matter)
<?php
get_header();
?>
<section id="wrapper" class="page <?php echo get_query_var('cat'); ?>">
<div id="container">
<?php
$category = get_category(get_query_var('cat'));
$cat_id = $category->cat_ID;
query_posts('showposts=1&cat='.$cat_id);
if ( have_posts() ) :
while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_format() );
endwhile;
endif;
?>
</div>
</section>
<?php
get_footer();
?>
I looked in the table "_term_relationships" and everything is right, they're not in the wrong categories.
So maybe someone have a clue to find out ?
PS : I'm using WPML, but if I desactive it, it's the same problem
You should not use query_posts(),
see (https://wordpress.stackexchange.com/questions/1753/when-should-you-use-wp-query-vs-query-posts-vs-get-posts)
try this:
<?php
$category = get_category(get_query_var('cat'));
$cat_id = $category->cat_ID;
$args = array( 'category' => $cat_id );
$query2 = new WP_Query($args);
if ( $query2->have_posts() ) :
while ( $query2->have_posts() ) :
$query2->the_post();
get_template_part( 'content', get_post_format() );
endwhile;
endif;
?>
First of all, never use query_posts to construct any type of query
Note: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).
Secondly, never change the the main query for a custom query on any type of archive page or home page. The correct way is to use pre_get_posts to alter the query variables before the main query executes. Check out this post I've done a while ago
Thirdly, category pages in Wordpress does work in a strange way. When a category page is visited, it will display posts from the selected category and posts from the selected category's child categories. I bet this is what you are seeing. This is pretty normal behavior. If you need to change this, have a look at this answer on WPSE by #ialocin. For the benefit of this answer, here is the solution
add_filter(
'parse_tax_query',
'wpse163572_do_not_include_children_in_category_archive_parse_tax_query'
);
function wpse163572_do_not_include_children_in_category_archive_parse_tax_query( $query ) {
if (
! is_admin()
&& $query->is_main_query()
&& $query->is_category()
) {
// as seen here: https://wordpress.stackexchange.com/a/140952/22534
$query->tax_query->queries[0]['include_children'] = 0;
}
}
I wanted to re-arrange my recent post in wordpress so they go Ascending/Descending.
Here is my code:
<ul>
<?php query_posts('cat=3,4,5&posts_per_page=5&order=ASC'); foreach ($post as $post) ?>
<li>
<span class="date"><?php the_time('M j') ?></span>
<?php the_title(); ?>
</li>
<?php endforeach; ?>
</ul>
Each post is pulled from different categories. View site here
Why don't you use the standard query_posts?
<?php
//The Query
query_posts('cat=3,4,5&posts_per_page=5&order=ASC');
//The Loop
if ( have_posts() ) : while ( have_posts() ) : the_post();
..
endwhile; else:
..
endif;
?>
This code should work, if there's another reason that you're using get_posts over query_posts your problem is likely to be your argument list - from what I can see you will need to change
get_posts('cat=3,4,5,numberposts=5&order=DESC&orderby=date')
to
get_posts('cat=3,4,5&numberposts=5&order=DESC&orderby=date')
as &'s are used to separate parameters.
try using "orderby" as well ...
see: http://codex.wordpress.org/Template_Tags/get_posts
I would put categories 3,4,5 under a parent category. Then you could just pull in the single category (the parent category). For example, if you're new parent category is 17, you would do:
<?php query_posts('cat=17&numberposts=5&order=DESC&orderby=date'); foreach ($post as $post) ?>
This will display posts in category 17 and any of category 17's children. Then sorting should happen as you'd expect it to.