Although I have experience with PHP, WordPress is pretty new to me and I am struggling here.
I am trying to help a friend complete a theme. He works with several artists and each artist has a WP page named after the artist (i.e., Tom Jones) on which he wants to display only the posts about that artist. We define a tag for each artist ( Tom Jones ) with a slug like ( tom-jones )
Following the Codex, I am prepping the Loop in the template as follows:
$tags = get_tags();
//query_posts( array( 'tag' => $tag->slug ) );
query_posts( array( 'tag' => 'tom-jones' ) );
if( have_posts()) : while( have_posts() ) : the_post();
echo '<li id="feed<?php theID(); ?>" style="border-bottom:1px solid #404040;">';
echo '<table><tr><td width="40">';
echo '<img src="<?php echo get_post_meta($post->ID, 'image_path', true); ?>" />';
echo '</td><td><a href="';
the_permalink();
echo '">';
the_title();
echo '</a><br><span class="smTxt">Posted by ';
the_author();
echo ' on <em>';
the_time('F jS, Y');
echo '</em></span><br>';
the_excerpt();
echo '</td></tr></table></li>';
endwhile;
else:
echo '<h3>There are no posts.</h3>';
endif;
I would have thought that the query_post that is commented out would have grabbed the particular slug of the artist but it returns "No Posts". When I hard code as it is now, it works as expected.
Any help is greatly appreciated.
Try this
query_posts( array( 'tag' => get_query_var('tag') ) );
This post may help you.
Update:
As you said artist is a custom texonomy so try this
$the_tax = get_taxonomy( get_query_var( 'taxonomy' ) );
query_posts( array( 'artist' => $the_tax->labels->name ) );
Must use wp_reset_query() after the loop when you are using query_posts
You may read this post.
Update: (using WP_Query)
$the_tax = get_taxonomy( get_query_var( 'taxonomy' ) );
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'artist',
'field' => 'slug',
'terms' => $the_tax->labels->name
)
)
);
$query = new WP_Query( $args );
Related
Try to achieve this:
I made a ACF (custom field) in my WooCommerce Product and now I try to get this field shown in my template with this code:
<ul class="products">
<?php
$args = array(
'posts_per_page' => 20,
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_type',
'field' => 'name',
'terms' => 'grouped',
),
),
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
$linked_with_items = the_field('linked_with_items');
the_title('<strong>', '</strong>'); echo '<br />';
echo $linked_with_items;
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</ul><!--/.products-->
But whatever I try also with get_field() the field does not show in my template.
Can someone help?
https://www.advancedcustomfields.com/
This is the final code fyi
<?php if( have_rows('catalogue') ): ?>
<?php
while( have_rows('catalogue') ): the_row(); // catalogue is the field
the_sub_field('linked_with_items'); ?>
<?php endwhile; ?>
<?php endif; ?>
You could try with this:
$linked_with_items = get_field('linked_with_items', get_the_ID());
If that doesn't work, just as a test, you could try to simply loop over posts with a foreach
foreach ( $loop->posts as $post ) {
$linked_with_items = get_field('linked_with_items', $post->ID);
}
If none of those work, please make sure that your product actually have that custom field, double check the ACF field settings (rule section), the field slug, and double check your product edit page to see if the fields shows there.
Can you help me list categories in the order as they appear in wordpress admin?
So I have the following piece of code in a wp page template, which lists categories for a vendor (user).
<ul class="category-vendormenu">
<?php foreach($categories as $category) : ?>
<?php $liclass = ($current_cat == $category->ID) ? ' class="current"' : ''; ?>
<li<?php echo $liclass;?>>
<?php $author_posts = new WP_Query( array(
'post_type' => 'product',
'author' => $vendor_id,
'tax_query'=>array(
array(
'taxonomy' => 'product_cat',
'terms' => array($category->ID),
'field' => 'term_id'
)
)
));
$count = $author_posts->found_posts;
wp_reset_query();
?>
<a href="<?php echo $store_url .'section/'. $category->ID ?>" title="<?php echo $category->name ?>">
<?php echo $category->name; ?>
</a>
<?php
if ($liclass!='') {
$sub_categories = $wpdb->get_results("
SELECT DISTINCT(terms.term_id) as ID, terms.name, terms.slug, tax.count
FROM $wpdb->posts as posts
LEFT JOIN $wpdb->term_relationships as relationships ON posts.ID = relationships.object_ID
LEFT JOIN $wpdb->term_taxonomy as tax ON relationships.term_taxonomy_id = tax.term_taxonomy_id
LEFT JOIN $wpdb->terms as terms ON tax.term_id = terms.term_id
WHERE posts.post_status = 'publish' AND
posts.post_author = '$vendor_id' AND
posts.post_type = 'product' AND
tax.taxonomy = 'product_cat' AND
tax.parent='".$category->ID."'
ORDER BY terms.name ASC
");
if (count($sub_categories)>0) {
echo '<ul class="sub-menu">';
$term = get_term_by( 'id', $current_cat, 'product_cat', 'ARRAY_A' );
wp_register_script( 'ajax-subcategories', get_template_directory_uri() . '/js/ajax_subcategories.js', array( 'jquery' ) );
wp_localize_script( 'ajax-subcategories', 'parent_cat', array('ajax_url' => get_template_directory_uri() . '/functions/ajax_subcategories.php', 'store_url'=>$store_url, 'vender_id'=>$vendor_id));
wp_enqueue_script ( 'ajax-subcategories');
foreach ($sub_categories as $sub_cate) {
$liclass = ($selected_sub_cat == $sub_cate->ID) ? ' class="selected"' : '';
echo '<li'.$liclass.'>'.$sub_cate->name.'<span>'. $sub_cate->count .'</span></li>';
}
echo "</ul>";
}
}
?>
</li>
<?php endforeach; ?>
</ul>
I might be pooped and missing the obvious, but I'm getting a list of the categories in alphabetical order, which is not what I want.
In wordpress, the categories appears listed as follows:
Accommodations
Luxury
Festivals
Private Events
Book your own
But the code outputs:
Accommodations
Book your own
Festivals
Luxury
Private Events
I would simply like to keep the order as show in the wordpress admin.
However you can help, THANK YOU!
In wp_query you need use this parameter
'orderby' => 'title',
'order' => 'DESC',
You can research this in this artice https://codex.wordpress.org/Class_Reference/WP_Query
just search "Order & Orderby Parameters" this segment http://joxi.ru/L21dLEjc8pNkbm
In wordpress i am creating a custom loop/query throughwhich i pass certain parameters. As i click through pages however the last page duplicates some posts/products inorder to satisfy the posts_per_page variable however i would like to specify that i dont want any repetitions. Is there a standard way to do this? It would seem like a pretty obvious point.
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 5, 'product_cat' => 'products', 'orderby' => 'rand' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<li class="product">
<a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
<?php woocommerce_show_product_sale_flash( $post, $product ); ?>
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />'; ?>
<h3><?php the_title(); ?></h3>
<span class="price"><?php echo $product->get_price_html(); ?></span>
</a>
<?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
</li>
<?php endwhile; ?>
<?php previous_posts_link('« Previous', $loop ->max_num_pages);
next_posts_link(' Next »', $loop ->max_num_pages); ?>
<?php wp_reset_query();?>
It would seem like a pretty obvious point.
Not if you're using 'orderby' => 'rand' on your query, which is also very expensive by the way on large tables.
If you want to make sure that the items which already have been displayed will be excluded in the upcoming queries you'll need to save the post_ids which already has been displayed and pass them to the post__not_in parameter, see the codex page an search for post__not_in .
You could do something like this which should help you get the idea:
...
// don't forget to initialize the session somewhere
$already_displayed_post_ids = [];
if ( ! isset( $_SESSION['already_displayed_post_ids'] ) {
$_SESSION['already_displayed_post_ids'] = $already_displayed_post_ids;
} else {
$already_displayed_post_ids = array_merge( $already_displayed_post_ids, $_SESSION['already_displayed_post_ids'] );
}
$args = [
'post_type' => 'product',
'posts_per_page' => 5,
'product_cat' => 'products',
'orderby' => 'rand',
'post__not_in' => $already_displayed_post_ids
];
$loop = new WP_Query( $args );
...
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.
I have a custom post type with page attributes so I can create sub pages. I'm trying to display the title of the parent page in the h1 and then have it loop through and display the content of the child pages. The code below almost does this but it's outputting the child pages also in the first bit as h1 titles so I'm getting duplicates of the child page titles. How can I exclude the child pages and prevent them from displaying in the first part of the loop?
Many thanks.
<?php
echo "<ul>";
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
echo "<li><h1>".get_the_title()."</h1>";
$args=array(
'orderby' => 'menu_order',
'order' => 'ASC',
'post_parent' => $post->ID,
'post_type' => get_post_type( $post->ID ),
'posts_per_page' => 10
);
$childpages = new WP_Query($args);
if($childpages->post_count > 0) { /* display the children content */
echo "<ul>";
while ($childpages->have_posts()) {
$childpages->the_post();
echo "<li><h2>".get_the_title()."</h2></li>";
echo "<li><h2>".the_content()."</h2></li>";
}
echo "</ul>";
}
wp_reset_query();
echo "</li>";
}
}
echo "</ul>";
?>
Update: Managed to get a little bit further, almost there I think. I can now see just one sub post (the latest) and the same sub post is duplicated under each parent title either though it isn't a child of the others.
Can anyone please help me nail this last bit. Thanks.
<?php $parent_pages = get_pages( array(
'parent' => 0,
'post_type'=> 'archive'
));
foreach( $parent_pages as $parent_pages)
{ ?>
<h1><?php echo $parent_pages->post_title; ?></h1>
<?php
$children = get_pages(array(
'orderby' => 'menu_order',
'order' => 'ASC',
'post_parent' => $post->ID,
'post_type' => get_post_type( $post->ID )
));
foreach($children as $child);
?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php } ?>
Update: Trying #robbintt suggestions, I'm now here. Not sure if I'm using the get_page_children correctly and I'm now getting "Parse error: syntax error, unexpected T_AS, expecting ';' "
<?php
$parent_pages = get_pages( array(
'parent' => 0,
'post_type'=> 'archive'
) );
for ( $parent_pages as $parent_page { ?>
<h1><?php echo $parent_page->post_title; ?></h1>
<?php
$all_pages = get_pages()
$child_pages = get_page_children($parent_pages->ID, $all_pages );
for ( $child_pages as $child_page ) { ?>
<h2><?php echo $child_page->post_title; ?></h2>
<p><?php echo $child_page->post_content; ?></p>
<?php } ?>
Update: Here's my final working code thanks to #robbintt for the help.
<?php
$parent_pages = get_pages( array( 'parent' => 0, 'post_type'=> 'archive' ) );
foreach ( $parent_pages as $parent_page ) {
echo '<h1>';
echo $parent_page->post_title;
echo '</h1>';
$all_pages = get_pages(array( 'post_type'=> 'archive' ) );
$child_pages = get_page_children($parent_page->ID, $all_pages );
foreach ( $child_pages as $child_page ) {
echo '<h2>';
echo $child_page->post_title;
echo '</h2>';
echo '<p>';
echo $child_page->post_content;
echo '</p>';
}
}
?>
You're abusing the loop a little bit here by using the standard while ( have_posts() ) {}.
Instead, lets target the parent page, and then use a for loop to get the child pages.
/* here we include only pages with no parent */
$parent_pages = get_pages( 'parent' => 0 )
foreach ( $parent_pages as $parent_page ) {
/* Proceed as you have inside the while loop, targeting $parent_page each time */
}
Here is the rest of the documentation so you can sort as you wish: http://codex.wordpress.org/Function_Reference/get_pages#Parameters
Part 2:
Here's the next requested section, how to get information for child pages:
http://codex.wordpress.org/Function_Reference/get_page_children
This function will return an array of page children. The key here is that this loop is run inside the other loop which actually gets all the parent pages.
$all_pages = get_pages( array( 'post_type'=> 'archive' ) )
$child_pages = get_page_children($parent_page->ID, $all_pages );
foreach ( $child_pages as $child_page ) {
/* proceed with any calls on child page such as ID/title, in the format $child_page->ID */
Let me know how it goes! Here's the new function we are using:
http://codex.wordpress.org/Function_Reference/get_page_children
Part Three:
Here I've cleaned up your code a bit for the child page section. There was a lot going on, so I standardized and simplified how you echo'd HTML and added a lot more lines. This made the design pattern a lot more visible to the human eye.
<?php
$parent_pages = get_pages( array( 'parent' => 0, 'post_type'=> 'archive' ) );
foreach ( $parent_pages as $parent_page ) {
echo '<h1>';
echo $parent_page->post_title;
echo '</h1>';
$all_pages = get_pages( array( 'post_type'=> 'archive' ) );
$child_pages = get_page_children($parent_page->ID, $all_pages );
foreach ( $child_pages as $child_page ) {
echo '<h2>';
echo $child_page->post_title;
echo '</h2>';
echo '<p>';
echo $child_page->post_content;
echo '</p>';
}
}
?>
Here's my original writeup of this code: http://codepad.org/pLtFCI1l