I have created a set of pages. In first page product.php all the custom categories are shown (title and image). If a user clicks on it then it moves to the taxonomy-product_categories page where products are shown of the specific category. Now i want that if user clicks on the product then it goes to single-product.php page.
code is here
<?php
get_header();
$slug = get_queried_object()->slug; // get clicked category slug
$name = get_queried_object()->name; // get clicked category name
$tax_post_args = array(
'post_type' => 'products', // your post type
'posts_per_page' => 999,
'orderby' => 'id',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'product_categories', // your taxonomy
'field' => 'slug',
'terms' => $slug
)
)
);
?>
<div id="main-wrap">
<div class="container">
<?php
$counter = 1;
$tax_post_qry = new WP_Query($tax_post_args);
if ($tax_post_qry->have_posts()) {
while ($tax_post_qry->have_posts()) {
$tax_post_qry->the_post();
$the_url = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), $type);
//var_dump($the_url);
if ($counter % 4 == 0) {
echo '<div class="row product-gallery">';
}
?>
<div class="col-sm-3">
<div class="product-warp">
<div class="product">
<img src="<?php echo $the_url[0] ?>" title="" alt="">
</div>
<div class="product-name">
<h5>
<a href="">
<?php echo get_the_title();
; ?>
</a>
</h5>
</div>
</div>
</div>
<?php
if ($counter % 4 == 0) {
echo '</div>';
}
?>
<?php
}
}
?>
</div>
</div>
<?php get_footer(); ?>
this is the place where user clicks then it should go to single-product.php
<a href="#"><img src="<?php echo $the_url[0] ?>" title=""
Somebody guide me step by step please
If your post_type => products Then your file name must be like this single-products.php
Read this article
And for precautions (if the page not open yet. means give 404 error) go to
admin panel
settings
permalinks
First save permalinks as plain and refresh your site home page.
Then save permalinks as post_name and again refresh your home page
Hope this will help you.
Please Try
'post_type' => 'products'
Your Post type is “products”.
And wordpress ruls is “single-{post_type}.php”.
You make “single-products.php” and remove “single-product.php”
https://codex.wordpress.org/Post_Type_Templates
Related
<? get_header() ;?>
<?
$cat_param = 'select-category';
if (strpos($_SERVER['REQUEST_URI'], $cat_param)) {
$category_id = htmlspecialchars($_GET[$cat_param]);
} else {
$category_id = null;
}
?>
<article id="post-<? the_ID(); ?>" <? post_class('bg-white'); ?> data-file="<? echo basename(__FILE__); ?>">
<div class="wrap-outer">
<div class="py-5 py-md-6 py-lg-7 py-xl-8 wrap-inner">
<div class="container-fluid container-lg">
<div class="row">
<div class="col-12 col-md-8">
<form method="" action="<? echo get_post_type_archive_link('pcm_review'); ?>/">
<div class="form-group posts-filter">
<div class="input-group">
<div class="input-group-prepend">
<label class="input-group-text" for="review-select-category"><i class="fas fa-filter"></i></label>
</div>
<?
$args_cats = array(
'show_option_all' => 'Show All',
'orderby' => 'name',
'order' => 'ASC',
'show_count' => 0,
'echo' => 1,
'selected' => $category_id,
'name' => $cat_param,
'id' => $cat_param,
'class' => 'select-category form-control ml-auto',
'taxonomy' => 'category',
'hide_if_empty' => false,
'option_none_value' => -1,
'value_field' => 'term_id',
'required' => false,
);
wp_dropdown_categories( $args_cats );
?>
</div>
</div>
<script type="text/javascript">
jQuery(function() {
$('#select-category').change(function() {
this.form.submit();
});
});
</script>
<hr>
</form>
<?
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args_reviews = array(
'post_type' => 'pcm_review',
'cat' => $category_id,
'paged' => $paged,
);
$query_reviews = new WP_Query( $args_reviews );
if ( $query_reviews->have_posts() ) {
while ( $query_reviews->have_posts() ) {
$query_reviews->the_post();
$content = get_post_meta(get_the_ID(), 'pcm_review_content', true);
$name = get_post_meta(get_the_ID(), 'pcm_review_name', true);
$rating_str = get_post_meta(get_the_ID(), 'pcm_review_rating', true);
$rating_int = intval($rating_str);
$rating_star = '<i class="fas fa-star mr-1 text-warning"></i>';
$rating_stars = str_repeat($rating_star, $rating_int);
?>
<div id="post-<? the_ID(); ?>" <? post_class(''); ?>>
<p>
<span class="review-stars"><? echo $rating_stars; ?></span><br>
<b><? the_title(); ?></b>
</p>
<div class="review-content">
<? echo $content; ?>
</div>
<div class="justify-content-between row small text-muted">
<div class="col-auto">
<i class="fas fa-user mr-2"></i><span class="review-name"><? echo $name; ?></span>
</div>
<div class="col-auto">
<i class="fas fa-tag mr-2"></i><span class="review-categories"><? echo get_the_category_list(', '); ?></span>
</div>
</div>
<hr>
</div>
<?
}
the_posts_pagination();
} else {
pcm_no_results();
}
wp_reset_postdata();
?>
</div>
<div class="col-12 col-md-4">
<div class="sidebar-wrap sticky-top">
<? get_template_part('template-parts/aside-blog'); ?>
<? get_template_part('template-parts/aside-reviews'); ?>
</div>
</div>
</div>
</div>
</div>
</div>
</article>
<? get_footer() ;?>
Struggling with a pagination issue and would really appreciate any help. I’m not making any progress in searching here or other forums so thought I’d ask for help.
Background
I have a custom post type for testimonials/reviews which supports the category taxonomy. My template file includes a dropdown/select-list to allow the visitor to choose a category if they’d like to filter the reviews and only see reviews for their selected category.
Problem
Okay so let’s say I have 10 total reviews and only 5 of them are assigned to ‘Category A’. If the visitor filters the posts by ‘Category A’, they will still see pagination links as if there are 10 posts instead of the 5 that belong to ‘Category A’. I hope that makes sense.
No errors in console or debug log.
Code Explained
Line 5 starts a little snippet just to make things easier later in th file. Setting a var to the value of a category ID if the category parameter is found in the URL. If not, we set the category ID to null.
Line 20 start the form that contains the wp_dropdown_categories() function which outputs a list of my categories. We set the selected param to the same value as the category ID from line 5. The form action attribute is set to the main archive url for the reviews so that if a category is selected and the user is on any page other than the first, they’re pointed back to the root review archive url.
Line 47 just forces the form to submit when an option from the dropdown is selected.
Line 57 is just telling WordPress which page we’re currently on, then the rest of the query.
By adding the 'total'=> $query_reviews->max_num_pages parameter to my pagination function, this gets outputs the correct number of pagination links since it uses the value of max_num_pages of my query rather than the main query. Crisis averted.
I know this question was asked so many times, but none of the solutions are working for me.
I want to display my categories on the website with their thumbnails together.
I tried to use category imagies plugin, but for some reason this plugin stopped working and doesn't give me the opportunity to attach picture to the category (the add image button isn't shown in the category settings).
After this I created custom field, where I made a taxonomy is equal to category. With this I could pair images with categories, but I don't know how to add it to the code.
Here is my code:
<section id="categories">
<div class="center-box">
<div class="categories">
<?php
$categories = get_categories(array ('parent' => 0));
foreach($categories as $category) { ?>
<div class="category-box">
<a class="category-img" href="<?php echo get_category_link($category->term_id); ?>">
<img class="img" src="<?php echo z_taxonomy_image_url($category->term_id); ?>"/>
<div class="category-name">
<h3> <?php echo $category->name; ?> </h3>
</div>
</a>
</div>
<?php
$args = array(
'numberposts' => 1,
'orderby' => 'date',
'order' => 'DESC',
'category' => $category->term_id, );
?>
<?php } ?>
</div>
</div>
</section>
It still has the previous category imagies plugin code which doesn't work anymore.
Someone could help me how to display the categories with image?
Thank you!
I'd suggest using the Advanced Custom Fields plugin for this, if you are not already. Under the working assumption that you are, you could do the following:
<section id="categories">
<div class="center-box">
<div class="categories">
<?php
$categories = get_categories(array ('parent' => 0));
foreach($categories as $category) { ?>
<div class="category-box">
<a class="category-img" href="<?php echo get_category_link($category->term_id); ?>">
<img class="img" src="<?php echo wp_get_attachment_image_src( get_field( 'thumbnail', "term_$category->term_id" ), 'small' ) ?>"/>
<div class="category-name">
<h3> <?php echo $category->name; ?> </h3>
</div>
</a>
</div>
<?php
$args = array(
'numberposts' => 1,
'orderby' => 'date',
'order' => 'DESC',
'category' => $category->term_id, );
?>
<?php } ?>
</div>
</div>
I'm reconfiguring a header for a Wordpress Theme. I just want to do this simple function:
Float the Logo to the center of the header when a search field is not present.
There used to be widgets in place where the search field is when a visitor navigates to a products page. I'd like to instead remove the rule that was in place for the widgets and float the Logo center, unless there is a search box, then I'd like to keep the Logo at the left.
Here is the code:
<div id="logo" class="col-sm-3">
<?php
$logourl = SNSSIMEN_THEME_URI.'/assets/img/logo.png';
if ( snssimen_get_option('header_logo') && snssimen_get_option('header_logo','','url') !='' ){
$logourl = snssimen_get_option('header_logo','','url');
}
?>
<a href="<?php echo esc_url( home_url('/') ) ?>" title="<?php echo esc_attr(get_bloginfo( 'sitename' )); ?>">
<img src="<?php echo esc_url($logourl); ?>" alt="<?php echo esc_attr(get_bloginfo( 'sitename' )); ?>"/>
</a>
</div>
<div class="header-right col-sm-9">
<div class="header-right-inner">
<div class="row">
**<?php if ( $snssimen_topHeaderSidebar == 'header_sidebar'):?>
<?php if(is_active_sidebar('header_sidebar')) dynamic_sidebar('header_sidebar');?>**
<?php else: // Header sidebar search box?>
<div class="col-md-8">
<div id="headerRightSearchForm">
<?php // Get Woocommerce categoies
$args = array(
'taxonomy' => 'product_cat',
'orderby' => 'name',
'show_count' => 0,
'pad_counts' => 0,
'hierarchical' => 0,
'title_li' => '',
'hide_empty' => 0
);
$all_categories = get_categories($args);
?>
The coding in the asteriks is the coding for what was there, but is not there anymore. Any suggestions? Regards.
I have created a custom taxonomy for products. In custom taxonomy user can enter name and can upload a image. In the products page I can get the image and title of category. When user clicks on this it moves to next page where description of post and title is shown.
I also want to get image also in this next page.
Here is the code: For showing custom categories
foreach($terms as $term) {
$prod_meta = get_option("taxonomy_term_".$term->term_id);
if(($counter % 4) == 0) {
echo '<div class="row product-gallery">';
}
?>
<div class="col-sm-3">
<div class="product-warp">
<div class="product">
<img src="<?php echo $prod_meta['img']; ?>" title="" alt="">
</div>
<div class="product-name">
<h5>
<a href="<?php echo get_term_link($term->slug, 'product_categories') ?>">
<?php echo $term->name; ?>
</a>
</h5>
</div>
</div>
</div>
For product detail page
<?php
get_header();
$slug = get_queried_object()->slug; // get clicked category slug
$name = get_queried_object()->name; // get clicked category name
$tax_post_args = array(
'post_type' => 'products', // your post type
'posts_per_page' => 999,
'orderby' => 'id',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'product_categories', // your taxonomy
'field' => 'slug',
'terms' => $slug
)
)
);
$tax_post_qry = new WP_Query($tax_post_args);
if ($tax_post_qry->have_posts())
while($tax_post_qry->have_posts()) :
$tax_post_qry->the_post(); ?>
<div class="row product-details">
<div class="col-sm-7">
<div class="fire-product">
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
Ok here's the deal I'm creating a custom WP theme for an mobile app. I want to display 2 download links for iOS and Android in a section on home page.
I created a custom post type called downloads and associated custom meta boxes to that custom post type.
I created 2 posts, one is with a ios download link and the other one is a play store link.
Now I have a problem when I query this post type on front page, both meta boxes display the output although only 1 has the link, see screenshoot:
This is my code at the moment:
<?php
$downloads = get_posts(array('post_type'=>'downloads','posts_per_page'=>2, 'order'=>'ASC'));
if ($downloads) { ?>
<?php foreach ($downloads as $post) { setup_postdata( $post ); ?>
<a href="<?php echo(types_render_field("download-play-store", array('raw' => true) )) ; ?>" target="_blank">
<?php the_post_thumbnail( 'medium', array( 'class' => '')); ?> <!-- featured img -->
</a>
<a href="<?php echo(types_render_field("download-app-store", array('raw' => true) )) ; ?>" target="_blank">
<?php the_post_thumbnail( 'medium', array( 'class' => '')); ?> <!-- featured img -->
</a>
</div>
<?php } wp_reset_postdata(); ?>
<?php } ?>
How to set some "if" condition which will help not to display the other field if there is no link added to it?
Thanks
EDIT: i dont have much experience using the types plugin. But this should run without any issues.
<?php
$downloads = get_posts(array('post_type'=>'downloads','posts_per_page'=>2, 'order'=>'ASC'));
if ($downloads) {
foreach ($downloads as $post) {
setup_postdata( $post );
if(types_render_field("download-play-store", array('raw' => true) ))){ ?>
<a href="<?php echo(types_render_field("download-play-store", array('raw' => true) )) ; ?>" target="_blank">
<?php the_post_thumbnail( 'medium', array( 'class' => '')); ?> <!-- featured img -->
</a>
<?php }
if(types_render_field("download-app-store", array('raw' => true) )){?>
<a href="<?php echo(types_render_field("download-app-store", array('raw' => true) )) ; ?>" target="_blank">
<?php the_post_thumbnail( 'medium', array( 'class' => '')); ?>
</a>
<?php }?>
</div>
<?php } wp_reset_postdata();
} ?>