Posts by tag - Creating a reset - php

I have a rather neat filter tag that will narrow down results in a specific post category to just the posts within that category that have the selected tag.
I am struggling to get the stock 'select tag...' option to reset the filter and show all results again - so once a user selects a tag the only way to reset the list is by refresh.
Here is my filter within the function.php script:
// CASE STUDY FILTER
function my_filters(){
$args = array(
'orderby' => 'date',
'order' => $_POST['date']
);
if( isset( $_POST['tagfilter'] ) ):
$args['tax_query'] = array(
array(
'taxonomy' => 'post_tag',
'field' => 'id',
'terms' => $_POST['tagfilter']
)
);
$query = new WP_Query( $args );
if( $query->have_posts() ):
while( $query->have_posts() ):
$query->the_post();
echo "<article class=\"post-box " . get_post_class() . "\">";
echo "";
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' );
echo "<div class=\"cs-image\" style=\"background-image: url('" . $url . "')\"></div>";
$case_study = get_field('case_study_page_content');
if( $case_study ):
echo "<div class=\"cs-text-container\">";
while( have_rows('case_study_page_content') ): the_row();
$case_study_title = get_sub_field('title');
$case_study_author = get_sub_field('author');
echo "<h2>" . $case_study_title . "</h2>";
echo "<p>" . $case_study_author . "</p>";
endwhile;
echo "</div>";
endif;
echo "</article>";
endwhile;
wp_reset_postdata();
else :
echo 'No case studies found';
endif;
die();
endif;
}
add_action('wp_ajax_customfilter', 'my_filters');
add_action('wp_ajax_nopriv_customfilter', 'my_filters');
// END CASE STUDY FILTER
The form that this takes input from is here:
<form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="advanced-filterform">
<?php
if( $terms = get_terms( 'post_tag', 'orderby=name' ) ) :
echo '<select class="tagfilter" name="tagfilter"><option>Select tag...</option>';
foreach ( $terms as $term ) :
echo '<option value="' . $term->term_id . '">' . $term->name . '</option>';
endforeach;
echo '</select>';
endif;
?>
<button>Apply filters</button>
<input type="hidden" name="action" value="customfilter">
</form>
As simply as possible without creating a tag on every post - how can I implement this.

Related

Array wordpress php

I want to create a portfolio in wordpress.
It is working properly but I would like to see the categories of the portfolio in h3 under the h2 title.
Other question.
Now the link will only work on the title h2, I wish it were all over the caption as link.
any advice on the code are welcome! Thank you so much
<?php
$args = array( 'post_type' => 'portfolio', 'posts_per_page' => -1 );
$loop = new WP_Query( $args );
$terms = get_the_terms( $post->ID, 'portfolio-categories' );
if ( $terms && ! is_wp_error( $terms ) ) :
$links = array();
foreach ( $terms as $term ) {
$links[] = $term->name;
}
$tax_links = join( " ", str_replace(' ', '-', $links));
$tax = strtolower($tax_links);
else :
$tax = '';
endif;
while ( $loop->have_posts() ) : $loop->the_post();
echo '<div class="row-masonry">';
echo '<div class="item">';
echo '<div class="well portfolio-item no-gutter">';
echo '<div class="thumbnail no-gutter">'. get_the_post_thumbnail() .'</div>';
echo '<div class="caption">';
echo '<div class="vertical-align">';
$link = get_the_permalink();
echo "<a href=$link>";
echo '<h2>'. get_the_title() .'</h2>';
I want to see here the portfolio image category in h3
echo "</a>";
echo '</div>'; /*close caption*/
echo '</div>'; /*close caption*/
echo '</div>';
echo '</div>';
endwhile;
echo '</div>';
echo '</div>';
echo '</div>';
?>
</div><!-- #page -->
This should work for you. You can google how to get the category for a post. It will yield your answer - but this will work.
<?php
$args = array( 'post_type' => 'portfolio', 'posts_per_page' => -1 );
$loop = new WP_Query( $args );
$terms = get_the_terms( $post->ID, 'portfolio-categories' );
if ( $terms && ! is_wp_error( $terms ) ) :
$links = array();
foreach ( $terms as $term ) {
$links[] = $term->name;
}
$tax_links = join( " ", str_replace(' ', '-', $links));
$tax = strtolower($tax_links);
else :
$tax = '';
endif;
while ( $loop->have_posts() ) : $loop->the_post();
echo '<div class="row-masonry">';
echo '<div class="item">';
echo '<div class="well portfolio-item no-gutter">';
echo '<div class="thumbnail no-gutter">'. get_the_post_thumbnail() .'</div>';
echo '<div class="caption">';
echo '<div class="vertical-align">';
$link = get_the_permalink();
echo "<a href=$link>";
echo '<h2>'. get_the_title() .'</h2>';
// GET THE CATEGORY -- Returns an array of all categories
// $categories = get_the_category();
$categories = get_the_terms( get_the_ID(), 'portfolio-categories' ); // for custom taxnomies
// If not an empty array then show the first category set
if ( ! empty( $categories ) ) {
echo "<h3>" . esc_html( $categories[0]->name ) ."</h3>";
}
echo "</a>";
echo '</div>'; /*close caption*/
echo '</div>'; /*close caption*/
echo '</div>';
echo '</div>';
endwhile;
echo '</div>';
echo '</div>';
echo '</div>';
?>
</div><!-- #page -->

Display items in order including image WooCommerce

I am trying to display the products/items within a WooCommerce order using the following php, but the items are not being shown.
The code I am using is an adaptation of this: Get cart item name, quantity all details woocommerce
Using the original code from the question above also does not display anything on my page.
Thanks in advance.
<?php
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = $values['data']->post;
//product image
$getProductDetail = wc_get_product( $values['product_id'] );
echo '<tr><td>';
echo $getProductDetail->get_image(); // accepts 2 arguments ( size, attr )
echo '</td>';
echo '<td>';
echo '<p style="font-size:10pt;">'.$_product->post_title.'</p><td><p style="font-size:10pt;">x'. $values['quantity'] . '</p></td>';
echo '</td></tr>';
};
?>
The full page code is here:
<?php
/*
Template Name: Store
*/
if (!is_user_logged_in() || !current_user_can('manage_options')) wp_die('This page is private.');
?>
<!DOCTYPE HTML>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>
<?php _e('Store Queue'); ?>
</title>
</head>
<body id="driverqueue">
<header>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h1 class="title"><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile; endif; ?>
</header>
<section>
<?php
global $woocommerce;
$args = array(
'post_type' => 'shop_order',
'post_status' => 'wc-processing',
'meta_key' => '_customer_user',
'posts_per_page' => '-1'
);
$my_query = new WP_Query($args);
$customer_orders = $my_query->posts;
foreach ($customer_orders as $customer_order) {
$order = new WC_Order();
$order->populate($customer_order);
$orderdata = (array) $order;
// $orderdata Array will have Information. for e.g Shippin firstname, Lastname, Address ... and MUCH more.... Just enjoy!
}
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$order_id = $loop->post->ID;
$order = new WC_Order($order_id);
?>
<table class="ordertable" id="<?php echo $order_id; ?>">
<tr>
<td>
<p>Order #
<?php echo $order_id; ?> —
<time datetime="<?php the_time('c'); ?>">
<?php echo the_time('d/m/Y g:i:s A'); ?>
</time>
</p>
</td>
<td>
<?php echo $order->billing_first_name . ' ' . $order->billing_last_name ?>
</td>
<td>
<table>
<tr>
<?php
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = $values['data']->post;
//product image
$getProductDetail = wc_get_product( $values['product_id'] );
echo '<tr><td>';
echo $getProductDetail->get_image(); // accepts 2 arguments ( size, attr )
echo '</td>';
echo '<td>';
echo '<p style="font-size:10pt;">'.$_product->post_title.'</p><td><p style="font-size:10pt;">x'. $values['quantity'] . '</p></td>';
echo '</td></tr>';
};
?>
</tr>
</table>
</td>
<td>
<p>
<?php
do_action( 'woocommerce_admin_order_actions_start', $order );
$actions = array();
if ( $order->has_status( array( 'pending', 'on-hold', 'processing' ) ) ) {
$actions['complete'] = array(
'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status=awaiting-shipment&order_id=' . $post->ID ), 'woocommerce-mark-order-status' ),
'name' => __( 'Complete', 'woocommerce' ),
'action' => "complete"
);
};
$actions = apply_filters( 'woocommerce_admin_order_actions', $actions, $order );
foreach ( $actions as $action ) {
printf( '<a class="button %s" href="%s" data-tip="%s">%s</a>', esc_attr( $action['action'] ), esc_url( $action['url'] ), esc_attr( $action['name'] ), esc_attr( $action['name'] ) );
}
do_action( 'woocommerce_admin_order_actions_end', $order );
?>
</p>
</td>
<td>
<form action="">
<input type="checkbox" class="ordercollected" value="0" />
</form>
</td>
</tr>
<?php endwhile; ?>
</section>
</body>
</html>
Solved this using info from the following link with the adapted code below:
https://wordpress.stackexchange.com/questions/180075/how-to-get-woocommerce-order-product-info
<?php
foreach ($order->get_items() as $key => $lineItem) {
//uncomment the following to see the full data
// echo '<pre>';
// print_r($lineItem);
// echo '</pre>';
$product_id = $lineItem['product_id'];
$product = wc_get_product( $product_id );
echo '<tr><td>' . $product->get_image() . '</td>'; // accepts 2 arguments ( size, attr )
echo '<td>' . 'Product: ' . $lineItem['name'] . '</td>';
echo '<td> x' . $lineItem['qty'] . '</td></tr>';
}
?>

Getting Featured Image as a Background Image

I have a code in my page.php file that creates a list of child pages. I want every li to have a background-image added by Featured image function. Here is the entire code I have
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php endwhile; // end of the loop. ?>
<?php
if (is_page('eventsphotography')) {
$query = new WP_query('pagename=eventsphotography');
$eventsphotography_id = $query->queried_object->ID;
//The loop
if($query->have_posts() ) {
while($query->have_posts() ) {
$query->the_post();
the_content();
}
}
/* Get the children of the eventsphotography page */
$args = array (
'post_parent' => $thePostID,
'post_parent' => $eventsphotography_id,
'order' => 'ASC'
);
$eventsphotography_query = new WP_query($args);
//The Loop
if($eventsphotography_query->have_posts() ) {
echo '<ul class="events-list">';
while($eventsphotography_query->have_posts() ){
$eventsphotography_query->the_post();
$background = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
echo '<li style="background:url(' . $background[0] . '); background-repeat:no-repeat; background-size:cover;">';
echo '<div class="events-centered">';
echo '<a href="' . get_permalink() . '">';
echo '<h4>' . get_the_title() . '</h4>';
echo '</a>';
echo '<div class="view-events-details">';
echo '<a href="' . get_permalink() . '">';
echo '<h5>View Images</h5>';
echo '</a>';
echo '</div>';
echo '</div>'; /* end of events-centered */
echo '</li>';
}
echo'</ul>';
}
}
?>
I only need help for these lines:
$background = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
AND
echo '<li style="background:url(' . $background[0] . '); background-repeat:no-repeat; background-size:cover;">';
Here's the screenshot of the result of my code:
http://oi68.tinypic.com/10xzdhl.jpg
I marked the first <li> with a red rectangle. As I said before, I want URL of the featured image to be placed in <li style="background:url(URL of the featured image)">
I have found a solution. First, I created a new WP_Query:
$subs = new WP_Query( array( 'post_parent' => $post->ID, 'post_type' => 'page', 'meta_key' => '_thumbnail_id'));
Then in my loop I added this lines:
if($eventsphotography_query->have_posts() && $subs->have_posts()) {
echo '<ul class="events-list">';
while($eventsphotography_query->have_posts() && $subs->have_posts()){
$eventsphotography_query->the_post();
$subs->the_post();
echo '<li>';
echo get_the_post_thumbnail($post->ID);
...rest of the code...

Populate Custom Ordering/Sorting woocommerce

I'm trying to figure out on how to populate my custom ordering inside my custom template page.
Here is my work in progress. So far the output shows a select box with empty values. When I check the view source i saw this error.
Warning: Invalid argument supplied for foreach() in C:\Users\Noel\Desktop\xampp\htdocs\lumiere\wp-content\themes\klasik\lumiere-products-page.php on line 23
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 10, 'product_cat' => 'Lumiere', 'orderby' => 'rand' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<?php //Woocommerce Custom Ordering ?>
<form class="woocommerce-ordering" method="get">
<select name="orderby" class="orderby">
<?php foreach ( $catalog_orderby_options as $id => $name ) : ?>
<option value="<?php echo esc_attr( $id ); ?>" <?php selected( $orderby, $id ); ?>><?php echo esc_html( $name ); ?></option>
<?php endforeach; ?>
</select>
<?php
// Keep query string vars intact
foreach ( $_GET as $key => $val ) {
if ( 'orderby' === $key || 'submit' === $key ) {
continue;
}
if ( is_array( $val ) ) {
foreach( $val as $innerVal ) {
echo '<input type="hidden" name="' . esc_attr( $key ) . '[]" value="' . esc_attr( $innerVal ) . '" />';
}
} else {
echo '<input type="hidden" name="' . esc_attr( $key ) . '" value="' . esc_attr( $val ) . '" />';
}
}
?>
</form>
<li class="product product-items ">
<div class="product-item">
<?php woocommerce_show_product_sale_flash( $post, $product ); ?>
<div class="product-thumbnail">
<?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" />'; ?>
</div>
<div class="product-info">
<h3>
<?php the_title(); ?>
</h3>
<h4 class="product-category-title">
<?php
$size = sizeof( get_the_terms( $post->ID, 'product_cat' ) );
echo $product->get_categories( ', ', '<span class="posted_in">' . ' ', '.</span>' );
?>
</h4>
<?php echo $product->get_sku(); ?>
<?php echo apply_filters( 'woocommerce_short_description', $post->post_excerpt ) ?>
<?php if ( $price_html = $product->get_price_html() ) : ?>
<span class="price"><?php echo $price_html; ?></span>
<?php endif; ?>
<div class="product-rating">
<?php if ($average = $product->get_average_rating()) : ?>
<?php echo '<div class="star-rating" title="'.sprintf(__( 'Rated %s out of 5', 'woocommerce' ), $average).'"><span style="width:'.( ( $average / 5 ) * 100 ) . '%"><strong itemprop="ratingValue" class="rating">'.$average.'</strong> '.__( 'out of 5', 'woocommerce' ).'</span></div>'; ?>
<?php endif; ?>
</div>
<?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
</div>
</div>
</li>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul><!--/.products-->

How to show first post content and links for older post only in WordPress

I am trying to create news paper web site and need help to show the content of recent post and title of old posts only... here is my code
<?php
$args = array(
'orderby' => 'id',
'hide_empty' => 1,
'order' => 'ASC'
);
$categories = get_categories($args);
foreach($categories as $category) { ?>
<div class="newsdiv">
<?php
echo '<center><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></center> <br/> ';
$post_args = array(
'numberposts' => 3,
'category' => $category->term_id
);
$posts = get_posts($post_args);
foreach($posts as $post) {?>
<?php echo"*";the_title(); ?>
<div class="entry">
<?php the_content(); ?>
</div>
<?php
}
echo '<dd class="view-all"> <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>Read more </a></dd>';
echo '</dl>';
?>
</div>
<?php
}
?>
you can try the codex previous post
<?php previous_post_link( $format, $link, $in_same_cat = false, $excluded_terms = '', $taxonomy = 'category' ); ?>
you can use it in simpler and/or complex way something like this.
<?php previous_post_link(); ?>
for more info you can go through with codex http://codex.wordpress.org/Function_Reference/previous_post_link

Categories