Display posts on tag.php template? - php

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)

Related

Create a second single.php file

I'm trying to create a second single.php for my WordPress theme.
The first single is used with get_permalink() but it's impossible to create another single2.php
How is this feasible?
I'd like to use the single2.php the same way as the regular single.php
I've seen some tutorials but every time they associate the single2.php with the creation of WP categories and I know it's doable without it. I just don't know how.
Open single.php in your HTML editor. Delete the contents of single.php and place the following code in it. Substitute the number 3 for the Category ID # you noted.
<?php
$post = $wp_query->post;
if ( in_category('3') ) {
include(TEMPLATEPATH . '/single2.php');
} else {
include(TEMPLATEPATH . '/single1.php');
}
?>
You should have a look at the Wordpress Template Hierarchy, it could give you tips to do that :
You could create a page-{slug}.php or a page-{id}.php file for example, but it's not the nicest way as it's only for a specific slug or specific id.
I'd recommend to create a template file, where you can create your page. This template can be reuse for several pages !

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.

How to define a file/page using conditional comments?

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

get post content into header

i have to get the post content into the tag <head>.
I was trying with this code into the header.php file of my theme:
if(is_single()){
$stringa = the_content();
}
but it doesn't work.
how can i do?
thanks
The functions the_content() and get_the_content() are meant to be used inside the WordPress loop, which means you can't just use them at will. You'll need to build a loop inside your header.php file that queries the WordPress database, fetches some content, and uses it as necessary.
Basically, wrap your the_content() call inside:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
...
<?php endwhile; endif; ?>
Then you'll be able to fetch post content anywhere on the page ... however, I don't quite understand why you're trying to get the post content inside the <head> section of the page. <head> is used for style declarations, <script> tags, and meta information about the page ... not for actual page content. If you're trying to get specific information about the current page, I'd recommend using a different function entirely.
I think what you are looking for is:
$stringa = get_the_content();
if (is_single())
{
the_post();
$content = get_the_content();
rewind_posts();
}
It's important to put rewind_posts(), otherwise post loop will not work in other templates.

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