Hot answers tagged custom-post-type-archives
8
after u have set a static page as your home page you can ad this to your functions.php and you a good to go. This will call the archive-POSTTYPE.php template correctly as well.
add_action("pre_get_posts", "custom_front_page");
function custom_front_page($wp_query){
if($wp_query->get('page_id') == get_option('page_on_front')):
...
6
I know this is old but I have this problem too and I found a rather clean way to handle it is to use a custom menu walker
class KB_Custom_Menu_Walker extends Walker_Nav_Menu {
protected static $custom_post_types = array();
public function start_el(&$output, $item, $depth=0, $args=array(), $id=0) {
if (isset( self::$custom_post_types[ ...
4
Hook into '404_template'. (Example)
Fetch all public custom post types where has_archive is not FALSE.
Find the post type’s has_archive string and see if it is part of the current request’s url.
Try get_page_by_title() with the last part of the requests.
wp_redirect() to the found post’s permalink.
exit;.
3
I did something similar for a client a while back, I'll give you some of the code here as-is that you can possibly adapt to your needs. I'll warn you, it's quite a bit to parse through!
First, I set up some custom rewrite rules to get the year/month URL structure and some query vars to pass the year and month to my template. In this example I have a page ...
3
Try this piece of code for page template. I've used it on one of my projects. It outputs taxonomy term one by one with list of all posts with this term. (Just replace YOUR_TAXONOMY_SLUG to yours)
<div id="content">
<h2 class="entry-title"><?php the_title(); ?></h2>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ...
3
You can use getarchives_where hook of wp_get_archives() function
Add this function to your functions.php:
function Cpt_getarchives_where_filter( $where , $r ) {
$post_type = 'blog';
return str_replace( "post_type = 'post'" , "post_type = '$post_type'" , $where );
}
Then when you want your monthly archive put this:
add_filter( 'getarchives_where' , ...
3
How did you create the link with your category '39'? Is it a custom taxonomy?
Here's the test i just did and it works fine:
In functions.php i created the custom post type 'recipe':
add_action( 'init', 'create_recipe' );
function create_recipe() {
register_post_type( 'recipe',
array(
'labels' => array(
'name' ...
3
This should work for you:
// grab all public post types
$post_types = get_post_types( array('public' => true), 'names' );
// loops through each post type
foreach( $post_types as $type ) {
// setup the query
$query_args = array(
'post_type' => $type,
'posts_per_page' => 1
);
// perform the query
$items = ...
3
To get the current post type use get_post_type(). Then ask get_post_type_object() for all the data you need, for example the slug:
$post_type = get_post_type();
if ( $post_type )
{
$post_type_data = get_post_type_object( $post_type );
$post_type_slug = $post_type_data->rewrite['slug'];
echo $post_type_slug;
}
3
Why don't you try using a home.php file and place your desired loop/query within that file or alternatively use get_template_part to retrieve a file that contains your loop.
Can you please show us your code?
What I frequently do is structure my index.php like so,
<?php get_header(); ?>
<?php if ( is_home() ) {
...
3
Include your template files in your plugin. I stick mine in /plugin/templates.
You need to hook into template location for that template:
add_filter('archive_template', 'yourplugin_get_custom_archive_template');
function yourplugin_get_custom_archive_template($template) {
global $wp_query;
if (is_post_type_archive('yourCPT')) {
...
3
If you'd like to list the individual courses, i.e. the taxonomy terms, you'd use neither WP_Query nor the WP standard Loop.
Instead, make use of the get_terms function to retrieve the courses. It returns an array of term objects (if the taxonomy does exist and has terms matching the function arguments). Iterate over that and do something with it, such as ...
3
Given the standard registration, you should have the following:
A post type with the name 'recipe'
A recipe post archive at example.com/recipe/
Recipe posts with urls that take the form example.com/recipe/helloworldrecipe/
A template archive-recipe.php
A template single-recipe.php
However, I see this in your registration code:
'rewrite' => ...
2
That's because 'meta_key' and 'meta_value' are not public query vars. In other words, you can't use them in URLs directly, nor should you.
Instead, register a specific query var, like so:
function register_my_qv() {
global $wp;
$wp->add_query_var( 'my_qv' );
}
add_action( 'init', 'register_my_qv' );
Then, you can go to a URL like this: ...
2
This conflict normally happens when a custom post type archive and a normal wordpress page has the same slug. The custom post type archive has the higher priority here.
If you have a custom post type by the name 'Projects' and 'has_archive' is set to true for the custom post type then the conflict will arise. Set 'has_archive' to false so that it shows the ...
2
you can query_posts and orderby=title:
<?php //*The Query*//
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
query_posts($query_string . '&post_type=YOUR-POST-TYPE&posts_per_page=1&orderby=title&order=asc&paged='.$paged);
if(have_posts()) : while(have_posts()) : the_post();
?>
<?php endwhile; ?>
...
2
I've been able to solve this myself. My entire code for registering the CPT:
<?php
add_action( 'init', 'events_post_type_register' );
function events_post_type_register() {
$post_type = "events";
$labels = array(
'name' => _x('Events', 'post type general name', 'project_X'),
'singular_name' => _x('Event', 'post type ...
2
If you using wp nav menu you can use this to add the current-menu-item class put this in your theme functions.php file and remember to change "mypageslug" to your wanted current page:
function additional_active_item_classes($classes = array(), $menu_item = false){
global $wp_query;
if ( $menu_item->post_name == 'mypageslug' && ...
2
Your error is likely get_query_var('paged') should be get_query_var('page').
However...
You should avoid calling query_posts in the template, it is a waste of resources, as you're just discarding the original query which has already happened.
Hook pre_get_posts to instead alter the query before it happens:
function wpa60728_pre_get_posts( $query ) {
...
2
Here's an analogy to help you out:
You have a book, there are 500 words per page and you open the book at a page and read. You then hand then close the book and hand it to me, asking me to read the next page. Which page?
You've told your query what you want, and how many per page you want, but nowhere have you specified which page you want it to grab, ...
2
The function get_event_list()
Should looks like this …
Whenever you use a custom query you must include paged variable in the query for pagination to work.
Pagination Note: Use get_query_var('page'); if you want your query to work in a Page template that you've set as your static front page. The query variable 'page' also holds the pagenumber for a single ...
2
Here's an example using add_rewrite_rule to handle years and months for a custom post type where news is the slug. Visit the Settings > Permalinks page in admin to flush rewrite rules after this is added. You could also put this in a plugin and flush rewrite rules on plugin activation.
function wpa83797_news_rewrite_rules(){
add_rewrite_rule(
...
2
Replace your code with the following.
I have made some changes to the post_type_link filter's callback function.
function my_custom_post_work() {
$labels = array(
'name' => _x( 'Work', 'post type general name' ),
'singular_name' => _x( 'Work', 'post type singular name' ),
'add_new' => _x( ...
2
If this is the main query, you shouldn't be creating a new query at all, just run the normal loop and you will see the posts from your CPT.
The reason your custom query isn't working is that post-type is not a valid parameter, it's post_type (underscore, not hyphen).
If your goal is to ultimately change the sort order of your CPT archive, then you're in ...
1
It sounds like you're looking for get_term_link( $term_ID ) which takes the term ID and returns a link.
===
EDIT: Try this:
$term_id = get_query_var('tag_id');
$term_link = get_term_link( $term_id );
1
When using custom post types I got around this problem by using the following code. Of course you will need to put in the HTML for how you want it to display on your site.
<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query(''); // Enter you query here
?>
<?php while ...
1
I would use a query parameter, something like ?time=future maybe, to shift your query. The difference between the future posts query and the past posts query should just be the orderby and the meta_query, so it should be relatively easy to change that based on the URL parameter. You also have the added benefit of reducing overhead by keeping it to one page. ...
1
You want is_paged() (see Codex). This returns true if you are on page 2,3, etc and false if you are on the first page. So...
if(!is_paged()){
//Display current / future sermons
}
//Display archived sermons.
1
So I think you are asking how to dynamically highlight the tab of the page you are currently on. I did something like this on my recent website project because I needed to highlight a certain tab when I was on a custom post type.
Here is some code you could use for your website:
function custom_tab_highlighting() {
global $post;
?>
...
1
Your custom post type is je_news. This means that your single and archive files should be called single-je_news.php and archive-je_news.php.
The has_archive option enables you to change the archive url, but it doesn't override what the templates should be named.
Only top voted, non community-wiki answers of a minimum length are eligible