I've got some code (see below) from an online tutorial that displays an alphabetical list of Category names and then,
underneath each category, a list of post titles for that category.
It works, but I want the post titles to also be displayed alphabetically. At present it's only category names that are alphabetical - see image:
I've done some research online and I think I may need to set up a 'nested loop' - but I have no idea how to edit my code to do this.
Hoping someone can show me how to edit code to get both category names AND post titles to display alphabetically.
This is the code I'm using:
// Grab all the categories from the database that have posts.
$categories = get_terms( 'category', 'orderby=name&order=ASC');
// Loop through categories
foreach ( $categories as $category ) {
// Display category name
echo '<h2 class="post-title">' . $category->name . '</h2>';
echo '<div class="post-list">';
// WP_Query arguments
$args = array(
'cat' => $category->term_id,
'orderby' => 'term_order',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<p><?php the_title(); ?></p>
<?php
} // End while
} // End if
echo '</div>';
// Restore original Post Data
wp_reset_postdata();
} // End foreach
$categories = get_terms( 'category', 'orderby=name&order=ASC');
// Loop through categories
foreach ( $categories as $category ) {
// Display category name
echo '<h2 class="post-title">' . $category->name . '</h2>';
echo '<div class="post-list">';
// WP_Query arguments
$args = array(
'cat' => $category->term_id,
'order' => 'ASC',
'orderby' => 'title',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<p><?php the_title(); ?></p>
<?php
} // End while
} // End if
echo '</div>';
// Restore original Post Data
wp_reset_postdata();
} // End foreach
We can add the orderby argument with title.
Related
I want to get the latest 5 posts using WordPress get_posts function. I did the following:
In functions.php file I have added extra.php file which contain following code:
if( !function_exists( 'evertstrap_post' ) ) {
function evertstrap_post() {
$args = array(
'post_type' => 'post',
'numberposts' => 5,
);
$recent_posts = get_posts( $args );
foreach ( $recent_posts as $post ) {
setup_postdata( $post );
echo get_the_title();
echo '<br/>';
}
wp_reset_postdata();
}
}
Now, from home.php file I am calling evertstrap_post() BUT it's not get the latest 5 posts !!
BUT
If I directly put the code into index.php file then it's working.
How can I solve it?
I've seen this sometimes in WordPress where echoing the output is unfavorable. Could you give this a shot?
if( !function_exists( 'evertstrap_post' ) ) {
function evertstrap_post() {
global $post;
$args = array(
'post_type' => 'post',
'numberposts' => 5,
);
$recent_posts = get_posts( $args );
$output = '';
foreach ( $recent_posts as $post ) {
setup_postdata( $post );
$output .= get_the_title();
$output .= '<br/>';
}
wp_reset_postdata();
return $output;
}
}
Then in home.php you could do:
<?php echo evertstrap_post(); ?>
Try this:
if( !function_exists( 'evertstrap_post' ) ) {
function evertstrap_post() {
$args = array(
'post_type' => 'post',
'numberposts' => 5,
);
$recent_posts = get_posts( $args );
foreach ( $recent_posts as $post ) {
setup_postdata( $post );
echo get_the_title($post->ID);
echo '<br/>';
}
wp_reset_postdata();
}
}
You saw only last post title 5 times because get_the_title() is a template tag used in "while post : do post" wordpress loop. However the same function accepts argument with post ID. more here: https://developer.wordpress.org/reference/functions/get_the_title/
Getting posts via function get_posts() you store post obejcts in an array and using normal foreach loop you can retrieve object data using $post->key_name within this loop. PHP: Getting data out of an object
Also I recommend wordpress codex. Its very well documented.
I would like to display a drop down menu for products in a category.
<select>
<option value="CODE HERE">Volvo</option>
</select>
So according to Wordpress coding..
<?php
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
Okay so I investigated further and I am looking to do a single page template according to https://developer.wordpress.org I am using a child theme for Storefront which is called NOVA WP.
To make this "single page template" I copied page.php and renamed it to page-buildit.php
This is Mypage in which I actually editing the code. I did copy the code but it turns out blank
found this: WooCommerce: Create a shortcode to display product categories
but my undestanding is we cant do this anymore with the new wordpress version.
<?php
$args = array(
'order' => 'ASC',
'hide_empty' => $hide_empty,
'include' => $ids,
'posts_per_page' =>'-1'
);
$product_categories = get_terms( 'product_cat', $args );
echo "<select>";
foreach( $product_categories as $category ){
echo "<option value = '" . esc_attr( $category->slug ) . "'>" . esc_html( $category->name ) . "</option>";
}
echo "</select>";
?>
Check this out. This is the way to get product categories.!
You can also use the function wp_dropdown_categories to make your code simpler.
To get a dropdown of categories of products you can write like this.
$args = array('hide_empty'=> 0,
'taxonomy'=> 'product_cat',
'hierarchical'=>1);
wp_dropdown_categories($args);
Or if you want to hold the output in a variable you can use the argument 'echo'=>0 and then echo the variable to get the same output.
$args = array('hide_empty'=> 0,
'taxonomy'=> 'product_cat',
'hierarchical'=>1,
'echo'=>0);
$cats = wp_dropdown_categories($args);
echo $cats;
Okay so here is how I solved it, with the help of Hemnath mouli, I already gave you the credit for the answer but I wanted to publish the products inside a category in a dropbox.
$args = array(
'posts_per_page' => -1,
'product_cat' => 'motherboard',
'post_type' => 'product',
'orderby' => 'title',
);
$products = new WP_Query( $args );
echo "<select>";
foreach ( $products as $product ) {
$products->the_post();
?>
<option value="<?php the_permalink(); ?>"> <?php the_title(); ?>
<?php
}
echo "</select>";
?>
Now I will need to show the image of this product after selecting it.
Im having problems with retrieving product infos trough object functions in Woocommerce.
This is how I do:
public function table_data()
{
$args = array( 'post_type' => 'product', 'posts_per_page' => -1 );
$products = get_posts( $args );
$pfactory = new WC_Product_Factory();
foreach($products as $product)
{
$_product = $pfactory->get_product($product);
}
}
This returns product informations from wp_posts only, it won't give me the information stored in wp_postmeta.
I need the full information from all products (such as price, sku, stock etc.) in one array, but I seem to be missing something and Im unsure if it has with the hierarchy of the functions in my code. However I thought this was possible to do without SQL-queries.
Basicly, what im trying to do is a complete duplicate of product list in admin for listing products with information in the panel.
Thanks for all the help I can get.
Use WP_Query instead, which will give you access to the global WooCommerce $product variable from within the loop. From there, you can grab the price, sku, stock and all other kinds of data. http://docs.woothemes.com/wc-apidocs/class-WC_Product.html
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ): while ( $loop->have_posts() ): $loop->the_post();
global $product;
$price = $product->get_price_html();
$sku = $product->get_sku();
$stock = $product->get_stock_quantity();
endwhile; endif; wp_reset_postdata();
?>
First thing I came up with was this.
function products() {
return array_map('wc_get_product', get_posts(['post_type'=>'product','nopaging'=>true]));
}
I'd love to know from someone more experienced with WooCommerce if there is a better way
As of WooCommerce 3 you can do it using the wc_get_products function like this:
$args = array(
'status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
'limit' => -1,
);
$products = wc_get_products($args);
if (count($products) > 0) {
foreach ($products as $product) {
echo $product->get_id() . '<br>';
echo $product->get_name() . '<br>';
echo $product->get_price_html() . '<br>';
echo $product->get_sku() . '<br>';
echo $product->get_stock_quantity() . '<br>';
}
}
My code:
<?php
$category = get_the_category();
echo $category[0]->cat_name;
?>
<?php
$category = get_the_category();
echo $category[1]->cat_name;
?>
I want get two category as links but exclude "uncategorized" category.
Try below code:
<?php
$args = array(
'include' => '1,2'
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
echo '' . $category->name . '<br/>';
}
?>
In above code change 'include' => '1,2' and add the category that you want to include.
Find more here.
Updated :
If you want to get only first two category and exclude Uncategorized from that two category, then you can try below code :
<?php
$i=0;
$args = array(
'orderby' => 'id'
);
$my_category = get_categories($args);
foreach($my_category as $mcategory){
if($i==0 || $i==1){
if($mcategory->name != 'Uncategorized'):
echo '' . $mcategory->name . '<br/>';
endif;
}
$i++;
}
?>
Here I have added one dummy condition to make it work.
I have the following query, which is designed to show a list of terms in a taxonomy, and under each term, a list of posts assigned to that term.
This appears in a sidebar on the left hand side of my single.php posts page. In the main area of the page, the actual single post is meant to display.
However, instead of displaying the current single post, it is only displaying the TITLE for the most recent single post.
Here is my query:
$terms = get_terms('benefit-cats');
echo "<ul>";
foreach ($terms as $term) {
$wpq = array ('taxonomy'=>'benefit-cats','term'=>$term->slug);
$query = new WP_Query ($wpq);
echo "<li class=\"list-item\">".$term->name.""; ////
echo "<ul class=\"children\">";
$posts = $query->posts;
foreach ($posts as $post) {
echo "<li>".$post->post_title."</li>";
}
echo "</ul></li>";
}
echo "</ul>";
I have tried adding reset query to the code, to no avail. I have identified that it is this specific section that is causing the problem:
$posts = $query->posts;
foreach ($posts as $post) {
echo "<li>".$post->post_title."</li>";
}
What exactly am I doing wrong here? I have been trouble shooting this for 30-40 minutes now without any success.
Would appreciate an explanation of my error.
try to use
<?php
if ($query->have_posts() ) : while ($query->have_posts() ) : $query->the_post(); ?>
echo "<li>".the_title()."</li>";
<?php endwhile; endif; wp_reset_query(); ?>
in place of
$posts = $query->posts;
foreach ($posts as $post) {
echo "<li>".$post->post_title."</li>";
}
hope this will work fine for you
Your arguments for WP_Query aren't well formed.
Here's how it should be for taxonomy (snippet taken from the Codex ):
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'people',
'field' => 'slug',
'terms' => 'bob'
)
)
);
After you get your arguments right, you should loop over the results:
while( $query->have_posts() ):
$query->the_post();
// It is now that get_permalink() will work
endwhile;