I’m getting a Page Not Found message when clicking on the link to display the terms in a taxonomy
Scenario:
I have a custom post type called "glossary"
Attached is a custom taxonomy called "section" with the rewrite set to “library/glossary-start-here”
I created a page that lists the terms in the taxonomy. This is a Wordpress page with the slug of “glossary-sections” - the template for the page is set to “taxonomy-section.php”. The page is a child of “library” and so the permalink for the page is “example.com/library/glossary-starts-here/glossary-sections/"
The template “taxonomy-section.php” has the following code:
<?php // Begin header section.
$argterms=array(
'include' =>array(
117,118,115)
);
$terms = get_terms('section', $argterms);
echo '<ul>';
foreach ($terms as $term) {
echo '<h3>' . $term->name . '</h3>';
}
echo '</ul>'; ?>
<div>
<?php
When clicking on the link to the page “glossary-sections”, which is used on a number of other pages, I get a page not found message.
I know the template does its job because on some occasions as I've poked around troubleshooting I've had it working. However, I can't find a consistent condition that causes the page to work. I suspect that the problem has something to do with the rewrite but my tests have been inconclusive.
I’d appreciate any suggestions on why this isn’t working and how to correct it.
I’m particularly puzzled by the fact that I can specify a page like “glossary-section” and yet have it not be found.
Thanks.
As I understand it, WordPress has a hierarchy / parse order (so to speak) as to what it looks for when it gets a request. Technically, a taxonomy is just a tag.
I'm not sure if this will help:
http://justintadlock.com/archives/2009/06/04/using-custom-taxonomies-to-create-a-movie-database
But J.Tadlock is always a solid place to start.
The source of the problem was that I had included a rewrite for the taxonomy, while at the same time using pages with assigned templates.
Once the rewrite was removed it appears that everything worked correctly.
Related
I'm simply placing a small snippet of code on a woocommerce product page of my Wordpress website, to display an image "IF" the product has a specific TAG in place.
The code itself seems like it would be simple enough, and ive googled the heck out of this and tried many variations, with no luck on any of them.
<?php if( has_tag( 'tagnamehere') ) : ?> <div>my content</div> <?php endif; ?>
Also tried this:
<?php if( has_tag( 'tagnamehere' ) ){ echo '<div>my content</div>'; } ?>
Thats didnt work either.
basically, i just want it to look at the TAG of the product, and if the TAG exists, simply show the image (div). Nothing seems to work.
WordPress actually uses what's called Taxonomies and Terms.
Taxonomies are basically a grouping of Posts or Custom Post Types (like 'Category' or 'Post Tag', and Terms are basically the individual grouping names 'Featured Posts', etc.
WooCommerce basically registers a Custom Post Type called product, and also a Taxonomy called product_tag. Note, this is different than the default Tags on Posts.
Effectively this means you'll need to check if the term 'tagnamehere' exists in the product_tag taxonomy, the easiest way would be with the has_term() function. This is basically like the "Custom Post Type with Custom Category (aka Taxonomy)" version of has_tag()
if( has_term( 'tagnamehere', 'product_tag' ) ){
echo '<div>my content</div>';
}
Also to address your original "two versions" of code - The curly brace or alternative-syntax if statements work identically, and are mostly up to style/preference.
I'm creating my first wordpress theme, and I've looked around on google, and the wordpress codex for an answer to my question, but I can't seem to find exactly what I'm looking for, or couldn't get it working.
What I'm trying to do, or trying to figure out, is how I can make it so a certain category has a certain design.
So if I wanted to make an index.php for any music videos in "www.domain.com/music/trash/drake-song.mp4.html"
the trash category, it'd have its own design, but songs in
"www.domain.com/music/good-music/coldplay-viva-la-vida.mp4.html"
the good-music category, I want it to look pretty much completely different. I've tried using something similar inside my header.php to this;
<?php
if( is_tag( 'good-music' ) ):
$my_classes = array( 'good-class', 'good-class-two' );
else:
$my_classes = array( 'not-good-class' );
endif;_
?>
but it seemed to simply change the category page.
"www.domain.com/categories/good-music"
Anyone know what I could be doing wrong? I know basic html/css/php/javascript, new to creating a WordPress theme, and can't seem to get this working..
Also:
Using XAMPP to host locally, using Friendly URL's, properly configured
In order to generate category-specific markup for a single post layout, you can put code like the below in your single.php file after the call of get_header().
Please note that this checks for your category based upon category slug. So if your category slug (the url version of your category) does not match the first arguments in the in_array() function call below, then you should change the argument to match the slug for your category.
<?php
/* Start the Loop */
while ( have_posts() ) : the_post();
?>
<?php
$categories = get_the_category();
$catSlugs = array();
foreach ($categories as $category){
$catSlugs[] = $category->slug;
}
if (in_array('good-music',$catSlugs)){
$post_cat = 'good-music';
} else {
$post_cat = 'not-good-music';
}
get_template_part( 'template-parts/post/content', $post_cat );
?>
<?php
endwhile; // End of the loop.
?>
It is important that this code only appear where there is a query to loop against.
Specifically, the file single.php in your theme should be the default file for displaying a single post, regardless of category. When you navigate to the url of a single post, this layout should be triggered.
As part of that triggering, a wordpress query of just that post is returned to be looped through.
Then, the code from while ( have_posts() ) : the_post(); until endwhile; will run one time, because there is a single post to be processed.
If there were more than one post, such as on a category page or on your default post listing page, then the code inside of that while loop would run as many times as there are posts in the query for that layout.
If you were to place the code in the header, it won't work because the header is prepared independently of the loop that runs on this page.
You could run a custom WP_query() in the header, but that is rarely a good way to handle site content.
In this situation it would not be appropriate, because you are customizing content of existing posts, and only differentiating based upon category.
So, just use the standard layouts files with custom layout parts.
I stripped out the divs in the loop below, because you may or may not be using bootstrap.
After this code is placed on your page, you would create files called content-good-music.php and content-not-good-music.php in YOUR_THEME_DIR/template-parts/post/ directory. The key is that you would add whatever your category slug is to the end of your
These files will contain the unique markup for these kinds of posts. You can also use a similar logic in your post listing loop to give the listed posts for each category their own unique php files.
Here's some get_template_part() documentation.
https://developer.wordpress.org/reference/functions/get_template_part/
I am trying to load a Wordpress page in another php file. Since I have integrated CodeIgniter with Wordpress.
The following works without problems and loads the wordpress site.
get_header();
get_footer();
But how do I change which page is loaded, instead of the homepage?
I only want to call the get_header() and get_footer wp functions but find a way to select the page.
I have thus far tried altering the $wp_query global object but haven't had any success.
Thanks,
The following worked:
$page_to_load = new WP_Query(array(
'page_id' => $page_id
));
global $wp_query;
$wp_query = $page_to_load;
wp();
get_header();
get_footer();
This sets the page to load in the $wp_query object, and then you have to call wp() in order to reload the page and have it route to the page.
So you want to display the header and footer only on some pages. There are 2 functions, which should solve the problem.
First one:
is_front_page()
This one just tests, whether the displayed page is your front page. You just need to use it like this:
<?php
echo is_front_page() ? "I'm a front page." : "This isn't a front page"
?>
Second one: is_page()
There are multiple ways to use this function. First one (without any parameter) just tests, whether it is page. Second one (with an ID) only returns true, if the requested page has got the specific ID. Third one (with slug or post title) is the same like the second only with those parameters.
This is question on the seo yoast wordporess plugin.
I have a friend who doesn't know that much about code/wordpress/etc, they have an ecommerce site built using magento and a blog using wordpress which is styled to match the main site and they use yoast seo plugin for seo etc. They have asked me to try an get the breadcrumbs working for them in a different way to what i know, from what i can see they want to add a prefix to the breadcrumbs 'home' which links to main site then renaming the blog from home to blog, for example home(main site) > blog(blog) > post.
Now i did just that in the plugin settings but they said that the schema markup wouldn't be complete and said there’s a filter hook called ‘wpseo_breadcrumb_links’ which gives access to the array of URLs and anchor text used to build the breadcrumbs, now i cant find anything on google that explains this or how to start writing it, i do know that it needs to go in the functions file.
Would it be possible to get some help on this.
Thanks in advance and very much appreciated.
Justin
I'm not sure what Schema.org markup has to do with breadcrumbs! :) Schema is used on post/page level. So, they are either oversmarting themselves or I got you wrong.
I think this sample code may be helpful:
add_filter( 'wpseo_breadcrumb_output', 'custom_wpseo_breadcrumb_output' );
function custom_wpseo_breadcrumb_output( $output ){
if( is_product() ){
$from = '<span typeof="v:Breadcrumb">Products</span> »';
$to = '';
$output = str_replace( $from, $to, $output );
}
return $output;
}
It looks like wpseo_breadcrumb_output gives you access to the entire output instead of just the link portion. Please see this page for more details: http://wpquestions.com/question/showChrono/id/8603
Not sure how to word this. I've set up a custom post type in WordPress called Products. I also have two taxonomies linked to Products called Brands and Categories. When you create a product you can set to which Brand and Category the product belongs. It works perfectly when using the type as permalink to list everything within a type
http://sitename.com/products
but I can't seem to get the taxonomies working in the same way, ie. list all the items within a taxonomy (like an archive, I suppose):
http://sitename.com/brands
http://sitename.com/categories
Is this possible, and if so, what could I possibly be doing wrong? I would try and set up some rewrite rules in functions.php, but I don't know what the query string looks like if you want to list items in a taxonomy, and if this is even possible. If it helps, I'm using the plugins More Types and Ultimate Taxonomy Manager to create the types and taxonomies.
There's not really a way to do this yet. WP is moving this direction and with 3.1 is setting up a top-level for custom post types, but still nothing for taxonomies. It's klunky but I added a page called page-products.php and then put my list in there.
$toplevelterms = get_terms($taxonomyname, 'hide_empty=0&hierarchical=0&parent=0');
$termchildren = array();
foreach ($toplevelterms as $toplevelterm) {
$termchildren[] = $toplevelterm->term_id;
}
foreach ($termchildren as $child) {
$term = get_term_by('id', $child, $taxonomyname);
$write .= '<li>';
$write .= '' . $term->name . '';
if ($term->description) $write .= apply_filters('the_content', $term->description);
$write .= '</li>';
}
if ($write) echo '<ul class="clearfix">' . $write . '</ul>';
FYI the correct link for that plugin (I'm the author) is now http://www.get10up.com/plugins/simple-custom-post-type-archives-wordpress/
And it should really be done using the built in archives feature in 3.1 - the plugin was built for 3.0.
There is a Trac ticket for this, but it's not clear what would the most obvious thing to show on this page:
a list of all posts attached to any term of that taxonomy, or
a list of all terms of this taxonomy?
A similar question on the WordPress Stack Exchange asked for option 2, and your question can be interpreted both ways: when you say all items within a taxonomy, do you mean all terms of this taxonomy, or all posts that are linked to a term of this taxonomy?
Currently the main query (the Loop) that you can control with the query vars will always return posts, not terms. As said in the ticket, option 2 would require the Loop to return terms for this query, possibly causing compatibility issues for themes that don't have a template for it. Until that is implemented, David's answer is probably the way to go.
Thanks for the answers! I've found a plugin that does exactly what I wanted - after manually coding everything :/
http://www.thinkoomph.com/plugins-modules/wordpress-custom-post-type-archives/
Use the WP htaccess Control Plugin
http://wordpress.org/extend/plugins/wp-htaccess-control/
In the options , you can remove the base of taxonomies you select.
Worked for me, hope It works for you as well :)