Viewing 12 replies - 31 through 42 (of 42 total)
  • Thread Starter jimmyt1988

    (@jimmyt1988)

    Here is the latest version of the site:
    https://wordpress.pastebin.com/kJjFB6SE

    Here is the site:
    https://www.jamestrusler.co.uk

    Alright, to select only posts from a given category, your sql needs to look like this:

    $cat_name = 'blog';
    $sql = "SELECT p.* FROM $wpdb->posts p
    JOIN $wpdb->term_relationships tr
       ON (tr.object_id = p.ID)
    JOIN $wpdb->term_taxonomy tt
       ON (tt.term_taxonomy_id = tr.term_taxonomy_id AND tt.taxonomy = 'category')
    JOIN $wpdb->terms t
       ON (t.term_id = tt.term_id AND t.name = '$cat_name')
    WHERE p.post_type = 'post'
       AND p.post_status = 'publish'
    ORDER BY p.post_title ASC";

    Please try this and see if it works to select only the ‘blog’ posts.

    The tricky part is knowing which category name to assign to $cat_name.

    Do you have a way to do that?

    Thread Starter jimmyt1988

    (@jimmyt1988)

    <?php
                                $cat_name = 'Blog';
                                $sql = "SELECT p.* FROM $wpdb->posts p
                                JOIN $wpdb->term_relationships tr
                                   ON (tr.object_id = p.ID)
                                JOIN $wpdb->term_taxonomy tt
                                   ON (tt.term_taxonomy_id = tr.term_taxonomy_id AND tt.taxonomy = 'category')
                                JOIN $wpdb->terms t
                                   ON (t.term_id = tt.term_id AND t.name = '$cat_name')
                                WHERE p.post_type = 'post'
                                   AND p.post_status = 'publish'
                                ORDER BY p.post_title ASC";
    
                                if ($mypages) :
                                   $limit = 3;  // The number of posts per page
                                   $range = 5;   // The number of page links to show in the middle
                                   $mypage = (isset($_GET['mypage'])) ? $mypage = $_GET['mypage'] : 1;
                                   $start = ($mypage - 1) * $limit;
                                   for ($i=$start;$i<($start + $limit);++$i) {
                                      if ($i < sizeof($mypages)) {
                                        // Process each element of the result array here
                                        $post = $mypages[$i];
                                        setup_postdata($post);?>
                                        <div class = "leftSideWrap">
                                            <h2><?php the_title(); echo " <small>"; the_time(get_option('date_format')); ?></small></h2>  
    
                                            <div class = "top"></div>
                                            <div class = "middle">
                                                <div class = "cornerImage"></div>
                                                <?php the_content(); ?>
                                                <div class = "commentSeperateLine dotted main"></div><br />
                                                <p>
                                                    <a href="<?php comments_link(); ?>">
                                                        Add / View Comments to this post</a> -
                                                         <?php comments_number('There are currently no comments', 'There is 1 comment', 'There are % Responses' );?>
    
                                                </p>
                                            </div>
                                            <div class = "bottom"></div>
                                        </div>
                                      <?php
                                      }
                                   }
                                   echo _mam_paginate(sizeof($mypages),$limit,$range);
                                else:
                                   echo '<h2>Sorry, There are no Pages to list</h2>';
                                endif;?>
                                </div>

    I have 4 categories:

    Blog: name: Blog, slug: blog, id: 3
    Music: name: Music, slug: music, id: 4
    Poetry: name: Poetry, slug: poetry, id: 5
    Pictures: name: Pictures, slug: pictures, id: 6

    I could do a for loop through it and use my array at top of page. But that code above is returning “Sorry, There are no Pages to list” so if that works i can figure out how to cycle through my pages.

    Looks like you left out this line: $mypages = $wpdb->get_results($sql);

    Now, if you are using the same template for all, you need a way to select the single one you want based on which tab you click.

    We will work on that next.

    Once you get it working with $cat_name = 'Blog';, replace that line with the following:

    $request = $_SERVER['REQUEST_URI'];
    if (strpos($request,'/music/')) {
       $cat_name = 'Music';
    } elseif (strpos($request,'/poetry/')) {
       $cat_name = 'Poetry';
    } elseif (strpos($request,'/pictures/')) {
       $cat_name = 'Pictures';
    } else {
       $cat_name = 'Blog';
    }
    Thread Starter jimmyt1988

    (@jimmyt1988)

    I can’t manage to get that part to work.

    <?php
                                $request = $_SERVER['REQUEST_URI'];
                                if (strpos($request,'/music/')) {
                                   $cat_name = 'Music';
                                } elseif (strpos($request,'/poetry/')) {
                                   $cat_name = 'Poetry';
                                } elseif (strpos($request,'/pictures/')) {
                                   $cat_name = 'Pictures';
                                } else {
                                   $cat_name = 'Blog';
                                }
                                $sql = "SELECT p.* FROM $wpdb->posts p
                                JOIN $wpdb->term_relationships tr
                                   ON (tr.object_id = p.ID)
                                JOIN $wpdb->term_taxonomy tt
                                   ON (tt.term_taxonomy_id = tr.term_taxonomy_id AND tt.taxonomy = 'category')
                                JOIN $wpdb->terms t
                                   ON (t.term_id = tt.term_id AND t.name = '$cat_name')
                                WHERE p.post_type = 'post'
                                   AND p.post_status = 'publish'
                                ORDER BY p.post_title ASC";
    
                                $mypages = $wpdb->get_results($sql);
                                if ($mypages) :
                                   $limit = 3;  // The number of posts per page
                                   $range = 5;   // The number of page links to show in the middle
                                   $mypage = (isset($_GET['mypage'])) ? $mypage = $_GET['mypage'] : 1;
                                   $start = ($mypage - 1) * $limit;
                                   for ($i=$start;$i<($start + $limit);++$i) {
                                      if ($i < sizeof($mypages)) {
                                        // Process each element of the result array here
                                        $post = $mypages[$i];
                                        setup_postdata($post);?>
                                        <div class = "leftSideWrap">
                                            <h2><?php the_title(); echo " <small>"; the_time(get_option('date_format')); ?></small></h2>  
    
                                            <div class = "top"></div>
                                            <div class = "middle">
                                                <div class = "cornerImage"></div>
                                                <?php the_content(); ?>
                                                <div class = "commentSeperateLine dotted main"></div><br />
                                                <p>
                                                    <a href="<?php comments_link(); ?>">
                                                        Add / View Comments to this post</a> -
                                                         <?php comments_number('There are currently no comments', 'There is 1 comment', 'There are % Responses' );?>
    
                                                </p>
                                            </div>
                                            <div class = "bottom"></div>
                                        </div>
                                      <?php
                                      }
                                   }
                                   echo _mam_paginate(sizeof($mypages),$limit,$range);
                                else:
                                   echo '<h2>Sorry, There are no Pages to list</h2>';
                                endif;?>
                                </div>
    Thread Starter jimmyt1988

    (@jimmyt1988)

    Also, my contact page and confirmation page no longer work because the original get or query posts has gone. Please refer to https://www.jamestrusler.co.uk/blog from now on, I had to put a underconstruction page up as visitors were getting lost (i love GAnalytics)

    OK. I need to see what the REQUEST_URI looks like. Please change this:

    echo _mam_paginate(sizeof($mypages),$limit,$range);

    to this:

    echo _mam_paginate(sizeof($mypages),$limit,$range);
    echo "<p>REQUEST:$request</p>";
    Thread Starter jimmyt1988

    (@jimmyt1988)

    Firstly, I got the pages listing correctly.

    Instead of $request,/pagename/, it needed to be $request,pagename.

    I’m sure i can sort the other if loops out.

    Last question I guess is how do i list things via post date rather than post title ??

    P.s, how can i thankyou for your sterling efforts and mega kind manner… It’s working exaclty how I imagined it. Beautiful.. damn damn beautiful. Thankyou so much.

    To sort by date, change this:

    ORDER BY p.post_title ASC";

    to this:

    ORDER BY p.post_date ASC";

    and, you may need to change ASC to DESC if the order is backward.

    If this is working, please use the dropdown at top right to mark this topic ‘Resolved’.

    Thread Starter jimmyt1988

    (@jimmyt1988)

    You are.. BRILLIANT. absolutely brilliant. Thankyou. Thankyou. Thankyou. Im so happy. Im so so damn happy. YESSS !!!!! THSNKSSSNAKNASANSKANSS

    You are quite welcome!

Viewing 12 replies - 31 through 42 (of 42 total)
  • The topic ‘Pagination, Not sure what to do’ is closed to new replies.