Solved

Wordpress Custom Fields - Auto Pagination?

Posted on 2011-03-12
24
1,151 Views
Last Modified: 2013-11-19
Hi there!

I'm creating a custom Wordpress theme for a client. On one of the pages in the site, I plan on using custom fields to allow my client to easily and continuously add documents (i.e. PDFs) to the page. However, over time, I can see this page getting to be very long.

Along with the code that I'll need to add to the page's template to allow the custom field information to display correctly, is there a way that I can have the page automatically paginate after a certain number of documents have been added?

Any help would be greatly appreciated. Thanks.
0
Comment
Question by:Adam
  • 10
  • 6
  • 2
  • +2
24 Comments
 
LVL 12

Expert Comment

by:Avinash Zala
ID: 35127661
Use below quick tag to devide the page content in separate pages,
<!--nextpage-->

Open in new window


Hope this helps
Avi
0
 
LVL 1

Author Comment

by:Adam
ID: 35127731
Thanks for the reply AddyWatson, but you may not have read my question correctly.

What I was asking for was how I can get the page to automatically paginate once the number of documents added through custom fields reaches a particular number.

By placing "<!--nextpage-->" at the end of each custom field entry, there will be a new page for each document added. I was thinking more like adding pagination after every 10 documents though.

I'm not that strong of a developer and was looking for some assistance on how to develop this functionality.
0
 
LVL 31

Expert Comment

by:gwkg
ID: 35144021
How are you with jQuery?  That would be the easiest way to go.

This plugin will allow you to paginate a table by rows <td>

http://www.devlegion.com/?page_id=15

You can see it's simple html and only a line of javascript.
0
 
LVL 1

Author Comment

by:Adam
ID: 35147082
Thanks for the reply gwkg.

However, to be honest, I was kind of hoping to avoid using a plugin to do this. I know there has to be a way to pull this off within Wordpress with some simple code and using the "<!--nextpage-->" shortcode that AddyWatson suggested earlier. I just  haven't been able to figure it out.

I appreciate the help anyway but am going to hold out for another solution.
0
 
LVL 31

Expert Comment

by:gwkg
ID: 35173898
FYI: The plugin is a JQuery plugin, not a Wordpress plugin.  It's just an extra javascript file.

http://plugins.jquery.com/project/paginateTable

0
 
LVL 1

Author Comment

by:Adam
ID: 35177400
Thanks gwkg. I realize that its not a Wordpress plugin. But as mentioned, I was hoping to not have to use a plugin to do this (Wordpress or not). I already have a handful of JQuery plugins being used on the site and would prefer not to add another.

I was hoping to figure out a way to do this using a combination of Wordpress codex and some PHP. I'm almost positive something can be done using the Wordpress codex, I just can't figure it out.
0
 
LVL 23

Expert Comment

by:jeremyjared74
ID: 35210239
Maybe you have a good reason for doing it the way you are, but to me it seems to be "re-inventing the wheel", and making it square. WordPress does this automatically with posts. If you created a page for the posts you could set a limit of 10 and it would auto-paginate. Is this not an option?
0
 
LVL 1

Author Comment

by:Adam
ID: 35214331
Thanks for the reply Jeremy. I'm not necessarily trying to reinvent the wheel. I basically want what you are describing but without mixing up the documents with my posts (hence the custom fields). Is there no way to set this type of functionality on a page without having to use posts?
0
 
LVL 14

Expert Comment

by:tomaugerdotcom
ID: 35233783
Hey furio, just want to be clear on what you're trying to accomplish. Here's my understanding:

- you want a single post to have multiple attachments associated with it
- you want these attachments listed after the body of the post
- you want to provide pagination automatically once the number of attachments exceeds an arbitrary limit
- the pagination will still display the same post content, but just different attachments.

The last point is a sticking issue, and certainly a jQuery solution would be best for this kind of many-to-one relationship. However, you are stating that you wish to do this through the theme.

There is no built-in way to do this. To accomplish this you will need to use PHP and write a custom query / custom loop on your page template (single.php if it's going to be for every page).

Here's an outline of the steps. If you need specific implementation help, let us know.

- create a secondary loop that queries the database, iterating over attachments that are associated with the post ID.
- use the LIMIT SQL statement to select a subset of that range, as well as the COUNT statement to count totals (so you can decide whether to display pagination or not)
- provide the pagination HTML, and have it link to the same permalink, but add an additional query variable that indicates the pagination within this post page (ie: index.php?p=5&page=2)
- pass this pagination value to your SQL query, so you can extract the appropriate records from the database
- use a custom loop to build your list of attachments.

Hope this all makes sense.

Tom
0
 
LVL 1

Author Comment

by:Adam
ID: 35234122
Hi Tom,

Thanks for the reply. I appreciate all the help.

Just to clarify one thing though - what I'm looking to do has nothing to do with posts. I plan on using custom fields on a page to allow me to add files to the page on a continual basis. So each time I want to add a new file, I open up the page in the admin, add a new custom field with the necessary details, and it will get populated automatically on that page. I apologize if I didn't make that clear from the get-go.

I kind of figured that Wordpress didn't have anything necessarily built in to pull this off and assumed I would need to use a little bit of PHP to get this working properly. I know that Wordpress has the shortcode "<!--nextpage-->" which will allow you to put "breaks" on a page and allow them to span more then one page. What I'm looking to do is have some sort of function that will count a particular number of custom fields that have been added to the page and once it reaches a certain number, it will then break the page and automatically add the "<!--nextpage-->" shortcode. So in the future, I can continue to add files to my page and never have to worry about the page getting too long.

Now, I didn't want to further complicate things but this question seems to be a much more difficult issue then I had anticipated (or at least it appears to be) so I might as well lay it all out there. I am actually using a plugin called "Magic Fields" which essentially takes the functionality of the custom fields and provides a lot more customized options (its a great tool for building a CMS). You can check out the details on the plugin here: http://magicfields.org/

Having said that, I have added a question to the Magic Fields Google groups. If it helps, take a look at my question here. I ended up getting a bit of help from someone and even posted some code for reference. I just can't get it to function exactly how I want.

I'm not sure if I made things more complicated by bringing that up but I figured I might as well.

Anyway, if I need to clarify anything, please let me know. I'm desperately trying to get this to work so I can use all the help I can get. Thanks.

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 14

Expert Comment

by:tomaugerdotcom
ID: 35236381
Hi furio, thanks for the clarification. It's actually a good thing you mentioned Magic Fields, because that changes things from the way I was thinking you had structured your blog.

I probably would have gone the custom post / custom taxonomy route with the documents as attachments, and that was how I had approached answering your question.

That having been said, unfortunately I have no experience with Magic fields (though it looks interesting!). I did come across this tutorial that's quite detailed. Now it doesn't cover documents, but it certainly does everything else, including paging. I hope that with just a little ingenuity, you can apply the principles here to your problem.

http://www.strategy6.com/creating-a-multi-day-event-calendar-in-wordpress-using-the-magic-fields-plugin/

Let me know if this is of any help.

Tom
0
 
LVL 1

Author Comment

by:Adam
ID: 35257712
Thanks for the reply Tom. I apologize for not bringing up Magic Fields earlier as like you said, it does change things a little.

I appreciate the tutorial you provided but unfortunately, it doesn't really help with my problem. The tutorial provided is using "posts" to create events. I on the other hand am using a page where items will continually be added to it. The problem I was having is that I wanted to create some sort of way to paginate those items on that one page. I know its relatively easy to do with "posts", but wanted to figure out a way to do it with pages (I'm trying to avoid mixing the items with posts).

Anyway, if I don't get any more replies for this question in the next little bit, I may just delete this question and start a new one (and try to make my question a little more clear).

Thanks again for the help.
0
 
LVL 14

Expert Comment

by:tomaugerdotcom
ID: 35259388
Posts or pages, it shouldn't make a ton of difference - these both use the exact same database structure. they're just different post types.

I would actually have used neither - I would have created a custom post type. That would have created a new entry (underneath the "Pages" section in your Admin panel).

Since you've piqued my curiosity, I'm going to create a sandbox and try to implement what you are doing.

Would you feel comfortable providing me with the exact details of what you're trying to accomplish (ie the nature of your "pages" and the nature of the "attachments" that you're adding) so I can reproduce it exactly?

T
0
 
LVL 1

Author Comment

by:Adam
ID: 35259927
Wow....that would be fantastic Tom!

To make things easier, I'll provide a screenshot to show you what I'm looking for. Essentially the page is going to be dedicated to showing publications. The grey box will be a thumbnail image, then followed by title (just plain text - not an active link), small description, and a download link (the download link will link to either images, PDFs, etc - so not always the same thing). In essence, its going to look like a mini blogroll.

After playing around with this for a bit longer, I have boiled it down to two options I can take to pull off what I'm looking for using Magic Fields. Both options will involve creating a custom page template in my theme to list these publications.

Option 1 - Use Posts
Using Magic Fields, I can set up a custom write panel for the publications and for each new publication, I create a new post (so, similar to what the guy in the tutorial you provided me with did with events). I can then simply create a "Publication" category and use Magic Fields and a query post to display the items on my page. The problem with this, if I only want these posts to appear on my custom page, I will need to do a bit of hacking to make sure the "Publication" post category doesn't appear anywhere else on the site (i.e. blogroll, widgets, etc). The advantage to using posts though is that it will easily allow me to add pagination.

If I took this route, here is a very simplified version of the code I came up (without any elements for styling):
<?php
$my_query = new WP_Query( array( 'category_name' => 'publications', 'paged' => get_query_var('paged') ) );

while ( $my_query->have_posts() ) : $my_query->the_post();
	echo '<img src="' . get('publication_thumb') . '" alt="" />';
	echo '<h3>' .get('publication_title') . '</h3>';
	echo '<p>' . get('publication_description') . '</p>';
	echo '<p><a href="' . get('publication_download') . '">Download</a></p>';
endwhile;
?>

Open in new window


Option 2 - Use Duplicate Groups on a Page
With Magic Fields, I can create a custom write panel that will allow me to create a group with all the necessary fields to display each publication on my page. I can then "duplicate" this group each time I want to add a new publication. The problem with this method though, there is no simple way to split up my page if the publications being added get to be too long (i.e. in a few years, there could potentially be hundreds of publications on the one page). So adding pagination seems to posing a problem.

I managed to get this option to work, but as mentioned, without pagination. Here is the code I used on my custom page (simplified):

<?php
$Publications = get_group("Publication");
if($Publications){
   foreach($Publications as $Publication){
    	echo '<img src="' . $Publication['publication_thumb'][1]["t"] .'" alt="" />';
   		echo '<h3>' .$Publication['publication_title'][1]. '</h3>';
        echo '<p>' . $Publication['publication_description'][1]. '</p>';
        echo '<p><a href="' . $Publication['publication_download'][1].'>Download</a></p>';
	}
} 
?>

Open in new window



If you've had a chance to play around with the Magic Fields plugin, then hopefully those options should make some sense. If things are still not clear, please let me know and I'll attempt to clarify.

Thanks so much Tom. If you can help me come up with a solution, you'll be a real life saver!

P.S. Sorry for the mini essay. Can you tell I've been racking my brain over this for some time now? ;)


Sample set up
0
 
LVL 1

Author Comment

by:Adam
ID: 35263574
Hi Tom,

I realized after posting my mammoth of a comment, that I may have frightened you (I know I would be). If what I wrote completely confused you, please disregard it.

If you know how to achieve essentially what I'm looking for in the screenshot provided using Magic Fields, then by all means, take whichever approach you feel works best. I provided the sample code and all my other comments to kind of give you a starting point, but it may have just made things more complicated.

Anyway, if you need anything else from me, just holler! Thanks again.
0
 
LVL 14

Expert Comment

by:tomaugerdotcom
ID: 35264813
thanks furio for all the information. I'll try to have an answer for you by tomorrow morning (EST).

T
0
 
LVL 14

Accepted Solution

by:
tomaugerdotcom earned 500 total points
ID: 35271563
Okay, so I did some sniffing about with Magic Fields and I have to say, I don't like it. But maybe I'm old-fashioned.

Nonetheless, in the interests of expedience, I tried to find the best solution given Magic Panels. I must admit I do like their file uploading function - that might be the only reason to go with them. But I digress.

Your option 1 is by far the better approach, because it works more "within the system", that is, the way WP is supposed to work. The other method seems a lot more "hackish" and even though you'll be doing the back end updates yourself, duplicating groups just to iterate through multiple instances of a record type just makes me shiver.

So we're left with creating a Post type Write Panel, hiding the normal stuff and exposing your custom fields, as you have suggested. To make life easier, we create a category called "Documents" or perhaps as I did "Magic Fields", which you could potentially apply to other custom write panels that you might want to create in the future. Then, when you create your Write Panel, make sure it defaults to this category.

The secret is how do you exclude these categories from your main loop. The solution is quite straightforward. All you need to do is figure out what the ID of your category. You can do this by just clicking on your category in the Admin panel, and looking at the URL - it will say tagID=nn where nn is the ID # of your category.

Now, open up index.php inside your theme's folder (wp-content/themes/mythemename). I tried it with the default TwentyTen theme. Add this section pretty near he top, certainly above the get_header() call:

if ( is_home() ) {
	query_posts( 'cat=-6' );
}

Open in new window


That's it, believe it or not. We're telling the Loop to exclude posts that belong to our Write Panel category (mine turned out to be ID 6)

For your reference, here's the full text of my index.php file:



<?php
/**
 * The main template file.
 *
 * This is the most generic template file in a WordPress theme
 * and one of the two required files for a theme (the other being style.css).
 * It is used to display a page when nothing more specific matches a query.
 * E.g., it puts together the home page when no home.php file exists.
 * Learn more: http://codex.wordpress.org/Template_Hierarchy
 *
 * @package WordPress
 * @subpackage Twenty_Ten
 * @since Twenty Ten 1.0
 */
 

if ( is_home() ) {
	query_posts( 'cat=-6' );
}


get_header(); ?>

		<div id="container">
			<div id="content" role="main">

			<?php
			/* Run the loop to output the posts.
			 * If you want to overload this in a child theme then include a file
			 * called loop-index.php and that will be used instead.
			 */
			 get_template_part( 'loop', 'index' );
			?>
			</div><!-- #content -->
		</div><!-- #container -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

Open in new window


Hope this solution works for you. It's the most elegant, because you're going to use WordPress' extensive archive functionality which supports paging and all that good stuff. Even though you're not really fully leveraging WordPress features (like attachments and the media center upload) this solution requires the least amount of hard coding.

I'll have to implement a document uploader myself soon, and I will be creating it using custom post types and attachments, unless I get really hung up. If I make any significant discoveries, I'll be sure to post them to the end of this EE question so you get an update. It will be a while though, so for now, Magic Fields is the way to go!

T
0
 
LVL 1

Author Comment

by:Adam
ID: 35278531
Thank you so much Tom for looking into this. I know I keep saying it but I really do appreciate the help.

I looked over your solution and I think taking the "post" route would be best. However, there is one snag with your solution. Although the posts for the "Document" category will no longer show in the blogroll, they will still appear in a number of other places (i.e. Recent Posts widget, Categories widget, Archives, Search results, etc.). That was one of the main reasons why I wasn't crazy about taking the post route - it seems like it would be just as "hackish" to try and hide that category from all those different areas on the site.

I know that there are some plugins that I can use to help exclude the category from other areas, I was just hoping there was a "better way". I prefer to not use plugins if I don't have to. But worst case scenario, I'll resort to one.

On a side-note, if you do end up coming up with a solution to create custom write panels, even if its further down the line, I would definitely be interested! This won't be my last Wordpress site so I'm always trying to find better approaches to doing things.

Out of curiosity, what exactly do you not like about the Magic Fields plugin? Not trying to defend it or anything, but considering what can be done with it, I thought it was a pretty decent one. Anyway, I'm curious to hear your thoughts.

I think that's about it for now. If you have any thoughts about excluding the "Documents" category from other areas of the site, that would be great. If not, I know I still have a few options.

Thanks again Tom.
0
 
LVL 14

Expert Comment

by:tomaugerdotcom
ID: 35278983
Hey furio, well the issue with Magic Fields (as it is now - they will probably address this at some future point) is that it is trying to do something that WordPress already does, but by hacking around instead of using the WordPress way. This can only lead to incompatibility issues when you upgrade WordPress in the future, and limits how you can take advantage of WordPress' other features in synergy with your custom fields.

WordPress features custom post types, which does exactly the same thing as Magic Fields, in terms of putting a new type of post up in your left-hand admin menu. What I do like about Magic Fields is how it then makes it so easy to add custom fields ("post meta") to these new custom post types.

The thing with custom post types is that you don't run into the problem that we have now. Custom post types will NOT appear by default in your sidebars, search results (if you don't want them to), widgets etc.

The disadvantage with going the manual route of creating custom post types is that you do have to also manually define the custom fields (though there are doubtless plugins that will help with this). And there's no easy way to add a file upload, which is a real bummer, especially in your case.

This is where "attachments" comes into play. You can upload an attachment (using the Media menu item in your admin panel) and associate it with your custom post. One attachment per post. Then you use a custom archive template for that custom post type to display all your posts (and their atachments), taking full advantage of pagination.

T
0
 
LVL 1

Author Closing Comment

by:Adam
ID: 35299499
Thanks again Tom for all your help and little factoids about Wordpress. Extremely helpful.

As mentioned at the end of my last message, my only concern with your solution is that the category will only be removed from the blogroll, and not from anywhere else on the site.

Unfortunately, due to time constraints, I was forced to use a plugin to remove the category from my site (except for on my custom page). I was hoping to do it without one but it's fine in the meantime.

If you do end up coming up with a solution for this, I'll definitely still be interested.

Thanks again. Cheers!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now