1

I have a website that I'm building for people to write their own books. I have a custom taxonomy for this, labeled books.

I now need a link or pull-down that contains all of the authors on the site, so that viewers can see all of the books made by each author (profiles show all posts, but no custom taxonomy entries). I've been trying to make something to do this (via a widget), but I don't really know php at all and have gotten nowhere (I barely know html either). The reason I need it in a widget is because the theme I'm using resets the php files every time it is updated, meaning I can't add or edit anything in them (even if I knew how).

Question: Is there a way I can:

  1. Create a self-updating list (page, pull-down, whatever) of authors.
  2. Have those authors, when clicked, show a list of the terms they have created within a specific custom taxonomy (I have several custom taxonomies, so I have to be able to specify).

I realize the automatic course of action is probably to start editing the php files of the theme, but remember that I can't do that. I need something I can either put in a widget (like html in a text widget), or some kind of a page that I can make a link to.

Example:

I have the custom taxonomy of 'books.' Author A makes the books 'One' and 'Two.' Author B makes the books 'Three' and 'Four.' Ideally, I would want a pulldown somewhere that lists all of the authors. This pulldown automatically updates itself, so if another author joins I don't have to manually go in and add him to the pulldown.

The pulldown lists Author A and Author B, amongst the others. If one clicks on Author A, he should be able to see all of the books that Author A has written, and click on them to view them. Same goes for Author B.

2

Well, when you need to modify a theme you create a "child theme." There are good instructions for how here.

So we would just create a safe copy of the modified code in a folder the other theme doesn't overwrite.

What I think you want to do is create a custom post type "Book" and register this post type to a taxonomy of "Author." Here is an example:

function book_object() {
    register_post_type('book', array(
        'labels' => array(
            'name' => __('Books', 'textdomain'),
            'singular_name' => __('Book', 'textdomain'),
            'add_new' => __('Add New', 'textdomain'),
            'add_new_item' => __('Add New Book', 'textdomain'),
            'edit_item' => __('Edit Book', 'textdomain'),
            'new_item' => __('New Book', 'textdomain'),
            'view_item' => __('View Book', 'textdomain')
        ),
        'description' => 'People like books.',
        'register_meta_box_cb' => 'book_fields',
        'public' => true,
        'menu_position' => 20,
        'supports' => array('title', 'editor', 'thumbnail',),
        'hierarchical' => true
    )
    );
}
    function book_fields() {
        add_meta_box('book_details', __('Book Details', 'textdomain'), 'book_fields_cb', 'book', 'normal', 'high');
        function book_fields_cb($post) {
            $value = get_post_meta($post->ID, 'pubdate', true);
            echo '<label for="pubdate">'.__('Publish date', 'textdomain').'</label><br />';
            echo '<input type="text" id="pubdate" name="pubdate" value="'.esc_attr($value).'" size="25" /><br />';
            $value = get_post_meta($post->ID, 'field_2', true);
            echo '<label for="field_2">'.__('More custom fields', 'textdomain').'</label><br />';
            echo '<input type="text" id="field_2" name="field_2" value="'.esc_attr($value).'" size="25" /><br />';
        }
    }

That creates the Book object.

To create taxonomy for Authors:

function author_taxonomy() {
    $labels = array(
        'name'              => _x( 'Authors', 'taxonomy general name' ),
        'singular_name'     => _x( 'Author', 'taxonomy singular name' ),
        'search_items'      => __( 'Search Authors' ),
        'all_items'         => __( 'All Authors' ),
        'parent_item'       => __( 'Parent Author' ),
        'parent_item_colon' => __( 'Parent Author:' ),
        'edit_item'         => __( 'Edit Author' ),
        'update_item'       => __( 'Update Author' ),
        'add_new_item'      => __( 'Add New Author' ),
        'new_item_name'     => __( 'New Author' ),
        'menu_name'         => __( 'Authors' ),
    );
    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array(
                                'slug' => 'authors',
                                'hierarchical' => true,
                                'with_front' => false
                            ),
    );
    register_taxonomy('author', array('book'), $args);
}

Then we hook these functions into Wordpress like so:

add_action('init', 'my_init');
function my_init() {
    book_object();
    author_taxonomies();
}

That last part is usually placed at the top of the file. Some people drop the add_action lines in below the blocks of code they invoke, but I like to nest successive action calls at the top to help make sense of it all for me.

This code basically goes all by its lonesome in a file called functions.php - except you don't put that functions.php in the old theme folder.

You create a new theme folder, with a new style.css - all it needs to have is the following comment at the top:

/*
 Theme Name:   My custom book portal
 Description:  Child theme of OLD-THEME-NAME
 Template:     OLD-THEME-SLUG
 Text Domain:  textdomain
*/

So now you have a new folder in /wp-content/themes/ which can be called anything you want. It contains style.css and functions.php with all the code from above. You can add a file to this folder which will now help you display the data; we're going to call it taxonomy-book.php and place it inside the child theme folder.

I would suggest you start by copying all of the code from your current theme's taxonomy.php file, or if no such file exists in your present theme, look for archive.php. These files will begin with almost everything you're looking for.

How to get users to this page brings us back to your original problem - the menu. We have the data it will display now, we have the pages its selections will link to - we need the dropdown menu. I like just putting it in the navigation.

In Appearance->Menus, you can now add the taxonomy items to any navigation menu from the WP selection boxes on the left side of the menu builder. If you'd like me to write you a quick little widget instead, I'll look for your reply.

  • Okay, so I've made a new folder in wp-content/themes, and put a style.css in there with the code you specified. Now comes the hard part. Something I should have mentioned earlier (was trying to keep it simple) is that I have a plugin built for writing that allows me to create custom taxonomies. It's a lot easier than creating them via the code. However, since I have this plugin, I don't think I want to create more taxonomies that might conflict with it. Is there some code I could use for an existing taxonomy of 'books'? Keep in mind I know virtually nothing about php. – Thomas Reinstate Monica Myron May 15 '16 at 2:12
  • Thanks for your time, by the way. Hopefully I can explain the issue well enough with my limited knowledge of what I'm talking about. :) EDIT: Oh, and what do I fill in for 'textdomain' in the style.css? Is it just the URL of the site? – Thomas Reinstate Monica Myron May 15 '16 at 2:13
  • textdomain is a unique string used to identify language translations for your theme, usually the same as your theme slug. eg. my-theme – majick May 15 '16 at 2:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.