I am new to PHP and Wordpress plugin so I am a bit ignorant of some features. I want to write a plugin that restricts the number of posts shown on the main page (index.php). I want to use plugin because changing index.php of a certain theme would only affect that theme so I have been writing some code to actualize the concept but does not work and I want to know how I would achieve this to happen.
<?php
/*Initalize to 0 */
$numPosts = 0;
if(have_posts()) :
while($numPosts < 10) : the_post();
$numPosts = $numPosts + 1; ?>
<article class="post">
<h2> <?php the_title(); ?> </h2>
<?php the_content(); ?>
</article>
<?php endwhile;
else :
echo '<p> No Contents Found </p>';
endif;
?>
Depending on the theme will depend on what page is used to display the list of posts.
You can study and work out the used page by looking at the Template Hierarchy
If you want to make small changes to affect a current theme please look at child themes.
As for you code, at first glance it looks somewhat correct.
Related
So, i finally got my css and js scripts to load on WP
Now there is but one thing i need to get done.
i have my own theme, including header.php, footer.php, page.php
header.php and footer.php is working just fine, loading scripts and showing properly, but now i need to add all the other content.
my page.php is currently:
<?php /* Template Name: CustomPageT1 */ ?>
<?php get_header(); ?>
<?php the_content(); ?>
<?php get_footer(); ?>
I would need to somehow add html content to pages, i have about 20 ready made .php pages which needs to be transfered to WP.
Soooo how do i go about making new page (pages -> add new) and using page template while getting the html content to show up?
I've tried to just make new page and in text mode add up all the html into page, but it only shows empty page with header and footer, so the problem is most likely the page.php and i have no idea how to get it to work.
You can do look like this:
<?php /* Template Name: CustomPageT1 */ ?>
<?php get_header(); ?>
<?php
while ( have_posts() ) : the_post();
the_content();
endwhile;
?>
<?php get_footer(); ?>
You are on the good way. While developing a custom theme from scratch is a great challenge it's not too hard.
I could recommend to take it easy and follow this tutorial I found really helpful some time ago, I learned a lot there:
Developing a WordPress Theme from Scratch
You must have the official source documentation always in your mind:
Theme Development
Do some reading and you will see that making themes is really fun and gratifying :)
EDIT:
I would recommend picking a good starter theme or framework and work using child themes. You have plenty of them to pick.
To get started adding a new page to your WordPress site, find the Pages menu in the WordPress Dashboard Navigation menu. Click Add new.
The WordPress page editor looks nearly identical to the post editor, except for a few different boxes located on the right side of the screen.
Add the title of the page, like About. Note: If you have pretty permalinks set up, the title of your page will also be the URL slug.
Next, add some content.
The Publish section of the page editor is exactly the same as for writing posts. When you’re ready to publish, you can either publish immediately, save this or a draft, or schedule the page to be published later.
The Page Attributes section applies a parent page and template to your new page. For the Parent section, you can arrange your pages into hierarchies. For example, you could create this new page with additional pages under it. There are no limits to how many levels you can nest pages.
Some WordPress themes have custom page templates, so the next Template section allows you to apply a template to your new page.
The Order box allows you to order your page numerically. Pages are usually ordered alphabetically, but you can choose your own order by entering a number in this field.
Preview the page one last time, then click Publish. You’ve added a new page to your WordPress site.
This is how your
your index.php should look like :
<?php
get_header();?>
<div class="YourContainer">
<div class="Whatever">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="ContentSectionDiv">
<?php the_content();?>
</div>
<?php endwhile; ?>
<?php else: ?>
<?php endif; ?>
</div>
</div>
<?php get_footer();?>
you can make also a custom loop
<?php
$arg = array("post_type" => "services",
"posts_per_page" => 9,
"order_by" => "date",
"order" => "ASC",
);
$services = new WP_Query($arg);
if ($services->have_posts()):;
while ($services->have_posts()):$services->the_post();
the_content();
endwhile;
endif;
?>
My Wordpress websites are not using single.php to show posts on the website. Every time I open a post, it opens it in index.php.
My single.php looks like this
<?php get_header(); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<? echo the_content(); ?>
<? endwhile;
endif; ?>
<? get_footer();
?>
How can I fix this?
I had the same problem with neither the single-CUSTOM-TYPE.php nor the single.php being rendered after clicking the single-post-link.... only index.php instead of the correct file...
What helped me was a simple change back to Standard Permalinks in "Settings" -> "Permalinks" and a restore back to "Name of the Post" (Beitragsname)....
...maybe this might help someone else as well...
greetz
This happens if the LOOP is not correctly setup ensure that index.php, and single.php contains the LOOP.
The loop normally looks something like this, but will change to setup requirements.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
The Wordpress Codex site is pretty awesome and will answer most questions, check out http://codex.wordpress.org/The_Loop
Furthermore questions and discussions such as this one is more ideal if you post on stacks sister site Wordpress Stackexchange. I expect this question will be deleted or moved to https://wordpress.stackexchange.com/.
You should check your loop.php or loop-single.php weather it is routing from these files or not this are the page from where it will bring the data from database
I have created the file latest.php in the public_html so that when I go to www.domain.com/latest.php it will show me the latest articles. Sadly, nothing of the posts came up. Later, I will sort them with other ways (mostly based on custom fields).
This is my latest.php file (I removed any styling for better understanding)
<?php include("wp-load.php"); ?>
<?php get_header(); ?>
<?php wp_head(); ?>
**AND HERE IS WHAT I COPY-PASTED FROM MY INDEX.PHP THAT IS WORKING**
<?php while (have_posts()) : the_post(); ?>
<a title="" href="<?php echo get_permalink(); ?>" ><?php the_title(); ?></a>
<?php endwhile; // End the loop ?>
<?php posts_nav_link(' · ', 'previous page', 'next page'); ?>
My question is how can I make it possible to show the latest articles with pagination ? wp-load.php , wp_head and get_header are loaded correctly.
Should I use an entire different method for my task? If yes, which one?
wordpress does not works this way... if you want to make a custom page. create a new template page in the themes folder and then (from back end) create a new page and assign that template to that page.. this way you can put you custom code in the template file and wordpress can process it
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...
I'm pretty new to WordPress but have spent some 50 odd hours studying up on it, trying things out and such and have the feeling I got a pretty good handle on it now..
However the one thing I simply cannot get working is to have a page spit out a list of posts of a certain category.
Here is my example: http://dev.jannisgundermann.com/zoeikin/graphic-design/typographic-posters
I have a post that if I go to it directly works correctly, but does not show up on this page.
The post direct link.
The category id is '3' while the category name is 'typographic-posters'.
I have a custom page template for the typographic-posters page that looks like this:
<?php
/*
Template Name: Typographic Posters
*/
?>
<?php get_header(); ?>
<?php get_sidebar(); ?>
<?php if (in_category('3')): ?>
<div class="post">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<div class="post-description">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
<?=get_image('flutter-image');?>
</div>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
</div>
<?php endif; ?>
<?php get_footer(); ?>
Using this code however the page only shows gets the header, sidebar and nothing else..
If someone could help me out that would really help me get a handle on this filtering of wordpress categories.
Thanks for reading,
Jannis
in_category will only work outside of the loop on a single page. I suggest using the query_posts function to solve this problem. You may use query_posts('cat=3') or query_posts('category_name=typographic-posters') to get the posts you are looking for.
Once obtained, just use the normal WordPress loop to access these posts.
The easiest way is to create a file called category-3.php and use the standard code from normal index.php or category.php file. Wordpress will take care of fetching posts only from category with id=3 and it's child categories.
in_category will only work outside of the loop on a single page. I
suggest using the query_posts function to solve this problem. You may
use query_posts('cat=3') or
query_posts('category_name=typographic-posters') to get the posts you
are looking for.
Once obtained, just use the normal WordPress loop to access these
posts.
This worked excellent, but make sure that you go into Settings > Reading and set the posts page to the -- Select -- option or it will override this query and dump all recent posts there regardless of category.
Simply add before the loop:
<?php query_posts="cat=3&showposts=5">
This will force the loop to display 5 posts (showposts=5) from category 3 (cat=3).
I would 2nd Eimantas' suggestion. The Template Hierarchy will use the category-3.php to display posts in that category. Usually you can just copy a theme's index.php or category.php to category-3.php and adjust that template for any customization you need. Plus the category template will better support pagination of posts.
But if you need to stick with a Page to display those posts, also see the Page of Posts example.
http://codex.wordpress.org/Template_Tags/query_posts
Just so you know where these answers are coming from...there are a lot more interesting functions you can do with query_posts as well.
This plugin could also help you if you want to be able to change the displayed categories without going through the code :
http://wordpress.org/extend/plugins/advanced-category-excluder/
I have filtered post by category Id using the method below:
query_posts('cat=1&showposts=3');
if (have_posts()) : while (have_posts()) :
// if(1) {
//echo the_category_ID();
the_post();
/**
* The default post formatting from the post.php template file will be used.
* If you want to customize the post formatting for your homepage:
*
* - Create a new file: post-homepage.php
* - Copy/Paste the content of post.php to post-homepage.php
* - Edit and customize the post-homepage.php file for your needs.
*
* Learn more about the get_template_part() function: http://codex.wordpress.org/Function_Reference/get_template_part
*/
$is_post_wrap++;
if($is_post_wrap == '1') {
?><div class="post-wrap clearfix"><?php
}
get_template_part('post', 'homepage');
if($is_post_wrap == '3') {
$is_post_wrap = 0;
?></div><?php
}
endwhile;
else :
get_template_part('post', 'noresults');
endif;
thank you for sharing on your thought its a great thought. Usually you can just copy a theme's index.php or category.php to category-3.php and adjust that template for any customization you need