displaying the filtered custom taxonomy - php

Still trying to figure it out how could I filter the custom taxonomy and display it. the JS is for firing the URL generated is working fine. the problem is the results page items are not showing. I have this code so far:
<?php
$args = array(
'orderby' => 'slug',
'order' => 'ASC',
'parent' => 0,
'hide_empty' => false
);
$terms = get_terms([
'taxonomy' => 'pdf_cat',
'hide_empty' => false,
]);
foreach( $terms as $term ){
echo '<option class="ctg" value="'. get_term_link($term->term_id) .' ">' . $term->name . '</option>';
}
//NEXT CODE IS GET THE CATEGORY ID AFTER CLICKED TO DISPLAY
<?php
$terms = get_terms([
'taxonomy' => 'pdf_cat',
'hide_empty' => false,
]); // get the category from query
$terms = $term[0];
$term_name = get_term_name($term->name); // get the category name
$term_id = get_term_ID($term->term_id); // get the catehory ID
?>
<?php
$paged = get_query_var('paged', 1);
$args = array( // asigned the functions inside the array
'paged' => $paged,
'post_type' => 'pdf',
'taxonomy' => $term_id,
);
$query = new WP_Query($args); // get the functions inside the assigned name
global $query_string; //Post the specify numbers per page
query_posts( $query_string . "&posts_per_page=12&paged=".$paged );
while ( have_posts() ) : the_post()
?>
and here is the js to fire the dropdown menu link when clicked:
<script type="text/javascript">
$("#form").change(function(){
var url = $(this).val();
location.assign(url);
});
</script>
//NEXT CODE IS TO THE DISPLAY
<div class="pdf-box">
<?php
$file = get_field('file');
if( $file ): ?>
<a href="<?php echo $file['url']; ?>" target="_blank"><?php echo $file['file']; ?>
<div class="service-list-col1">
<i class="fa-file-o"></i>
</div>
<div class="service-list-col2">
<p><?php the_time('Y.m.d'); ?></p>
<h3><?php the_title(); ?></h3>
</div>
</a>
<?php endif; ?>
</div>

Related

Display the filtered existing post through category button when its clicked

Anyone can help me. "my goal is to display the filtered existing post through category button when its clicked." I have a custom post added in my functions.php, through get_terms I've displayed the categories listed on my custom post. on my page, the echoed category is form as dropdown, so when you choose and clicked the specific category, the filter function will occurred and display the specific post. here are my code so far:
here is the dropdown category list:
<select class="custom-select sources" name="" id="dynamic_select">
<div class="options">
<option value="<?php echo home_url('/newsletter'); ?>" selected>All</option>
<?php
$args = array(
'orderby' => 'slug',
'order' => 'ASC',
'parent' => 0,
'hide_empty' => false
);
$terms = get_terms([
'taxonomy' => 'pdf_cat',
'hide_empty' => false,
]);
foreach( $terms as $term ){
echo '<option class="ctg" value="'. get_term_link($term->term_id) .' ">' . $term->name . '</option>';
}
?>
</div>
</select>
when I clicked the category, it supposed to proceed to pdf_cat.php where the filter codes is located, but it won't connect.
here is the page where the categories was being clicked and displayed:
<?php
$terms = get_terms();
$terms = $terms[0];
$termsname = get_terms_name($terms->term_id);
$termsid = get_terms_ID($termsname);
?>
<?php
$paged = get_query_var('paged', 1);
$args = array(
'paged' => $paged,
'post_type' => 'pdf',
'taxonomy' => 'pdf_cat',
'hide_empty' => false,
);
$query = new WP_Query($args);
global $query_string;
query_posts( $query_string . "&posts_per_page=12&paged=".$paged );
while ( have_posts() ) : the_post()
?>
from here, I can't able to display the filtered post
I added this Javacript:
<script type="text/javascript">
$("#form").change(function(){
var url = $(this).val();
location.assign(url);
});
</script>

Integrating isotope.js with Wordpress loop

I've been trying to get isotope.js working on a Wordpress site. I've been following this tutorial https://www.aliciaramirez.com/2014/03/integrating-isotope-with-wordpress/ and have been able to get it all functioning. For my design, I'm trying to add <div class="grid-sizer"></div> every four posts that are called. I've been referring to this question: Wrap every 4 posts in a custom wordpress loop with a div but cannot seem to figure out the proper placement for the count and i statements. Can anyone help me figure this out? Here's my loop right now:
<?php
$terms = get_terms( array(
'taxonomy' => 'solutions',
'parent' => 0
)
); // get all categories, but you can use any taxonomy
$count = count($terms); //How many are they?
if ( $count > 0 ){ //If there are more than 0 terms
foreach ( $terms as $term ) { //for each term:
echo "<li><a href='#' data-filter='.".$term->slug."'>" . $term->name . "</a></li>\n";
//create a list item with the current term slug for sorting, and name for label
}
}
?>
</ul>
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => asc,
'tax_query' => array(
array(
'taxonomy' => 'solutions',
'field' => 'term_id',
'terms' => get_queried_object()->term_id,
),
) );
$the_query = new WP_Query( $args ); //Check the WP_Query docs to see how you can limit which posts to display ?>
<?php if ( $the_query->have_posts() ) : ?>
<div id="isotope-list">
<?php
while ( $the_query->have_posts() ) : $the_query->the_post();
$termsArray = get_the_terms( $post->ID, "solutions" ); //Get the terms for this particular item
$termsString = ""; //initialize the string that will contain the terms
$i = 0;
foreach ( $termsArray as $term ) { // for each term
$termsString .= $term->slug.' '; //create a string that has all the slugs
}
if($i%4 == 0) {
echo "<div class='grid-sizer'> </div>";
}
?>
<div class="<?php echo $termsString; ?>item">
<p class="product-image"><a href="<?php the_permalink(); ?>" ><img src="<?php the_field("product_image") ?>" alt="<?php the_title(); ?>" class="solution-image" /></a></p>
<h4 class="product-name">
<?php the_title(); ?>
</h4>
</div>
<?php $i++; ?>
<!-- end item -->
<?php endwhile; ?>
</div>
<!-- end isotope-list -->
<?php endif; ?>
Here's the resulting code - thanks for misorude's help!
<?php
$terms = get_terms( array(
'taxonomy' => 'solutions',
'parent' => 0
)
); // get all categories, but you can use any taxonomy
$count = count($terms); //How many are they?
if ( $count > 0 ){ //If there are more than 0 terms
foreach ( $terms as $term ) { //for each term:
echo "<li><a href='#' data-filter='.".$term->slug."'>" . $term->name . "</a></li>\n";
//create a list item with the current term slug for sorting, and name for label
}
}
?>
</ul>
<?php
$i = 0;
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => asc,
'tax_query' => array(
array(
'taxonomy' => 'solutions',
'field' => 'term_id',
'terms' => get_queried_object()->term_id,
),
) );
$the_query = new WP_Query( $args ); //Check the WP_Query docs to see how you can limit which posts to display ?>
<?php if ( $the_query->have_posts() ) : ?>
<div id="isotope-list">
<?php
while ( $the_query->have_posts() ) : $the_query->the_post();
$termsArray = get_the_terms( $post->ID, "solutions" ); //Get the terms for this particular item
$termsString = ""; //initialize the string that will contain the terms
foreach ( $termsArray as $term ) { // for each term
$termsString .= $term->slug.' '; //create a string that has all the slugs
}
if($i%4 == 0) {
echo "<div class='grid-sizer'> </div>";
}
?>
<div class="<?php echo $termsString; ?>item">
<p class="product-image"><a href="<?php the_permalink(); ?>" ><img src="<?php the_field("product_image") ?>" alt="<?php the_title(); ?>" class="solution-image" /></a></p>
<h4 class="product-name">
<?php the_title(); ?>
</h4>
</div>
<?php $i++; ?>
<!-- end item -->
<?php endwhile; ?>
</div>
<!-- end isotope-list -->
<?php endif; ?>

How to display all available custom post categories in WordPress?

How to display all categories of a custom post type on home screen without listing the items.
I already created the custom post type and it's categories, now I need to display all the categories on my home page as links to the each category page. Can someone help please?
You can use now get_categories
Here is an example of code:
<?php
$args = array(
'taxonomy' => 'Your Taxonomy Name',
'hide_empty' => 0,
'orderby' => 'name'
);
$cats = get_categories($args);
foreach($cats as $cat) {
?>
<a href="<?php echo get_category_link($cat->slug); ?>">
<?php echo $cat->name; ?>
</a>
<?php
}
?>
Remember write your taxonomy name as you registered, in here 'Your Taxonomy Name'
e.g. product_cat, blog_cat etc
Hope this will help you.
$cat_args = array(
'taxonomy' => 'your-custom-post', // your custom post type
);
$custom_terms = get_categories($cat_args);
echo print_r($custom_terms);
<?php
$terms = get_terms( 'taxonamy_name', array(
'orderby' => 'count',
'hide_empty' => 0
) );
foreach($terms as $term)
{
echo $term->name;
}?>
</ul>
</div>
$args = array( 'post_type' => 'post', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;

How to restrict what tags are fetched in WordPress

I have this PHP script to fetch tags (as we use WordPress as a CMS, the tags are "categories" for our partners).... However, I'm trying to free up "tags" to be used on blog posts - but currently the code below fetches ALL tags - how can I restrict this to only tags I specify (happy to enter each Tag ID)
Here is the code:
<?php
$tags = get_tags('order=ASC&orderby=name);
foreach ( $tags as $tag ) { ?>
<?php $tag_name = $tag->slug;
echo '<div class="col-md-12 padbot50" id="tabs-' . $tag->slug . '">';
echo '<div class="green_txt font20">' .$tag->name. '</div>';
//echo $tag_name;
$args = array( 'posts_per_page' => 10, 'cat'=> 11, 'tag' => $tag_name, 'orderby' => 'slug', 'order' => 'ASC' );
query_posts( $args );
// The Loop
?>
<div class="row">
<?php
while ( have_posts() ) : the_post();
$thumb_id = get_post_thumbnail_id();
$thumb_url_array = wp_get_attachment_image_src($thumb_id, 'medium', true);
$thumb_url = $thumb_url_array[0];
$post_id = get_the_ID();
?>
<div class="col-md-6 col-sm-5 col-xs-7 pad10 center"> <a href="<?php the_permalink(); ?>">
<div class="partners_container">
<div class="row" style="height:125px;">
<div class="col-md-12 col-xs-12"><div class="left"> <img src="<?php echo $thumb_url; ?>" height="100%" alt="Volo Commerce Partners - Multichannel Automated Back Office Software. eBay, Amazon, Rakuten" title="Volo Commerce Partners - Multichannel eCommerce Software. Stock & Inventory Management"/><div class="partner-more-icon"></div></div></div>
</div>
<div class="padtop10" style="text-align:left;font-weight:bold;color:#f08f00!important;"> <?php echo '<div>' . the_title() . '</div>';?> </div>
<div class="padtop10 left"> <?php echo '<div>' . the_field('partner_page_description', $post_id ) . '</div>';?> </div>
</div>
</a> </div>
<?php
endwhile;
?>
</div>
</div>
<?php
wp_reset_postdata(); }?>
If anyone could be so kind to tell me how I fetch ONLY certain tag ID's - that would be absolutely perfect!
Thanks muchly!
This is a basic example of WP_Query which should get you started:
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 10,
'orderby' => 'slug',
'order' => 'ASC',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'term_id',
'field' => 11
),
array(
'taxonomy' => 'tag',
'field' => 'term_id',
'field' => array( /* Your Tag IDs */ ),
'operator' => 'IN'
)
)
);
$post_query = new WP_Query( $args );
if ( $post_query->have_posts() ) {
while ( $post_query->have_posts() ) :
$post_query->the_post();
// Inner loop stuff code
endwhile;
endif;
wp_reset_postdata();?>
get_tags function accepts include argument. You could pass by it ids of tags you want to get.
$tags = get_tags('order=ASC&orderby=name');
change to
$tags = get_tags('order=ASC&orderby=name&include=12,45,67');
12, 45 and 67 are ids of tags
get_tags on codex

How to display the most recent post of each category of a custom post type in wordpress

This question seems to have been asked in some varieties, But mine is quite specific.
I need to display the most recent post of all categories of a custom post type called "products". I need to display the category instead of the title and link to that cat's archive.
The problem is, What I am getting instead is only one post of that general post type. Any thoughts?
Here is my starting point:
<?php
$query = new WP_Query( array(
'posts_per_page' => 12,
'post_type' => 'products',
'post_status'=>'publish',
'orderby'=>'post_date',
'order' => 'DESC',
'cat' => $category->term_id,
'count'=>1,
));
if ( have_posts() ) : while ( $query->have_posts()) : $query->the_post(); ?>
<div class="col-sm-6 col-md-4">
<?php if ( has_post_thumbnail()) : ?>
<?php the_post_thumbnail('product', array('class' => 'img-responsive center-block')); ?><h3><?php the_title(); ?></h3>
<?php else : ?>
<!--<img class="img-responsive center-block" src="<?php echo THEME_DIR; ?>/img/default-thumb.png" alt="<?php the_title(); ?>" />--><img class="img-responsive " data-src="holder.js/100%174/auto" alt="Generic placeholder image"><h3><?php the_title(); ?></h3>
<?php endif; ?>
</div><!-- col-sm-6 -->
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
</div><!-- end row -->
<ul class="pager">
<li class="previous"><?php previous_posts_link(); ?></li>
<li class="next"><?php next_posts_link(); ?></li>
</ul>
<?php else : ?>
<h1>Not Found</h1>
<p>Click Here to return to the Home Page</p>
<?php endif; ?>
The custom post type was created via a 3rd plug into Super CPT Plugin
Here is the code:
add_action( 'after_setup_theme', 'setup_data_structures' );
function setup_data_structures() {
if ( ! class_exists( 'Super_Custom_Post_Type' ) )
return;
$products = new Super_Custom_Post_Type( 'products', 'Product', 'Products' );
# Test Icon. Should be a square grid.
$products->set_icon( 'suitcase' );
# Taxonomy test, should be like categories
$product_category = new Super_Custom_Taxonomy( 'product-category', 'Product Category', 'Product Categories', 'category' );
# Connect both of the above taxonomies with the post type
connect_types_and_taxes( $products, array($product_category) );
# Add a meta box with every field type
$products->add_meta_box( array(
'id' => 'product-fields',
'context' => 'normal',
'fields' => array(
'client-name' => array('column' => true ),
'product-description' => array( 'type' => 'wysiwyg' )
)
) );
$products->add_to_columns( 'product-category' );
This should help you out. The follow code displays 5 of each category. Just switch the 5 to how many ever you wish to display from each category.
<?php
$cat_args = array(
'orderby' => 'name',
'post_type' => 'products',
'order' => 'ASC',
'child_of' => 0
);
$categories = get_categories($cat_args);
foreach($categories as $category) {
echo '<dl>';
echo '<dt> <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></dt>';
$post_args = array(
'numberposts' => 5,
'category' => $category->term_id
);
$posts = get_posts($post_args);
foreach($posts as $post) {
?>
<dd><?php the_title(); ?></dd>
<?php
}
echo '<dd class="view-all"> <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>View all posts in ' . $category->name.'</a></dd>';
echo '</dl>';
}
?>
<?php
$cats = array('3d-mail', 'boxes', 'folders', 'binders');
$exclude_posts = array();
foreach( $cats as $cat )
{
// build query argument
$query_args = array(
'category_name' => $cat,
'showposts' => 1,
'post_type' => 'products',
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC'
);
// exclude post that already have been fetched
// this would be useful if multiple category is assigned for same post
if( !empty($exclude_posts) )
$query_args['post__not_in'] = $exclude_posts;
// do query
$query = new WP_Query( $query_args );
// check if query have any post
if ( $query->have_posts() ) {
// start loop
while ( $query->have_posts() ) {
// set post global
$query->the_post();
// add current post id to exclusion array
$exclude_posts[] = get_the_ID();
<?php the_content(); ?>
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
}
?>

Categories