0

My current site has custom post types, that run of off the root domain, so anything created inside the CPT will be something like: domain.com/page-name/ rather than domain.com/custom-post-type-name/post-name/. With this, I want to update to WP 4.7 but I cannot because it breaks my website and the CPT's mess up.

Here's why I'm using custom post types, so I have this page template that pulls in everything related to that custom post type and displays those pages a list (similar to the blog page).

However, seeing as everything is of off the root. I was wondering if there was a way to customise Wordpress to give me the option to add a category to a PAGE, therefore, after I can create a code that will display all PAGES within that CATEGORY as a page template.

Reason for this, is will allow me to update to Wordpress 4.7 and not have 5-10 different CPT's as it just gets annoying clicking between the different custom post type labels in the backend.

If anyone has any ideas or know's off a plugin that will do this.

And maybe if some has the code to show all pages as a list, that sit under that category, that would be great - please share.

Thanks.

Update

Here's my code for loop:

<?php wp_reset_query(); ?>
                            <?php $query = new WP_Query( array( post_type=page, 'cat' => 541 ) ); ?>
have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>

                                            <?php if(has_post_thumbnail()) {
                                                the_post_thumbnail(array(150,150));
                                            } else {
                                                echo '<img class="alignleft" src="'.get_bloginfo("template_url").'/images/empty_150_150_thumb.gif" width="150" height="150" />';
                                            }
                                            ?>


                                            <div class="entry">
                                                <h3 class="blog_header"><a href="<?php echo get_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_title(); ?></a></h3>
                                                <?php the_excerpt(); ?>
                                                <a class="button_link" href="<?php the_permalink(); ?>"><span>Read More</span></a>

                                            </div>  


                                    </div>

I'm attempting to display the pages that have been added to that category within a loop. Is there something wrong with my loop?

Thanks.

  • Do you mean to say you need something like categories to be assigned into posts? – Pratik bhatt Jan 19 '17 at 13:09
0

If you don't want to attach the default post category taxonomy, you can always create a new one specifically for pages. Insert this into your functions.php file:

add_action( 'init', 'create_page_taxonomies' );
function create_page_taxonomies() {
    register_taxonomy('page_category', 'page', array(
        'hierarchical' => true,
        'labels' => array(
            'name' => _x( 'Page Category', 'taxonomy general name' ),
            'singular_name' => _x( 'page-category', 'taxonomy singular name' ),
            'search_items' =>  __( 'Search Page Categories' ),
            'all_items' => __( 'All Page Categories' ),
            'parent_item' => __( 'Parent Page Category' ),
            'parent_item_colon' => __( 'Parent Page Category:' ),
            'edit_item' => __( 'Edit Page Category' ),
            'update_item' => __( 'Update Page Category' ),
            'add_new_item' => __( 'Add New Page Category' ),
            'new_item_name' => __( 'New Page Category Name' ),
            'menu_name' => __( 'Page Categories' )
        ),
        'public' => true,
        'rewrite' => array(
            'slug' => 'page-category',
            'with_front' => false,
            'hierarchical' => true
        )
    ));
}

This is a fairly bare-bones new taxonomy, and there are plenty of other options available, of which there is a complete listing in the WP Codex.

UPDATE:

I believe that this is what the whole loop would look like:

<?php
    wp_reset_query();
    $query = new WP_Query(
        array(
            'post_type' => 'page',
            'tax_query' => array(
                array( 
                    'taxonomy' => 'page_category',
                    'field' => 'term_id',
                    'terms' => 541
                )
            )
        )
    );
    if ( $query->have_posts() ) :
        while ( $query->have_posts() ) : $query->the_post(); ?>
            <div>
<?php
            if ( has_post_thumbnail() ) :
                the_post_thumbnail( array(150,150) );
            else : ?>
                <img class="alignleft" src="<?php echo get_bloginfo("template_url"); ?>/images/empty_150_150_thumb.gif" width="150" height="150" />
<?php
            endif; ?>
                <div class="entry">
                    <h3 class="blog_header"><a href="<?php echo get_permalink(); ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_title(); ?></a></h3>
                    <?php the_excerpt(); ?>
                    <a class="button_link" href="<?php the_permalink(); ?>"><span>Read More</span></a>
                </div>
            </div>
<?php
        endwhile;
    endif; ?>
  • This works, however, when I try to display these pages in a loop for a page template (I've updated my question with code) - it doesn't allow me to view the page... – Jacob Punton Jan 19 '17 at 17:47
  • I saw that Kudratullah fixed the error in the loop, so you should at least be seeing a non-blank page. However, to query the proper taxonomy, replace 'cat' => 541 with this: 'tax_query' => array( array( 'taxonomy' => 'page_category', 'field' => 'term_id', 'terms' => 541 ) ). More info here: codex.wordpress.org/Class_Reference/… – Jared Counts Jan 19 '17 at 18:14
  • Thanks. But I still don't get how this would work, would you be able to post some code somewhere and send me a link so I can see the whole loop? Thanks. – Jacob Punton Jan 19 '17 at 18:18
  • Updated the post, see above. – Jared Counts Jan 19 '17 at 19:05
2

checkout this solution by @Matoeil

function myplugin_settings() {  
// Add tag metabox to page
register_taxonomy_for_object_type('post_tag', 'page'); 
// Add category metabox to page
register_taxonomy_for_object_type('category', 'page');  
}
 // Add to the admin_init hook of your theme functions.php file 
add_action( 'init', 'myplugin_settings' );
  • Thanks for sharing, where and how should I add this? – Jacob Punton Jan 19 '17 at 13:15
  • in functions.php please :) – Kudratullah Jan 19 '17 at 13:17
  • That worked, thanks so much. Do you have any idea on how to have the categories assigned only for pages? Because it's pulling the categories through for posts.. Thanks. – Jacob Punton Jan 19 '17 at 13:58
  • just change your query in your page template and add post_type=page. eg. $query = new WP_Query( array( post_type=page, 'cat' => 4 ) ); – Kudratullah Jan 19 '17 at 17:36
  • This doesn't seem to work, just bring back a blank page! – Jacob Punton Jan 19 '17 at 17:52

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.