Custom Message Types and Categories

I have been trying for several days to create a custom post type with categories. While this works for me, I can easily add content and assign categories to it. My code is below.

What I don’t understand, and it doesn’t seem to work, creates an archive page to display category messages. For example: My message type is called Ads . My category is called Photographers .

Is it possible for the page to dynamically determine which category you are in and display all user messages related to this category?

function my_custom_post_advert() { $labels = array( 'name' => _x( 'Adverts', 'post type general name' ), 'singular_name' => _x( 'Advert', 'post type singular name' ), 'add_new' => _x( 'Add New', 'advert' ), 'add_new_item' => __( 'Add New Advert' ), 'edit_item' => __( 'Edit Advert' ), 'new_item' => __( 'New Advert' ), 'all_items' => __( 'All Adverts' ), 'view_item' => __( 'View Advert' ), 'search_items' => __( 'Search Adverts' ), 'not_found' => __( 'No adverts found' ), 'not_found_in_trash' => __( 'No adverts found in the Trash' ), 'parent_item_colon' => '', 'menu_name' => 'Adverts' ); $args = array( 'labels' => $labels, 'description' => 'Holds our adverts and advert specific data', 'public' => true, 'menu_position' => 5, 'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'category' ), 'has_archive' => true, ); register_post_type( 'advert', $args ); } add_action( 'init', 'my_custom_post_advert' ); function my_taxonomies_advert() { $labels = array( 'name' => _x( 'Advert Categories', 'taxonomy general name' ), 'singular_name' => _x( 'Advert Category', 'taxonomy singular name' ), 'search_items' => __( 'Search Advert Categories' ), 'all_items' => __( 'All Advert Categories' ), 'parent_item' => __( 'Parent Advert Category' ), 'parent_item_colon' => __( 'Parent Advert Category:' ), 'edit_item' => __( 'Edit Advert Category' ), 'update_item' => __( 'Update Advert Category' ), 'add_new_item' => __( 'Add New Advert Category' ), 'new_item_name' => __( 'New Advert Category' ), 'menu_name' => __( 'Advert Categories' ), ); $args = array( 'labels' => $labels, 'hierarchical' => true, ); register_taxonomy( 'advert_category', 'advert', $args ); } add_action( 'init', 'my_taxonomies_advert', 0 ); 
+6
source share
5 answers

You should be able to navigate / advertisements. In addition, has_archive should create an archive page for you.

+1
source

You basically need to create a page template that contains customized wp_query so that WordPress can determine which category you are in.

After creating the page template, you can create the page in your WordPress admin ... by choosing a new page template as the template.

And if you want the category to be dynamic, you can always customize your page template to accept the $ _GET parameter to determine which category to display ads from. For instance:

 http://example.com/adverts-listing/?mycat=photographers 

or

 http://example.com/adverts-listing/?mycat=programmers 

and etc.

Here is an example of what the page template looks like (of course, this will differ depending on the theme you are using ... this example was built to use the twenty-four theme):

 <?php /** * Template Name: Advert Listing * */ get_header(); ?> <section id="primary" class="content-area"> <div id="content" class="site-content" role="main"> <?php // Set the args array for the query to happen $args = array( 'post_type' => 'adverts', 'post_status' => 'publish', 'posts_per_page' => 10 ); // Dynamically set the mycat argument from a $_GET parameter if( isset($_GET['mycat']) ) { $args['tax_query'] => array( array( 'taxonomy' => 'advert_category', 'field' => 'slug', 'terms' => $_GET['mycat'] ) ); } // Issue the query $q = null; $q = new WP_Query($args); // Start the loop if( $q->have_posts() ) : ?> <header class="page-header"> <h1 class="page-title"><?php _e( 'Advert Listing:', 'twentyfourteen' ); ?></h1> </header> <?php while ($q->have_posts()) : $q->the_post(); ?> <article id="post-<?php the_ID(); ?>" class="post-<?php the_ID(); ?> adverts type-adverts status-publish hentry"> <header class="entry-header"> <a href="<?php echo get_permalink(get_the_ID()); ?>"><h3 class="entry-title"><?php the_title(); ?></h3></a> </header><!-- .entry-header --> <div class="entry-content"> <?php the_excerpt(); ?> </div> </article> <?php endwhile; // Previous/next post navigation. twentyfourteen_paging_nav(); else : // If no content, include the "No posts found" template. get_template_part( 'content', 'none' ); endif; wp_reset_query(); // Restore global post data stomped by the_post(). ?> </div><!-- #content --> </section><!-- #primary --> <?php get_sidebar( 'content' ); get_sidebar(); get_footer(); 
+1
source

To save a lot of hassle, and what I used in the past, this custom message type plugin - it works like a charm

Types let you customize your WordPress admin by adding content types, custom fields, and a taxonomy. You can create a WordPress admin and turn it into your own content management system.

And with this I use this Custom post type archive plugin :

This plugin allows you to create your own post archives (also every year, monthly and daily) along with feeds, custom headers and paging.

0
source

The solution is essentially contained in an answer to this question elsewhere on StackOverflow.

To summarize, create a custom query, but in the $ args array, replace:

'cat_name' => 'Photographers'

with a taxonomy request, for example:

'tax_query' => array( array( 'taxonomy' => 'advert_category', 'field' => 'slug', 'terms' => 'photographers' ) )

Of course you should include 'post-type' => 'advert' in $ args. Hope this helps!

0
source

So I also needed custom post types with categories.

The code below is really simple and clean. Literally copy and paste . and then just adapt to your needs. Hope this helps people in the future.

Mainly associates regular WordPress categories with your custom post types. makes it very easy for your client when he works from the wordpress admin section. It also has an individual taxonomy through tags. therefore , you have the opportunity to have categories across all message types or publish a specific taxonomy .

The code is pretty clear. Please vote for my answer. I need to create my representative. Thanks.

Copy code to functions.php file

 add_action( 'init', 'create_post_types' ); function create_post_types() { // Custom Post 1 register_post_type( 'companies', array( 'labels' => array( 'name' => __( 'Companies' ), 'singular_name' => __( 'Company' ) ), 'public' => true, 'has_archive' => true, ) ); // Default Wordpress Category Taxonomy register_taxonomy_for_object_type( 'category', 'companies' ); // Post Specific Taxonomy register_taxonomy( 'company_category', 'companies' ); // Custom Post 2 register_post_type( 'events', array( 'labels' => array( 'name' => __( 'Events' ), 'singular_name' => __( 'Event' ) ), 'public' => true, 'has_archive' => true, ) ); // Default Wordpress Category Taxonomy register_taxonomy_for_object_type( 'category', 'events' ); // Post Specific Taxonomy register_taxonomy( 'events_category', 'events' ); // Custom Post 3 register_post_type( 'deals', array( 'labels' => array( 'name' => __( 'Deals' ), 'singular_name' => __( 'Deal' ) ), 'public' => true, 'has_archive' => true, ) ); // Default Wordpress Category Taxonomy register_taxonomy_for_object_type( 'category', 'deals' ); // Post Specific Taxonomy register_taxonomy( 'deals_category', 'deals' ); // Custom Post 4 register_post_type( 'banners', array( 'labels' => array( 'name' => __( 'Banners' ), 'singular_name' => __( 'Banner' ) ), 'public' => true, 'has_archive' => true, ) ); // Default Wordpress Category Taxonomy register_taxonomy_for_object_type( 'category', 'banners' ); // Post Specific Taxonomy register_taxonomy( 'banners_category', 'banners' ); } 

There are 4 custom message types, so I said the code is pretty clear

0
source

Source: https://habr.com/ru/post/945268/


All Articles