Hot answers tagged categories
7
There are 2 points of attack to cover when you are adding custom post type rewrite rules:
Rewrite rules
This happens when the rewrite rules are being generated in wp-includes/rewrite.php in WP_Rewrite::rewrite_rules(). WordPress allows you to filter the rewrite rules for specific elements like posts, pages and various types of archive. Where you see ...
7
I wrote a plugin for this -- that needs some updating, but it should still work fine.
Essentially you hook in some place late after WordPress knows what page is requested (like template_redirect), use one of the conditional functions and either use $wp_query->set_404() or just redirect the user to a page of your choice.
To disable categories, tags, ...
7
You should be able to use pre_get_posts and set the posts_per_page to 50 conditionally (on being feed for categories). Unfortunately there is this unresolved trac ticket.
The only work-around is to do hook into post_limits and replace the LIMIT part of the SQL query directly.
add_action('post_limits','wpse71759_category_rss_limit',10,2);
function ...
6
The article on Ghacks is actually a pretty silly way to do it as well.
The get_header() function is actually a pretty smart function. You can do some neat things with it. For example, you can do this:
get_header('category');
That will cause it to load the header-category.php file, if such a file exists, or the header.php file, if header-category.php does ...
4
When no hook is available, you can always count on the old jQuery trickery...
add_action( 'admin_footer-edit-tags.php', 'wpse_56569_remove_cat_tag_description' );
function wpse_56569_remove_cat_tag_description(){
global $current_screen;
switch ( $current_screen->id )
{
case 'edit-category':
// WE ARE AT ...
4
building on chrisguitarguy's answer here is a quick snippet you can drop in your theme's functions.php file to do job
add_action('template_redirect', 'wpse69948_archive_disabler');
function wpse69948_archive_disabler()
{
if(is_tag() || is_category() || is_date() || is_author())
{
global $wp_query;
$wp_query->set_404();
}
}
4
I would surely use Pages instead of posts/categories in this case. Pages are meant to be "static" and posts are meant to be used for more dynamic content such as a blog.
Pages also gives you an easier way to customize the look of individual pages if needed, with page templates.
Have a look at these pages for some more info...
Post vs. Page (at ...
4
You can do this using Wordpress's handy body_class() function. Depending on whether and how it is used in your theme, it may already be giving you what you need. Here's how to find out:
Check the source of your page to see if the <body> tag in your category archive pages has any classes containing your category slug: category-apple, category-area, ...
4
There is even an example in the WordPress codex here for this:
http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
function exclude_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', '-3,-8' );
}
}
add_action( 'pre_get_posts', 'exclude_category' );
Just modify ...
4
Try to do all theme modifications in functions.php whenever possible. It keeps the theme files clean and uncluttered. Here's an example using the pre_get_posts action:
function order_category_archives( $query ) {
if ( is_category() && $query->is_main_query() ){ // is_category() can specify a category, if necessary
$query->set( 'orderby', ...
3
You may want to use this function:
Place this in your functions.php
function isotope_categories() {
$categories = get_categories();
$html = '<ul class="filters option-set" data-option-key="filter">';
$html .= '<li><a href="#filter" data-option-value="*" class="selected">All items</a></li>';
...
3
add
'rewrite' => array('slug' => 'portfolio-categories'),
but be sure to remove (not sure why that's there, it tries hides the slug yet tries to rename it to "products" at the same time?):
'rewrite' => array( 'with_front' => false, 'slug' => 'products' ),
So it looks like this:
function mysite_post_types() {
...
3
Rather than performing a separate query, to exclude a category (or any other taxonomy) term, you can hook into pre_get_posts:
add_action('pre_get_posts', 'wpse41820_exclude_cat_from_front_page');
function wpse41820_exclude_cat_from_front_page( $query ){
if( $query->is_main_query() && is_front_page() ){
$tax_query = array(array(
...
3
If you are deleting code from the main WordPress files, this means that you know your way around a whole bunch of PHP files, so it's time now to learn how to do things without touching core files.
Side note to the hook presented in the Question:
the filter global_terms_enabled only works for Multisite (/wp-includes/functions.php, line 3006).
In many cases, ...
3
If you want to use the function to output the code somewhere:
function displayImage($currentPost) {
// Show the featured icon only if current post is in "featured" category
if ( in_category ( 'featured', $currentPost ) ) {
$output = '<a href="<?php the_permalink(); ?>">
<span ...
3
This looks like a plain PHP question at the first sight, but there is also at least one WordPress issue. Let’s start with that.
You should not use include or TEMPLATEPATH in a theme. There are alternatives for include in WordPress: get_template_part() and locate_template(). And the constants TEMPLATEPATH and STYLESHEETPATH will be deprecated in the near ...
3
You can do this with a rewrite rule from within WordPress. Take a look at the documentation for add_rewrite_rule.
Something like this:
<?php
add_action('init', 'wpse65855_rewrite');
function wpse65855_rewrite()
{
add_rewrite_rule(
'^photos/?$', // the rule regex
'index.php?taxonomy=category&term=photos', // where you want the ...
3
See the Codex's WordPress Taxonomy documentation.
WordPress 2.3 replaced the previous categories, post2cat, and link2cat tables with three a more flexible set of taxonomy tables.
wp_terms
wp_term_relationships
wp_term_taxonomy
wp_terms- holds the basic information about single terms.
term_id bigint(20) unsigned NOT NULL auto_increment,
name ...
3
You need to create a new loop for that. Here's the code I use for displaying products from a specific category on the home page:
<ul class="products">
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'product_cat' => 'shoes', 'orderby' => 'rand' );
$loop = new WP_Query( $args );
while ( ...
3
http://codex.wordpress.org/Function_Reference/get_term_by
example:
<?php
$link_id = 23;
$link_category = get_term_by('id',$link_id,'link_category');
var_dump($link_category);
?>
3
If you want only 2 levels, you may use the parent parameter. Example
<?php
$args = array(
'hide_empty' => 0,
'parent' => 0
);
$categories = get_categories($args);
$menu = '<ul class="nav">';
// iterate through your categories
foreach($categories as $category) {
$menu .= '<li class="' . 'cat-item category-' . ...
3
You can use the filter hook for 'single_template'. Create single-cat1.php, single-cat2.php and single-cat3.php (cat1, cat2 and cat3 are the category names should be replaced by your category names)
function template_change( $template ){
if( is_single() && in_category('cat1') ){
$templates = array("single-cat1.php");
} elseif( ...
3
Filter category_link so WordPress creates slashed URLs for categories, and redirect_canonical so it accepts those URLs:
add_filter( 'category_link', 'wpse_71666_trailingslash_cat_url' );
add_filter( 'redirect_canonical', 'wpse_71666_trailingslash_cat_url', 20, 2 );
function wpse_71666_trailingslash_cat_url( $url, $request = '' )
{
if ( 'category_link' ...
3
Try adding this code to functions.php file:
add_action('pre_get_posts', 'ad_filter_categories');
function ad_filter_categories($query) {
if ($query->is_main_query() && is_home()) {
$query->set('category_name','news, uncategorized');
}
}
category_name is the slug or the nicename of the category. Add a comma separated list of ...
3
Remove the cat parameter from your query and name your template file category.php
From the WordPress Codex on Template Hierarchy;
1. category-{slug}.php - If the category's slug were news, WordPress would look for category-news.php
2. category-{id}.php - If the category's ID were 6, WordPress would look for category-6.php
3. category.php
4. ...
3
There are two issues here:
The use of query_posts()
Undefined $pagename variable
I assume that you want to use the page slug as the string passed for the category parameter in the query arguments array? You can get that via $post->post_name, like so:
global $post;
$page_slug = $post->post_name;
Then, to pass that as a query parameter, you would ...
3
Category-list uses the function wp_terms_checklist() in wp-admin/includes/template.php on row 90. The parameter "checked_ontop" is set to true. So the checked checkboxes will be on top.
This is only happening when editing a post, when I add a new one everything is fine and dandy
Thats because when you create a post, none of the categories are checked and ...
3
Something like this should do it. Replace wpse_77670_getPermittedCategories() with however you select the array of permitted categories, and 'your_custom_category' with whatever your custom taxonomy is for your custom post type.
/**
* filter terms checklist args to restrict which categories a user can specify
* @param array $args arguments for function ...
3
Must have both
The following arguments array searches for the post-type-slug videos and category-slug video. It doesn't use pagination by setting posts_per_page to -1 and only returns published posts.
$args = array(
'post_type' => 'videos',
'category_name' => 'video',
'posts_per_page' => -1,
'post_status' => 'publish'
);
...
3
To change the default "Uncategorized" using code you can do the following:
// Uncategorized ID is always 1
wp_update_term(1, 'category', array(
'name' => 'hello',
'slug' => 'hello',
'description' => 'hi'
));
Read this: http://codex.wordpress.org/Function_Reference/wp_update_term
Only top voted, non community-wiki answers of a minimum length are eligible
