So i try to show a custom taxonomy on the frontend.
The custom taxonomy is from a plugin i use and is called wcs-instructor.
I just want to show a Single Page with the content of the taxonomy by listing the names and images of the Instructors, the image is a custom field.
When i try to Query the taxonomy with get_term or with WP_Term_Query everything shows up nicely as long as im logged in with my Admin Account (havent actually tested another account).
When i log out and open the Page again its blank.
The structur is as follows.
Custom Taxonomy: wcs-instructor
Page Endpoint: /trainer
Custom Single page: page-trainer.php
get_header();
?>
<main id="primary" class="site-main">
<?php
while ( have_posts() ) :
the_post();
get_template_part( 'template-parts/content', 'trainer' );
endwhile;
?>
</main><!-- #main -->
<?php
get_footer();
content-trainer.php (striped some html thats unnecessary)
<div class="content">
<?php
$term_query = new WP_Term_Query(
array( 'taxonomy' => 'wcs-instructor',) );
if ( ! empty( $term_query->terms ) ) {
foreach ( $term_query ->terms as $term ) {
echo $term->name;
}}
else {
echo 'No term found.';
}
?>
</div>
I know that the code above only gives me the results that are not empty, but the taxonomy got entries that aren't.
Also the Code below also doesn't work.
<?php
$terms = get_terms( array(
'taxonomy' => 'wcs-instructor',
'hide_empty' => false,
) );
foreach ( $terms as $term )
{
echo $term->term_id;
echo '<br>';
echo $term->name;
}
?>
And i just cant find the reason why because when i go to /wcs-instructor/testinstructor i can view the content even when im not logged in.
Any hints on how to debug this behavior or why it's behaving like this in the first place?
Sounds like that taxonomy might have the publicly_queryable arg set to false. Not sure which plugin you are using to manage your custom taxonomies but the CPT UI Plugin has a setting for that argument.
The Public Queryable setting for taxonomies in CPT UI plugin
Related
I've been looking through answers on how to get this work but can't find a solution that works.
I have organised my plugins so that each upload automatically generates a custom field value depending on the uploaded information.
Screenshot of custom fields
I want it to be so that when I type the field name, i.e "track_bpm" into the description or short description of a product in Woocommerce that it then shows the value. That way I can automate the creation of product pages. Currently, it doesn't change.
I've added:
<?php echo get_post_meta($post->ID, 'key', true); ?>
To:
<?php while ( have_posts() ) : the_post(); ?>
<?php
if ( flatsome_product_block( get_the_ID() ) ) {
wc_get_template_part( 'content', 'single-product-custom' );
} else {
wc_get_template_part( 'content', 'single-product' );
}
?>
<?php echo get_post_meta($post->ID, 'key', true); ?>
<?php endwhile; // end of the loop. ?>
To the single-product.php file in the loop with the 'key' value replaced with each custom field name, but this just adds the value to the top or bottom of the page depending on the location in the loop.
Any advice here would be really appreciated!
I am developing a wordpress website which uses woocommerce for e-commerce functionality. I have 3 categories on the website and each one will have it's own template assigned for products within these categories.
I have created the templates and have got two of them working fine. However I'm not sure how to call the third template within my single-product.php file which contains the following code to change the templates depending on what category the product is assigned to:
<?php while ( have_posts() ) : the_post(); ?>
<?php global $post;
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ) $categories[] = $term->slug;
if ( in_array( 'legal', $categories ) ) {
woocommerce_get_template_part( 'content', 'single-product-legal' );
} else {
woocommerce_get_template_part( 'content', 'single-product-merc' );
} ?>
<?php endwhile; // end of the loop. ?>
the templates i have are:
single-product-legal (custom template)
single-product-merc (default woocommerce template)
single-product-show (custom template)
The categories are legal, show and merchandise.
I need help with the php code so I can switch between the 3 templates. Not sure if I should use a switch statement, or how to implement it or if I could use elseif or how to implement that. Even if there's a completely different way to achieve this, I'd love to know.
Any pointers would be appreciated.
The best it to use elseif:
if ( in_array( 'legal', $categories ) ) {
woocommerce_get_template_part( 'content', 'single-product-legal');
}elseif (in_array('show', $categories)){
woocommerce_get_template_part('content', 'single-product-show');
}else {
woocommerce_get_template_part( 'content', 'single-product-merc');
}
Rather than adding a seperate elseif for merchandise you can just do it in the else like above because by default if it is not legal or show it must be merchandise, but you may just add another elseif as well with the same basic structure.
I'm using the following wp_query on a few pages in Wordpress, and as you can see I am trying to ensure the query is paginated. Everything is working successfully on a custom page template page-articles.php, however I'm unable to get the same results on the archive.php template.
The pagination link renders successfully (e.g mydomain.com/category/my-cat/page/2) however upon clicking the link does not work, it just throws a 404 error? How can these links not go anywhere?
I'm assuming there is some issue with using custom wp_query on the archive.php template?
Thanks!
Query
$args = array(
'post_type' => 'post',
'posts_per_page' => 1,
'paged' => $paged,
'orderby' => 'date',
'order' => 'DESC'
);
$articles = new WP_Query($args );
?>
Loop
<?php if ( $articles->have_posts() ) : ?>
<?php while ( $articles->have_posts() ) : $articles->the_post(); ?>
Posts here!
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Pagination
<nav>
<div class="prev"><?php echo get_previous_posts_link( 'Previous', $articles->max_num_pages ); ?></div>
<div class="next"><?php echo get_next_posts_link( 'Next', $articles->max_num_pages ); ?></div>
</nav>
After a bit of digging around, and with help from this article, the below solution solves the issue. Just place this in your functions.php file and thats it. The below implementation works for archives of custom post types, as well as categories.
/**
* Wordpress has a known bug with the posts_per_page value and overriding it using
* query_posts. The result is that although the number of allowed posts_per_page
* is abided by on the first page, subsequent pages give a 404 error and act as if
* there are no more custom post type posts to show and thus gives a 404 error.
*
* This fix is a nicer alternative to setting the blog pages show at most value in the
* WP Admin reading options screen to a low value like 1.
*
*/
function custom_posts_per_page( $query ) {
if ( $query->is_archive('cpt_name') || $query->is_category() ) {
set_query_var('posts_per_page', 1);
}
}
add_action( 'pre_get_posts', 'custom_posts_per_page' );
Yes, it is.
It is because main query of archive.php is kept unchanged while you are playing with WP_QUERY. Try use query_posts() in archive.php.
query_posts($args);
and then default loop (instead of $articles)
<?php if ( have_posts() ) : ?>
<?php while (have_posts() ) : the_post(); ?>
Posts here!
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
I need display the custom taxonomy thumnbail and name on category page. I have this code :
<h1><?php single_cat_title(); ?></h1>
<?php
$taxonomy = 'kategorie_goralskie';
$terms = get_terms($taxonomy, array('parent' => 0, 'orderby' =>
'meta_value', 'order' => 'DESC') ); // Get all top level terms of a
taxonomy
if ( $terms && !is_wp_error( $terms ) ) :
?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="col-md-3">
<?php the_title();?>
</div>
<?php endwhile; ?>
<?php endif; ?>
It shows only name of child (its ok) I must show thumnail too. How i can do it ? I tried in many ways
enter image description here
In front-page.php everything is ok, i see taxonomy parent - just look : enter image description here
You need to call the_post_thumbnail function somewhere in your loop to display thumbnail. you can pass the size of the thumbnail you whant in function parameter.
See the detail of this function here : https://developer.wordpress.org/reference/functions/the_post_thumbnail/
I am creating a website that integrates a portfolio which uses custom post types, this was done based off of this tutorial.
So far it is exactly what I am looking for and works great except for one small detail. In order to fetch the posts from the new custom post type the author of the tutorial used the query_posts() codex. So the top of my portfolio page looks like this:
<?php
/* Template Name: Portfolio */
get_header();
query_posts('post_type=portfolio&posts_per_page=10');
?>
What I gather is that this declares "get posts from "post type" portfolio and show 10 per page". My problem is that I can't go get content from my portfolio page. It seems that now my portfolio page only fetches the content from the custom post type, and I can't use:
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; // end of the loop. ?>
to get content from the actual page.
This is what I am trying to do, I've replaced:
query_posts('post_type=portfolio&posts_per_page=10');
with:
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
function add_my_post_types_to_query( $query ) {
if ( is_page( 8 ) && $query->is_main_query() )
$query->set( 'post_type', array( 'portfolio' ) );
return $query;
}
This seems like the right track, but it stills doesn't work. I'm not getting the posts from my custom post type.
Any ideas how I could modify this? I am also still learning so being clear with explanations would be greatly appreciated.
Thank you!
Editing the pre_get_posts will replace the original query and you will not have the content for your page at all. I would only recommend going this approach if you only wanted to display the content of your portfolio post type and not the content of your portfolio page.
For general post queries it is recommended to use WP_Query or get_posts.
http://codex.wordpress.org/Class_Reference/WP_Query
http://codex.wordpress.org/Template_Tags/get_posts
If you use the WP_Query function the wp_reset_postdata() will restore the post data back to the original so you can get the content of your original page.
$args = array(
'posts_per_page' => 10,
'post_type' => 'portfolio',
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
Now you will be able to use the original loop to show the content of your page
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; // end of the loop. ?>
Usually, I stick my query posts in a variable, like so:
$catid = get_cat_ID('My Category Name');
$args = array(
'posts_per_page' => 5,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'category' => $catid
);
$posts_array = get_posts($args);
Then you can loop it like so:
<?php foreach ($posts_array as $post) : setup_postdata($post);?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<?php endforeach; ?>
Finally, to access your page content you can use the variable $post, it's automatically set by wordpress. No need to add any more code than this to access your page content.
<?php foreach( $posts as $post ) : setup_postdata($post); ?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<?php endforeach; ?>
The foreach loop for your page content is a little overkill, and there is a better way to do it (most likely at least), but I haven't been bothered to look into it further yet! It works though!