WordPress - Custom Post Type Entry Won't Load Into A Page - wordpress

I created a very basic custom post type in my WP site to hold events. Basically, a user can input an event title, some information about the event and the date.
Currently, the only time the events are being displayed are in my sidebar where I just pull the title and date of the upcoming events.
However, I want to be able to allow the user to also click on the title of the link to lead to the event's individual page. I tried previewing my custom post type event and it kept leading me to my 404.php page. The events don't seem to load into a page at all.
I'm hoping this is a small property that I didn't set when I registered my post type. Any help?

Wordpress has incorporated custom post types into the template hierarchy and you have access to them at single-$posttype.php and archive-$posttype.php.
If you create these templates and your posts don't show up in the loop, you need to reset your permalinks/flush your rewrite rules.
As well, it would help to see how you are registering your post types, as in order for your archive-$posttype.php to be available you need to call has_archive => true in your arguments.

What i've done when i needed it:
Add this code in the same function that create your post type:
add_action("template_redirect", array(&$this, 'template_redirect'));
function template_redirect() {
global $wp;
if ($wp->query_vars["post_type"] == "events")
{
include(TEMPLATEPATH . "/events.php");
die();
}
}
Then, just create events.php with the_loop (like single.php)!
Hope it helps
[]'s

Related

how to hide a page from being seen in wordpress backend and frontend

In my plugin i have created a custom template that prints a requested sidebar. and for running the code of this template i assigned a custom page to it (by calling update_metadata) .
Is it a good idea for getting content of a specific sidebar into Ajax call ?
Now my problem is that WORDPRESS shows it in the dashboard and front page , and after searching i have not found any easy to understand solution for Hiding a page completely so only can be accessed by its id .
Can any one tell me how to do that ?
you are going about this the wrong way. You can create a function that can create anything that can be created on a wordpress page.
But if you really must you can create a page outside of the database, etc:
add_action('init', 'add_rewrite_rule');
function add_rewrite_rule(){
// add_rewrite_rule(REGEX url, location, priority (i.e. top is before other rewrite rules)
// I created a custom post type for this plugin called market -- replace post_type with whatever you want
//basically tell wordress to add a query var if sidebar is added to url.
add_rewrite_rule('^sidebar?','index.php?is_sidebar_page=1&post_type=market','top');
}
// register a query var
add_action('query_vars','market_set_query_var');
function market_set_query_var($vars) {
array_push($vars, 'is_sidebar_page');
return $vars;
}
// associate a template with your quer_var
add_filter('template_include', 'market_include_template', 1000, 1);
function market_include_template($template){
if(get_query_var('is_sidebar_page')){
$new_template = (theme or plugin path).'/pages/yourpage.php'; // change this path to your file
if(file_exists($new_template))
$template = $new_template;
}
return $template;
}
This will not be a page that will be in the admin section or in any query that relates to pages but someone could of course navigate to this page. But as i said above you would be better to create a function to create your sidebar. If you want a seperate file to handle the "view" you use require_once 'filename'; a file and keep your functions area free of html.
If you are creating functions in a wordpress plugin dont forget many functions may not be available until later in the load process. Use add_action() if you run into any undefined functions
edit:
you are loading wordpress before you get to the template so you have all the functions. (google wp load for more info) + get_header() / get_footer() will also load a few things like css, etc. I had a small typo in the code above, fixed that but basically what you are doing is telling wordpress if someone lands on www.example.com/sidebar to apply a query_var (rewrite rule). Wordpress will look up its saved vars (final function) and return the template assoc. The 2nd function just registers the var.
You also have wp_functions in any file you create and include in a plugin, etc hence why you can create a file that does exactly the same as this page.

Dropdown of existing posts in a metabox

I want to have ability to choose for each page what post should appear in a sidebar, from multiple posts type. So I understand that I need a meta box with a dropdown list of all posts, but I don't know how to build this in functions.
I only found this solution which is quite similar to what I want, but this doesn't help me to much, because I can only choose from a single post type and display only in post pages.
There is a free plugin that will solve all of your woes. It's called ACF or Advanced Custom Fields. It has the ability to add a list of posts to a field and attach that field to pages. Here's how you'd do it:
First install the plugin and navigate to the custom fields screen. Setup your field exactly like this:
Then in the options below that section you need to select these options:
That will tell ACF to put the field only on pages. After you have set that up you will get a little sidebar block like this:
You can then select each post for the page and it will return that object on the frontend. You do need to use a little code to get the frontend to spit out the posts you need. Here is the code to get a frontend option from ACF. Inside of the sidebar.php file you need to add this code:
global $post; // Get the global post object
$sidebar_posts = get_field('posts', $post->ID); // Get the field using the post ID
foreach($sidebar_posts as $sidebar_post){ // Loop through posts
echo $sidebar_post->post_title; // Echo the post title
}
This will simply loop through the posts you select and echo out the title. You can do more with this by adding some other Wordpress post functions using setup_postdata(). This will allow you to do things like the_title() and the_content().
Hope this helps!

Custom Post Type - Single-{slug}.php to override Single-{$posttype}.php in WordPress

I am looking to add a custom template for a single post inside of a Custom Post Type. The Custom Post Type is working as it should and all of the posts are correctly using single-{$posttype}.php. However, for the page with the slug "our-wedding", I am trying to override single-{$posttype}.php and have it use single-our-wedding.php. However, the page is still using single-{$posttype}.php.
Any ideas?
You can either use a solution that will let you assign custom templates to one particular post of that post type (there are plugins).
Or you can edit the single-{$posttype}.php to include a
if( is_single('our-wedding') ){
get_template_part( 'my-template');
} else {
// The usual code for this single-posttype
}
And then create a file called "my-template.php" inside your theme folder.
(Edited based on user feedback.)

Wordpress Custom Post type page display

Hi I am reading Building wordpress themes from scratch in order to understand theme development.I got to a point in the book where it explains how to create the custom post type pages.And the author mentions that this function is required in order to display the page:
add_action('init' , 'director_rewrite');
function director_rewrite(){
global $wp_rewrite;
$wp_rewrite->add_permastruct('typename','typename/%year%%postname%/' , true , 1);
add_rewrite_rule('typename/([0-9]{4})/(.+)/?$','index.php?typename=$matches[2]', 'top');
$wp_rewrite->flush_rules();
}
I have deleted the function and the page still display corectly without it.That leads me to belive that I do not understand wha this actualy does.
So is this function required to properly display custom post type pages?If so what does it do?
It adds rewrite rule for the permalinks structure of your page. If you remove it, the page still show correctly, but link towards your page is different!

Can WordPress post content be external data (not from the database)

I am trying to display a page based on some data returned from an external API (Amazon). This data is formatted then, has to be displayed on a page, created on the fly, based on URL querys. I can already do this with shortcodes but this has to be from the query.
I see all kinds of info in the codex on returning custom query_posts into the loop from the database. However, I cannot find info getting external data to appear on a page.
Is this possible in WordPress? (anything is possible, right?) Just point me to some functions, filters or tutorials please.
If I understand you correctly, you want to retrieve data dynamically and display it in a WordPress page?
There are many ways to do this, but here's one option:
Create a Page Template
Create a WordPress page and use the Page Template created in step 1.
Edit the Page Template to call the external API and display the data
I'm guessing you've been trying to find a way to do this from the "content" of a page or post, but the easiest way is to put the code in a custom Page Template.
UPDATE: If you want to programmatically create a page, this might work for you: http://wordpress.org/support/topic/how-to-create-pages-programmatically?replies=5#post-1230619
http://www.prelovac.com/vladimir/wordpress-shortcode-snippet-to-display-external-files
This is a snippet to display external data in a post.
Would this help? If the dynamic page was a HTML page and THEM displayed in WP.
Yep, Thats possible.
I Did by using bridges.
You can do it by add "add_meta_boxes" action.
Inside the meta box function you can add call and get the external page contents, or can give your own forms .etc
My Code :
/*
* Add Meta Product Type Field to POST
*/
add_action('add_meta_boxes', 'meta_box_product_type_add');
My Meta Box
/*
* Product Type Meta Box Init
*/
function meta_box_product_type_add()
{
add_meta_box('ptype_testing', 'Product Type', 'add_ptype', 'testing', 'normal', 'high');
}
/*
* Product Type Field
*/
function add_ptype()
{ ?>
<label>Type of Product : </label>
<select name="ptype" id="ptypes">
<option>----Select----</option>
<option>Physical</option>
<option>Virtual</option>
</select>
<label>Unit :</label>
<select name="punit" id="units">
<option>----Select----</option>
<option>KG</option>
<option>Mtr</option>
<option>Ltr</option>
</select>
<?php
}
Try It....

Resources