Noob Question: Wordpress Looping - wordpress

Can someone give me an essential Wordpress Loop and explain to me what's happening with it? I'd like to put together some templates, but I don't do well with blackboxing. In other words, I'm fully capable of writing my own CMS, but when it comes to using someone else's and its arbitrary rules, I'm completely at a loss, and I just can't get my head around the standard loop Wordpress uses. Thanks for your patient guidance.

There is a really nice tutorial from SpoonGraphics describing how to build your own theme for wordpress and explaining the required coding. It also covers the standard wordpress loop and explains how to build around it:
http://www.blog.spoongraphics.co.uk/tutorials/how-to-build-a-custom-wordpress-theme-from-scratch

You might want to start with reading the codex article on the The_Loop. Beyond reviewing their loop documentation, reading up on the various template tags should be helpful to you as well.
Begins the loop:
<?php if ( $posts ) : foreach ( $posts as $post ) : start_wp(); ?>
Generates a div container marked up with a post-specific id and post_class adds additionally CSS classes depending on the categories/tags/etc of the post.
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
Displays the post title as a permalink to the post.
<h2><?php the_title(); ?></h2
Displays some the post's publish date with a link to other posts by the same author.
<small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>
Pulls the full contents of the post, wrapped in a div container to ease CSS styling.
Closes the loop, and displays a message if no posts were found.
<?php endforeach; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>

Related

Wordpress showing post info under new pages, but I want it to show only under new posts

I have basic loop to show PHP posts
if (have_posts()) :
while (have_posts()) : the_post(); ?>
<div class="post">
<h2><?php the_title(); ?></h2>
<div class="post-info"><?php echo get_the_date(); ?> - posted by <?php echo get_the_author(); ?> </a></div>
<?php the_content('<button class="show-more">Saznajte više</button>'); ?>
</div>
<?php
endwhile;
else :
echo "<p>No posts</p>";
endif;
It is the line that has <div class="post-info...
Now it is working very well, it shows date and author under post heading, but it also shows that when a new page is created. I understand Wordpress works like blog and it is natural for it to treat new pages as posts, however I want to avoid that behavior. I only want it to be shown under post headings, but not under new page headings. What would you suggest me?
https://developer.wordpress.org/themes/basics/template-hierarchy/
Check this template hierarchy and you'll understand how WordPress call template files.
Basically, WordPress firstly runs index.php and then call different template files. Since you only have index.php, so when rendering pages it all also falls back to index.php.
What you can do is creating a page.php with its own design for WordPress to call properly.

restrict viewing photos and article content on a wordpress site

I have a question, and i hope i am posting in the right place, if this topic belongs to another forum, please guide me where to post it.
the question is: i have a website created with WordPress and i am using the Jupiter theme, i need to hide some content (like hiding the last half of the article in a page) and disable the photos to be enlarged unless the visitor is a registered user, i need to know how to do that, and if there is a plugin to do that, i have tried "layered-popups-for-wordpress" and "optin-content-locker-layered-popups-addon" but they didn't work properly.
It might request a lot of effort for you to edit the theme on your own...You can use the_excerpt() insetead of the_content() for display info, and add a rule that only registered members can see, something like
if(is_user_logged_in()) {
the_content();
} else {
the_excerpt();
}
Do this while in the loop, of course...
These plugins might help you though
https://wordpress.org/plugins/tags/paid-content
If you're using WPMU might try this one
https://premium.wpmudev.org/project/pay-per-view/
I don't know of a plugin that does this automatically, but you can do it yourself if you are willing/able to do some minor theme development. Make a child theme of your Jupiter theme, and copy over the file content.php. There will probably be some part of the code that looks like this:
<?php if ( is_search() ) : ?>
<?php the_excerpt(); ?>
<?php else : ?>
...
Or something like that. The point is, the theme should already be set up to serve excerpted content for a search. You could either simply add code like this:
<?php if ( is_search() || !is_user_logged_in() ) : ?>
<?php the_excerpt(); ?>
<?php else : ?>
...
Or you could customize what non logged in users are seeing like this:
<?php if ( is_search() ) : ?>
<?php the_excerpt(); ?>
<?php else if (!is_user_logged_in()) : ?>
<!-- Your custom code display here -->
<?php endif; ?>
Hope that's helpful!
In order to hide certain parts of your content with just a simple shortcode you may give "Restrict Anonymous Access" plugin a try:
https://wordpress.org/plugins/restrict-anonymous-access/
Examples:
[member]My restricted content …[/member]
This shortcode can be placed wherever you need to hide something from logged-out users or even users of a certain user role can be addressed:
[member role="author"]My restricted content to users below author role …[/member]

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

Trouble with paginating an archive page in wordpress

Im trying to make an archive page in wordpress that works. Im trying to have the archive page show all posts in a list, but i dont want a list that is 100 list items long. So im trying to figure out how to get it to make a second, third, fourth, etc... page after about 20 to 30 list items on one page.
I dont mind using the wp-paginate plug in, but im still not sure how to get that to work with wp_get_archives()
I dont think it matters, but i will have a search bar on the page and its through google custom search. So i didnt supply it, as i dont think it contributes to the actual issue.
Any help would be greatly appreciated!
<?php
/*
Template Name: Archives
*/
get_header(); ?>
<div id="main">
<div id="posts">
<?php the_post(); ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
<ul>
<?php wp_get_archives( 'type=postbypost' ); ?>
</ul>
<?php wp_link_pages(); ?>
<?php rewind_posts(); ?>
</div>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
For pagination, I always use Eric Martin's emm_paginate function.
http://www.ericmmartin.com/pagination-function-for-wordpress/
In regards to your archive page, why not just use Wordpress's built in Archive page.
http://codex.wordpress.org/Creating_an_Archive_Index
With those two links, you should be able to build an Archive page that has the pagination you desire.

Wordpress - show posts from a particular category in sidebar "recent posts"

how can i make the "recent posts" in the WP sidebar show posts from a particular category only?
Probably easiest to make your own recent posts widget with one of Otto's php code widgets http://wordpress.org/extend/plugins/php-code-widget/ and a new query:
<?php $my_query = new WP_Query('category_name=mycategory&showposts=1'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<?php the_title(); ?></a>
<?php endwhile; ?>
Style it any way you want. The new query loop won't conflict with the main WP loop and can be used any numbers of time.
Or if you prefer something a little more user-friendly(doesn't require posting code into the widget), you may find the following plugin of use.
http://wordpress.org/extend/plugins/query-posts/
Nice little plugin that one, written by Justin Tadlock.
Just #FYI on both previous answers here:
'Otto's php code widgets' was last updated more than two years ago and has not been tested on the last 3 major Wordpress releases. Justin Tadlock's plugin also hasn't been tested on the last 3 major Wordpress releases.
At time of writing this update the recommended plugin is 'Category Posts Widget' by TipTopCopy, which was last updated approximately 2 weeks ago.

Resources