• Hi.
    I’m trying to make a page template which lists every posts made for a category. The category will contain hundreds of posts, so I want to divide them into columns. Let’s say I have 300 posts and wants to divide them into 3 columns, using table.

    Left column – 1~99
    Center column – 100~199
    Right column – 200~300

    Is it possible to make this happen using get_posts?

    Thank you!

Viewing 9 replies - 1 through 9 (of 9 total)
  • Thread Starter code012

    (@code012)

    Yes I’ve tried it but it’s not something I’m looking for. My page templates needs extensive customizations for each category. Thank you for the suggestions though.

    Any other suggestions?

    Have you tried looking at theme options? What theme are you using now?

    Thread Starter code012

    (@code012)

    It’s not the theme problem or issue. The theme works well as intended.
    I’m just trying to make custom page templates for each category, listing every posts in the past in one page. I can actually do this by get_posts but it just lists top to bottom. I was wondering if I can divide the listed items into columns or separate sections..

    This is totally Un-Tested so you need a local install for any fatal errors!!

    The style.css, adjust the widths and add any padding or margins

    /* Start Column Panel */
    .cms-columns
    {
    	position: relative;
    	margin: 0;
    	padding: 0;
    	float: left;
    	overflow: hidden;
    	width: 960px;
    	height: auto;
    }
    /* Our Column */
    .cms-columns .cms-column{
    	position: relative;
    	float: left;
    	margin: 0;
    	padding: 0;
    	overflow: hidden;
    	width: 320px;
    }
    /* End Column Panel */

    The Incomplete Sample Code, Un-Tested, based on ‘twenty ten’
    https://digitalraindrops.pastebin.com/VUkd9AkN

    What the code does, it looks for a category slug with the same name as the page, lets say news and news, then get the total and calculate how many per column.

    //Get the total Count of posts
    $postotal=(int) get_category($catid)->category_count;
    $query= 'cat=' . $catid. ''; query_posts($query);
    //Round up to the Nearest Whole Number
    $postcount = ceil($postotal/3);

    Lets say you have 97 posts the post count = 32, 32, 33 posts in columns, you might want to do something different, sample from inside the loop

    <?php $counter+=1; ?>
    <?php if( ($counter !=  $postotal) && !($counter % $postcount) ) : // Remainder = 0 ?>
    </div> <!-- Close Open Column -->
    <div class="cms-column"> <!-- Add Column -->
    <?php endif; ?>

    Every time the counter divided by 33 returns no remainder 33 and 66, the column is closed and a new one opened.

    HTH

    I would asked that if you use the code then you update the pastebin with the solution.

    Or maybe you could use a plugin if there is one, code is more intuitive and fun!

    Phew!

    David

    Thread Starter code012

    (@code012)

    Holy crap, adeptris! Thank you for the very in-depth explanation.
    I was looking for something simple out of get_posts with extra strings. For example:

    get_posts(‘numberposts=100&somethingfrompost1to99&&category=1’)

    I’m not too good with codes, so I don’t fully understand your suggestion but it definitely looks like a solution. Thank you again for your input! ??

    Thread Starter code012

    (@code012)

    adeptris, I have a question.
    Instead of same name as the page, is there way I simply do it with category ID?

    Thread Starter code012

    (@code012)

    OMG I can’t believe how simple the answer was. I could’ve just use offset parameter.
    Sorry for all the troubles guys. I’m very new at coding. ??

    I did think about offset but was torn between the number of database queries, get 99 posts and set the columns in the loop, or three offset queries?

    One Query Return 99 posts.

    $myposts = get_posts('numberposts=99&category_name='.$catname.'');

    Offset
    Query 1 Get first 33 entries

    $myposts = get_posts('numberposts=33&category_name='.$catname.'');

    Query 2 Get second 33 entries (loops 66 records)

    $myposts = get_posts('numberposts=33&offset=33&category_name='.$catname.'');

    Query 3 Get third 33 entries (loops 99 records)

    $myposts = get_posts('numberposts=33&offset=66&category_name='.$catname.'');

    Or can you return 99 records and then offset the returned recordset, what happens if you have less that 99 posts say 75??

    Not sure which is better for page speed, as wordpress has to visit the database to return the datasets, the speed of the calls could be different in test and production websites, not all websites return quick data.

    When working with other databases I try to limit the database queries by returning a single dataset.

    How is the page load time difference, between the single call for 100 posts, and the three calls for the smaller sets?

    David

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘get_posts – divide into several columns?’ is closed to new replies.