How to define a file/page using conditional comments? - php

I am curious as to how to utilize conditional comments to define pages/files with PHP.
This technique is used in WordPress; one can define the template file by inserting conditional comments at the top of the file. Then when you select this template, the application knows which file to use. Something like this;
<?php
/*
Template Name: Snarfer
*/
?>
I would like to point out that I do not wish to perform this actin in a WordPress environment, but I am curious as to how they do it.
So how can one use this technique in PHP? I would assume one needs to create a function or a class or a series of functions and classes.
I have tried searching but perhaps I'm using the wrong keywords in my search.
If anybody could point me to documentation or tutorials i would be very appreciative.

I think you must have to give the condition on wordpress archive.php to check which custom post type you like to use and you can define here to which templete file you like to call.
<?php if ( 'news' == get_post_type() ) { ?>
<div class="container">
<?php
/* Include the Post-Format-specific template for the content.
* If you want to overload this in a child theme then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'news-blog', get_post_format() );
?>
</div>
<?php } else { ?>
Wordpress Default call
<?php } ?>
in get_template_part( 'news-blog', get_post_format() ); having news-blog is the file name of you template. for example news-blog.php

Related

Wordpress single.php another get_post function destory the first function

I am trying to deal with two get_post functions, in single.php the first get_post function is the post from the wordpress, But after that I called get_post function to other post also to use both of them in the same page but after I call the first get_post ( the main post ) I get the only second data and cant reach the first data.
My code called to the second function ( The first is from wordpress post):
$main_post = get_field('main_post');
$main_p = get_post($main_post->ID);
Then I am trying to use the variable $post OR the_title() OR any other functions to get the first post and it always returning the info of the $main_p post
for example
get_the_title( get_post()->ID )
returns the $main_p post id and not the main post of the single.php
any soulutions ?
I may be wrong, but it seems to me that you are trying to post a different post format with normal post format?
I, myself use get_post_format() so it can be styled differently or have different options.
in single.php I use
<!-- checking if there are any blogposts to be shown using have_posts check which is a wordpress function-->
<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?> <!-- the correct syntax for while loop in wordpress to show all the blogposts -->
<?php get_template_part('content', get_post_format()); ?>
<?php endwhile; ?>
<?php else : //else stament if there aren't any posts (put inside if before endif)?>
<p><?php __('No Posts Found'); ?></p>
<?php endif; ?> <!-- stop checking for blog posts-->
</div><!-- /.blog-main -->
Inside functions.php I activated the post-formats inside wp_theme_setup() function
add_theme_support('post-formats', array('aside', 'gallery'));
In this case i activated the gallery and aside posts
That way I have 2 different post types on one page
like this
image from my theme blog page
Here is also a video tutorial on post formats from Traversy media
https://www.youtube.com/watch?v=CRa7eiqyiCM&list=PLc5p9nvpdwBlrNU0hr1f0kXPRkh0aGo1Q&index=7
The key reason why your post values are being overwritten, the additional get_post() declarations are overriding the default query. Now, the code in your pastebin is a pretty massive dog's breakfast, so a direct solution is a rather large undertaking (e.g. the indentation is all over the place, the code snippets are less than ideal regarding their readability, etc...). However, I can point you in the right direction for the solution.
When I pull content from another page on my WordPress sites, I avoid using get_post() in favour of declaring a fresh new WP_Query() (that's just my preference), following it up with a wp_reset_postdata() declaration.
Here's an example of multiple queries on a single template in the WordPress codex:
https://codex.wordpress.org/Class_Reference/WP_Query#Multiple_Loops
The key here is the wp_reset_postdata(). I'd recommend looking into it's purpose. It'll save you a lot of grief:
https://codex.wordpress.org/Function_Reference/wp_reset_postdata

Custom post_type template with get_template_part

So, my parent-theme uses a single.php calling a content-single.php with the function get_template_part().
The code for single.php:
get_header(); ?>
<div class="container">
<main id="main" class="postItem" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?>
<?php get_template_part( 'content', 'single' ); ?>
<?php codex_coder_content_nav( 'nav-below' ); ?>
<?php endwhile; // end of the loop. ?>
</main><!-- #main -->
<!--<?php get_sidebar(); ?>
I am using a custom post_type named "ourNews". Following wordpress documents, I created two files: single-ourNews.php and content-single-ourNews.php
With this, in my "single-ourNews.php" I changed the following line:
<?php get_template_part( 'content', 'single-ourNews' ); ?>
But it keeps loading the file "content-single.php". What am I doing wrong?
Other question is: How can I put a image with relative path on this custom template? I created a folder "img" and I was calling using:
But says that the image was not found. I read a little and some places said that I could not use relative path, but why not, if the theme uses? I'm confused.
Wordpress is still loading single.php that loads content-single.php
Your custom post is not ourNews. This is probably the label but not the slug. The slug is always lowercase and separated by a -. Try renaming your file to single-our-news.php.
Also, every time you register a custom post type, you should reset your permalinks options back to default and then back to what you want it to be.
For the relative path, you should not use it. You should use .'/img/name-of-image.jpg
When using get_template_part, the first parameter defines the base template name (e.g. content, which loads content.php) and the second parameter loads a suffixed version if available. Therefore your call
get_template_part('content', 'single-ourNews');
is looking for a file named content-single-ourNews.php first, then falls back to content.php in case the first one is not available. I'm not sure whether the get_template_part function converts the suffix parameter to something like single-ournews or single-our-news before appending it to the first parameter, be sure to test a few variants of that.
I'm not 100% sure if the function behaves differently or not in child themes and parent themes. One option is to override the parent's single.php in the child theme and modify it directly with
if ($post->post-type === 'ourNews') {
get_template_part('content', 'single-ourNews');
}
else {
get_template_part('content');
}
Lastly, WordPress will look for a file single-[cptslug].php before loading a template for a custom post type's single view.

Inserting custom content block into wordpress

I've been looking around for a while on how to do this but I don't think I've been wording my searches correctly. I'm developing a custom theme and want to break out a section of a page into a reusable piece that I can reference like you would insert a header with <?php get_header(); ?> but I want to use a different header file instead of header.php. How would I do this?
You can use the function like this:
<?php get_header( $name ); ?>
$name :: (string) (optional) Calls for header-name.php.
if you have a seperate header for home page you can create a header file like this "header-home.php" to call that header file use the get_header function like this:
<?php get_header( 'home' ); ?>
Also you can call multipe header in a theme like this according to different conditions
<?php
if ( is_home() ) :
get_header( 'home' );
elseif ( is_404() ) :
get_header( '404' );
else :
get_header();
endif;
?>
You can specify which header file you want to use by naming it, as Ajith R Nair said in his answer. <?php get_header('new');?> would load the file header-new.php in your theme's directory. If you plan on changing small parts of your header, I suggest not using duplicate header files, but instead define a global variable that holds an array of default parameters in the header file, and just use that same global variable in your theme pages to override defaults. Hopefully that points you in the right direction, though I would be happy to clarify if needed.

Display posts on tag.php template?

Is it possible to create a tag.php template that when you navigate to the url www.domain.com/tag/architecture/ it will display all the custom posts that have been tagged with that specific tag? And so on for various other tags?
What code will I need to include on my template?
Yes You can create,below is the code i used to display custom post type "knowledge"
<?php
global $query_string;
$posts = query_posts($query_string.'&post_type=knowledge');
if ( have_posts() ) while ( have_posts() ) : the_post();
?>
<?php the_title(); ?>
<?php endwhile; // end of the loop. ?>
This will help you understand hierarchy of templates:
http://codex.wordpress.org/images/1/18/Template_Hierarchy.png
Use of $query_string (example) available here:
https://developer.wordpress.org/reference/functions/query_posts/
According to the template hierarchy, you can create a file called tag.php which will be used instead of index.php if a tag page is displayed. You can also prepare separate templates for specific tags.
The best way is to start by creating a copy of your theme's index.php and calling it tag.php, this way you'll have some basic code working. Then you can modify tag.php to fit your needs - probably by editing The Loop, or maybe changing some includes if your theme loads the loop from separate file. (but in this case you should consider editing index.php to load some other loop for tag pages - is_tag() may come in handy)

How to make a page for Custom Post Types in Wordpress 3.0?

I have some custom post types, such as 'review'. I can't seem to find out how to make a section (e.g., www.mysite.com/reviews/) which works like the blog home page, but lists reviews instead of posts (with pagination and everything). I'd like to use a separate template for it too.
Create a page called reviews and then create a new template in your theme folder called page-reviews.php. Add all the necessary elements to the template and include a query_posts in front of your post loop. It should look like this:
<?php query_posts("post_type=reviews"); ?>
<?php if (have_posts()) :?>
<?php while (have_posts()) : the_post(); ?>
<div class="post" >
<h2><a href="<?php the_permalink() ?>" ><?php the_title(); ?></a></h2>
<?php the_content(); ?>
</div><!-- Post ends -->
<?php endwhile; ?>
<?php else: ?>
<p>Sorry, we could not find what you were looking for</p>
<?php endif; wp_reset_query(); ?>
You need help getting .htaccess to convert your categories into hard URLs. I thought WP did this automatically so you'll want to look and make sure you have set up the directory permissions on WP so it can write to your .htaccess file.
Please read this guide and it will be cleared up.
Duplicate the file in your theme called "single.php" and rename it to "single-reviews.php"
Since "single.php" is used for your regular posts, you can append the name of the custom post type to the end of "single-" to automatically use that.
Now once inside of the file "single-reviews.php" you can adjust the layout to be how ever you want.
If you get a 404 error, or it is not showing you the correct layout, you may need to flush the rewrite rules. You can do this two ways.
Go to the permalinks page in your backend and it will sometimes auto flush them.
The best way to do it is in your "functions.php" file in your theme directory add the following code:
add_action ( 'init', 'flush_rewrite_rules' );
function flush_rewrite_rules()
{
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
Create a new page called reviews. Create a new page template that calls the custom post type. Assign the page template to the page...

Categories