How to Show Custom Post on different Single.php File in wordpress? - wordpress

i have created a Custom Post type Movie, and Also created a page Movies and Showed all the movies on that page. Great….
“But the Problem is”, When I click on that movie, Its’ going on the same single.php page, and that’s what I don’t want, I want a Saprate Single.php file for this movie section and seprate others like news, videos, so tell what i’ll do for this

Create single-movie.php file in your theme. Put custom code in there.
For any other custom post type use single-{custom-post-type-slug}.php

heres how to do it.
Delete everything in your single.php insert the following, and create multiple single.php files for your needs.
[note: in_category('id of your category')
<?php
$post = $wp_query->post;
if ( in_category(‘3′) ) {
include(TEMPLATEPATH . ‘/single-photo.php’);
} elseif ( in_category(‘4′) ) {
include(TEMPLATEPATH . ‘/single-video.php’);
} else {
include(TEMPLATEPATH . ‘/single-default.php’);
}
?>
Good luck ^ ^

Related

Switch Between Multiple Headers in WordPress Theme

I am building a custom theme for WordPress. One thing I want to do is allow myself to switch the header from the edit page.
I know I can hard code in the header to switch based on the page ID or name, like this:
<?php
if(is_page(10)) {
get_header('new');
}
else {
get_header();
}
wp_head();
?>
But I want a drop down menu similar to the Page Template option in the sidebar. (See screenshot)
Screenshot of sidebar menu
I have looked for any online tutorials that cover this type of option, but they all cover the basic ID or name setup shown above.
Does anyone know of a good tutorial to create a drop down similar to Page Templates to use for multiple headers?
You can use Custom Metaboxes. That link contains a comprehensive tutorial on creating custom metaboxes for post meta fields by hand. You can also use a plugin like Advanced Custom Fields to create them.
Doing this would allow you to check for the header style value through get_post_meta() or get_field(), respectively.
<?php
// If using the Custom Metabox/post_meta approach:
$header_style = get_post_meta( get_the_ID(), 'my_custom_header', true );
// If using ACF:
$header_style = get_field( 'my_custom_header', get_the_ID() );
if( $header_style == 'new' ){
get_header('new');
if( $header_style == 'something-else' ){
get_header('something-else');
} else {
get_header();
}
?>

How to remove wordpress date archive pages?

I have developed my own wordpress theme, and learning all kinds of programming stuff, but my priority is the content, not the programming knownledge. I need to know how to remove archive date listing from wordpress?
Question 1: The google search results displayed like this: virmodrosti.com/2017/05/
I don't want any kind of date archive option, how do you disable that?
I also don't use any kind of plugin, and always like to do it on my own.
Question 2: I don't know why older entries doesn't work anymore
virmodrosti.com/zdravje/ this page works fine
virmodrosti.com/zdravje/page/2/ it redirects to 404 error page
I only choose option in wordpress to hide that annoying /category/ with dash . inside editor at permanlinks, Category base. Maybe somehow these stuff is kinda fighting with each other and doesn't work properly.
Thank you.
This is code from Digital Nomad theme I maintain:
function digitalnomad_remove_date_archives() {
//if we are on date archive page
if ( is_date() ) {
// theme sets alternatine archive page with table like list of all posts
$archive_page = get_option( 'digitalnomad_archive_page' );
if ( $archive_page ) {
// redirs to alternatine archive page if configured (good for SEO)
wp_redirect( esc_url( get_page_link( $archive_page ) ) );
die();
} else {
// otherwise error 404 is displayed
global $wp_query;
$wp_query->set_404();
}
}
}
add_action( 'template_redirect', 'digitalnomad_remove_date_archives' );
Use the smart Archive Page Remover wordpress plugin
or visit your theme's functions.php file
then insert this code
/* Register template redirect action callback */
add_action('template_redirect','makes_remove_wp_archives');
/* Remove archive*/
function makes_remove_wp_archives(){
// if we are on category or tag or date or author archive
if(is_category()|| is_tag()||is_author()){
global $wp_query;
$wp_query->set_404();
}
}

I see my URL but I can't find the page in wordpress dashboard

I'm using wordpress and my page has the URL http://proservicescontractors.com/services/
But when I go to the page in my dashboard with the above URL, any change I make does not show on the front end. I tried simply duplicating my content and that change did not show on the front end.
Not sure what to do, this has me completely baffled.
Any ideas?
Since they're custom post types, by default, they're not actually loaded into a page per se. You should read up on WordPress's template hierarchy. To give you a rough idea of what's happening:
WP looks at your URL, and since it recognises it as a custom post type archive, it will look for a template to use...
It will first look for archive-$post_type.php, or in your case, archive-services.php
If it can't find that, it will look for archive.php
If it can't find that, it will use index.php
The important thing to note is that archive pages don't actually show up in the admin area, since they simply gather up and display custom posts, so there's nothing for you to edit.
Now, if you really want to edit some content on the Services archive, you have two options:
Edit archive-services.php in a text editor.
This is the quick and dirty option; the downside is that it defies the point of a CMS.
Create a page template with it's own loop
Create a new page template called page-services.php and insert a loop in there to display your custom posts. To get you started:
<?php get_header(); ?>
<?php // The main loop
if (have_posts()) {
while (have_posts()) {
the_post();
}
} else {
echo 'No posts';
}
?>
<?php // Now for the services loop
// WP_Query arguments
// For additional options, see: https://codex.wordpress.org/Class_Reference/WP_Query#Parameters
$args = array (
'post_type' => array( 'services' ),
);
// The Query itself
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// Do something with the post
// In your case look at archive-services.php and see what
// that template does inside the loop
}
} else {
// no posts found
}
// Restore original Post Data
// Don't forget this, it's important
wp_reset_postdata();
?>
<?php get_footer();?>
You should then be able to apply that page template to your Services page; it should then display your posts below the page content. One thing to look out for is that WordPress will continue to load archive-services.php whenever you go to http://proservicescontractors.com/services/. While there are ways around this, the easiest fix would be to simply give your new page a different url, such as http://proservicescontractors.com/all-services/
Thanks for your help. I'm using yoast and I wanted to change the title and description. When you pointed out that it was a custom post type archive and not a page, I went back through yoast and found where I could change them under "Titles and Metas" > "Custom Post Type Archives" > "Services"

How to create two different single.php for same post type?

I have a post type named 'Property'.
I want to show single post in two different way.
if anyone click on post then it will shows a simple layout with name of post and description.
Now i have also category for beds. now if anyone goes in category '2 Bed' then you can see all post with '2 Bed' categories('its done'). but now if anybody click on post then it have to show different single page.
my English is very bad so please excuse it.
You can set up individual templates for a single category by using the single_template hook.
Put this in your functions.php file:
function my_category_templates($single_template) {
global $post;
if ( in_category( 'property' )) {
$single_template = dirname( __FILE__ ) . '/single-property.php';
}
// Copy the above for your other categories
return $single_template;
}
add_filter( "single_template", "my_category_templates" );
You can then create individual single templates for each category, just add more conditions and point them to the template you create.
There is the concept of Category Templates as dictated by the
Template Hierarchy but because you are asking how to display a "single" post based on category, you will want to use the in_category() Conditional Tag in the Template file you use to display singe posts. The Loop article has an example of using in_category.
Or look at this concept:
http://www.nathanrice.net/blog/wordpress-single-post-templates/
Or this:
http://justintadlock.com/archives/2008/12/06/creating-single-post-templates-in-wordpress
Or this plugin:
http://wordpress.org/extend/plugins/custom-post-template/
Or this plugin:
http://guff.szub.net/2005/07/21/post-templates-by-category/
So according to the Wordpress template hierarchy there is only one single.php, that cannot be separated by category (like the archive page for example.)
https://developer.wordpress.org/themes/basics/template-hierarchy/
So in this case I suggest you read the current category id in your single.php file and then adjust the content to your needs. You can use get_the_category() do do this (reference: https://developer.wordpress.org/reference/functions/get_the_category/) which will return you an array with categories. In my simple example I just pick the first category to do something:
$categories = get_the_category();
$category_id = $categories[0]->cat_ID;
if($category_id == 1) echo 'do something here';
Thanks for you help. I am done with it myself.
I create single.php for simple format and and in category result i didn't use the_permalink and call id of post and made a url like http://localhost/demo/page?id=$id

is there a way (plugin) to insert html/css/images ect automatically into every wordpress post?

is there a way (plugin) to insert html/css/images ect.. automatically into every wordpress post? most of my posts are going to be very similar so is there a plugin that will automatically insert the pre written html/css/ ect in every post as opposed to me entering it manually every time.
Thank you in advance ;-)
You can write your own simple function for this, see my example below:
add_filter( 'default_content', 'my_editor_content' );
function my_editor_content( $content ) {
global $post_type;
if( $post_type == 'post') { /* Or your custom post type, pages etc. */
$content = 'Your custom HTML/CSS content here';
}
return $content;
}
Place this in functions.php and it will be the default content of every new post/page/custom post type you create.
For a list of available post types, please refer to the Codex
You could use a plugin such as Ad injection, it will allow you to do what you need without having to alter / amend / ad any code to the templates or files

Resources