create a different index.php for post in wordpress - wordpress

I'm working on a wordpress that has, among other things, a Front Page and a Post Page for showing news.
From what I understand, the template that the Post Page uses, is the index.php, but the index.php is also used by other pages.
The thing is that I want to create a special page for post, with a header that says news, etc., but I don't want other pages to use that template.
Is there a way to create an alternative index.php (index_news.php) that is only used to show posts?
That way I can use index_news.php for the post and index.php for everything else.
Thanks
EDIT //////////
My first option was to create a new page-template (news.php) with the loop inside, and then, in the the wordpress options, targeted the post to that new page (news.php).
But when I do that, it loads the index.php not the news.php template. Maybe is something wrong with the loop. Here is the loop I'm using:
<!-- Start the Loop. -->
<div class="post">
<!-- Display the Title as a link to the Post's permalink. -->
<h2><?php the_title(); ?></h2>
<!-- Display the date (November 16th, 2009 format) and a link to other posts by this posts author. -->
<small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>
<!-- Display the Post's content in a div box. -->
<div class="entry">
<?php the_content(); ?>
</div>
<!-- Display a comma separated list of the Post's Categories. -->
<p class="postmetadata">Posted in <?php the_category(', '); ?></p>
</div> <!-- closes the first div box -->
<!-- Stop The Loop (but note the "else:" - see next line). -->
<?php endwhile; else: ?>
<!-- The very first "if" tested to see if there were any Posts to -->
<!-- display. This "else" part tells what do if there weren't any. -->
<p>Sorry, no posts matched your criteria.</p>
<!-- REALLY stop The Loop. -->

single.php - is for one post view, but if You wolud like to modify blog index, than you could also create a custom template (page-news.php) with a header that allows it to be separated as page template:
<?php
/*
* Template Name: News Page
* #package WordPress
*/
?>
and then use loop inside of it.
But for this case what I think is even better, You can modify header.php and use wordpress conditional tags such as:
is_front_page(), is_page() or any that will work for You: http://codex.wordpress.org/Conditional_Tags
There are many ways to do same ;).

Related

Include Advanced Custom Fields (ACF) in Wordpress the_content()

I've inherited a project structure that uses front-page.php to render the home page. Most of the site uses the Wordpress content field, but we've switched to building new features with Advanced Custom Fields. the_content() is used throughout the codebase to render content, and I'm wondering if there is a way to include ACF layouts in the_content().
An example workaround I've had to use for the homepage is below, if I don't call the layout in this way content won't render on the page. I'm new to ACF, so if what I'm describing is a fundamental misunderstanding of how to bring ACF into an ecosystem I'd appreciate any guidance on better file structures and calling content.
<?php get_header(); ?>
<?php
if (is_page('Home')) {
?>
<div class="container">
<div class="row">
<?php
get_template_part('/layouts/home');
?>
</div>
</div>
<?php
} else {
the_content();
}
?>
<?php get_footer(); ?>
You can use ACF in addition to the original "the_content()" or "get_the_content()" functions. That content comes from the default block or WYSIWYG editor. ACF comes from additional fields that you add to your pages or posts.
<?php get_header(); ?>
<?php
if (is_page('Home')) {
$home_acf_field = get_field('field_name_from_home_acf');//this assumes that the ACF field has been added to the home page, because I'm not using the second parameter
?>
<div class="container">
<div class="row">
<?php
get_template_part('/layouts/home');
echo $home_acf_field;//this could also be placed in "layouts/home"
?>
</div>
</div>
<?php
} else {
$acf_field_from_page = get_field('field_name_from_page_acf');
the_content();
echo '<div>'.$acf_field_from_page.'</div>';//an example of where this can go
}
?>
<?php get_footer(); ?>
You can incorporate ACF in the template part, or in the page itself. You need to have the ACF field set up in the CMS to be on home or wherever, but then it's as simple as what I showed you to pull it in (my examples were assuming text fields).
You aren't asking this question, but if you are using "front-page.php" as your home page, then why do you need the if statements to show content? You can use "the_content()" in addition to your ACF fields, especially if it's the content editor from the home page. Or, you can just hide it (it's an ACF setting).
Note: I wrote this from memory, and didn't test anything, so please excuse any potential typo. :)

Wordpress - Post Page Associator Issues

For a client site that I am working on, the client wants to be able to associate certain custom post types to a given page. my supervisor has pointed me to the website of a previous client that did something similar. They used the Post Page Associator plugin (which seems to have been removed from the plugins list).
Here is a link to the plugin I am using: http://www.mediafire.com/?2dpbxac73rsn8l6
I've tried my best to modify the code inside the plugin to handle the custom post types I wanted to use (which the changes are included in the download). The main conflict comes from this piece of code (from the previous client's page.php file)
<?php
$sidebar = new WP_Query( $args ); //queries for posts
$sidebar = wp_plugin_post_page_associator::get_associated_posts ($post->ID); //looks for associated posts via the plugin, the main offending line of code
if ($sidebar!="") : while ( $sidebar->have_posts() ) : $sidebar->the_post();
?>
<div id="sidebar-content"> <!-- sidebar area -->
<h1 class="blue-text"><?php the_title(); ?></h1> <!-- the title of the associated post -->
<div class="divider"></div>
<?php the_content(); ?> <!-- the content of the associated post-->
<br/><br/>
</div>
<?php endwhile; else:?>
<br/><br/>
<?php endif; ?>
I was wondering if there was a way to determine what kind of post type the associated content is so I can change the WP_Query settings accordingly. If I comment out the second line of code with $sidebar, then all posts gets displayed in the sidebar. Is there a solution to this problem, or is there a better plugin for this?
I found a replacement plugin that does exactly what I need and shows me how to do it: http://www.cssigniter.com/ignite/custom-post-types-relationships/

Category Images Only Displaying Once on Special Recent Posts (WordPress)

I am using WordPress to put together a blog. I am using the Category Image(s) plugin to put an image for each category above the posts.
The main page is laid out as a big image and excerpt for each article and when you click on it, it takes you to the full article (I am using the 'Special Recent Posts' plugin for this). I want category image headers above each big image/excerpt.
Everything works fine for the first article but after that I get no headers. The reason is because the code I have in my header is calling the 'Category Image(s)' php function, which works. Then it calls the 'Special Recent Posts' php function which in effect runs the loop to grab the first five most recent articles. It doesn't run the category images function every time for every article, only the first time.
Here's the code:
<?php get_header(); ?>
<?php c2c_the_category_image($image_extensions='png gif jpg', $image_dir='/wp-content/images/', $use_name_if_no_image=true, $start_from='begin', $limit=999); ?>
<?php echo do_shortcode("[srp srp_number_post_option='5' srp_thumbnail_option='yes' srp_widget_title_hide_option='yes' srp_post_date_option='no' srp_wdg_excerpt_length='50' srp_wdg_excerpt_length_mode='fullexcerpt']"); ?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
How can I get it to run the category images function for all the recent posts? Thanks for the help.
EDIT:
I attempting to go into the PHP of the Special Recent Posts plugin but when I attempted to enter the category images code in, it created some critical errors. I'm looking for the easiest solution if it's out there (I know this isn't a very simple question to start with). Any help? (I've placed a bounty)
Your way of doing things is extremely complicated and convoluted.
The simplest way that is proved to work is by having images that correspond with either category ID or category slug!
http://baldino.rs/collection
As you can see, this is a page that lists all the categories with their images and descriptions.
I'm having little trouble understading if you want:
To have a category image above every post
To have a category image AND post image above every post
I'll demonstrate the first solution below
Here is the order in which we will create the blog page:
Name the category images either by slug, or by ID (i recommend ID)
Upload the images to the server - prefferably to the images folder of the theme
Create our custom loop which will display our category image, category name, post title and excerpt
So the index page (blog page) should look something like this
<?php get_header();?>
<div id=:content">
<div id="posts">
<?php if (have_posts()) : while (have_posts()) : the_post();?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<?php
$category = get_the_category();?>
<img src="<?php bloginfo('template_directory'); ?>/images/<?php echo $category[0]->term_id;?>.jpg"/>
<h3 class="cat-title"><?php echo $category[0]->cat_name;?></h3>
<h2><?php the_title(); ?></h2>
<div class="entry">
<?php the_content(); ?>
</div>
</div>
<?php endwhile;endif; ?>
</div>
<?php get_sidebar(); ?>
</div>
<?php get_footer(); ?>
You should really avoid plugins for the simple stuff that is easily solved by the loop
I recommend that you start reading the WP codex - http://codex.wordpress.org
Every function, class, reference is documented in detail and it's very easy to understand.
If you have more questions, do not hesitate to ask

Same editable region/content same on all pages

I'm working on this website in WordPress and I want the content with the title "Featured Business" that's on the sidebar to appear on all pages. I created a template for it but it doesn't display when called. The code is as shown below.
<div id="ftb">
The content
</div>
<!-- End FTP -->
and I called it with <?php get_ftb(); ?> But it doesn't show.
If you want to display a chunk of html that's part of the sidebar (but not the entire sidebar), I recommend to place it on functions.php, like this:
<?php
function get_ftb() {
?>
<div id="ftb">
The content
</div>
<!-- End FTP -->
<?php } ?>
After that you'll be free to call it with get_ftb();
If you just need to call the entire sidebar, use get_sidebar() instead.

Drupal front page - two columns for most recent news

I have graphical project of three-column Drupal front page which has first column for one piece of custom content and two other colums designed for most recent news (one content type) as it is shown on image below:
I am quite new to Drupal and so far I created my own page.front.tpl.php in my PHPTemplate theme with regions like header, footer, menu, search_box etc and content of course. Now I have problem with $content region, in which there should be columns as I described above. My question is: How do I style them (CSS? somewhere in Drupal's admin? use separate blocks? Some module?) to look like in project?
page.front.tpl.php fragment:
<?php if ($content): ?><div class="content-middle"><?php print $content; ?></div><?php endif; ?>
Output simplified HTML structure (so far only two most recent news), I want to style this somehow:
<div class="content-middle">
<div class="node">
<h2 class="title">
<div class="content">
<div class="node">
<h2 class="title">
<div class="content">
You can create 3 new sections in your theme. Modify your .info file (for example MyThemeName.info) and write the following :
regions[content_center_left] = Left Sidebar
regions[content_center_middle] = Middle Sidebar
regions[content_center_right] = Right Sidebar
The next step is to modify your page.tpl.php or if you want these sections to be available only in front page you can modify page-front.tpl.php. Write the following where you want the sections to be displayed:
<?php if ($content_center_left): ?>
<div class="content-left">
<?php print $content_center_left?>
</div><!-- /content_center_left -->
<?php endif; ?>
<?php if ($content_center_middle): ?>
<div class="content-middle">
<?php print $content_center_middle?>
</div><!-- /content_center_middle -->
<?php endif; ?>
<?php if ($content_center_right): ?>
<div class="content-right">
<?php print $content_center_right?>
</div><!-- /content_center_right -->
<?php endif; ?>
Now you can create your views (as blocks) and display them in these sections.
I recommend using the Panels module.
From the project page:
The Panels module allows a site administrator to create customized
layouts for multiple uses. At its core it is a drag and drop content
manager that lets you visually design a layout and place content
within that layout. Integration with other systems allows you to
create nodes that use this, landing pages that use this, and even
override system pages such as taxonomy and the node page so that you
can customize the layout of your site with very fine grained
permissions.
Agreed with Laxman 13 , Panels is definitely the way to go, you could double it with the Views module , which would allow you to retrieve the most recent news or whatever query you may have for you content display.

Resources