Retrieving *number* pages by page id

132 views Asked by At

Hello all,

Resolved

I am currently trying to develop a plugin for wordpress to output three pages into one page, the reason for this is to create something like below.

How i would want the plugin to output the information

I have looked around and not seen something similar to what i am wanting to do.

Below is my code & output. EDITED BELOW CODE: as according to Pieter Goosen

<?php
function page_list( $atts ) {

        //extracting input parameters (attributes of shortcode)
        shortcode_atts( array(
            'pages' => ''
        ), $atts );

/***************************************************Out***********************************************************/

$i = '';

// Get the page id by the page name

$page_names = explode(",", $atts['pages']);

foreach($page_names as $page_name) {


    $page = get_page_by_title( ''.$page_name.'', OBJECT, 'page' );

        //argument it

        $args = array(
            'post_type' => 'page',
            'posts_per_page' => 3,
            'page_id' => ''.$page->ID.''
        );

        // The Query
        $the_query = new WP_Query( $args );

        // The Loop
        if ( $the_query->have_posts() ) {

            $i .= '<div class="page-information">';

            $i .= '<div class="page-information-color">';

            while ( $the_query->have_posts() ) {

                $the_query->the_post();

                $i .= '<div class="page-information-widget" style="padding-left: 20px;">';

                $i .= '<div><a href="'.get_permalink( $the_query->ID).'"><img class="alignnone size-medium wp-image-81" src="'.wp_get_attachment_url( get_post_thumbnail_id($the_query->ID,'thumbnail') ).'" alt="" width="auto" height="70" /></a></div>';

                $i .= '<div><h4>'.get_the_title().'</h4></div>';

                $i .= '<div> '.wp_trim_words( get_the_excerpt(), 19, '...').' </div>';

                $i .= '<div><a href="'.get_permalink( $the_query->ID).'"><button class="news-widget-button" type="button">More info &#x3e;</button></a></div>';

                $i .= '</div>';


            }

            $i .= '</div>';

            $i .= '</div>';

        } else {

            echo 'No Pages Found! :(';

        }

        /* Restore original Post Data */
        wp_reset_postdata();

}

//returning the output to page
return $i;
}
//instantiate the shortcode
add_shortcode( 'list_pages', 'page_list' );

?>

enter image description here

This continues further down the page another 5/6 times.

I wondering why:

  1. I have multiple entry's
  2. They're not the pages I requested

Could anybody possibly help, fix the probelms?

2

There are 2 answers

0
Jamie Harding On BEST ANSWER

Resolved

What I done:

  1. Removed the second foreach ( creating twice as many duplicates)
  2. Adjusted the global output ( Changed it to a less guessable output )
  3. Changed the posts_per_page ( As its in a foreach it would get looped, there is no need for three, 1 will do. user can then specify how many pages to display.)
  4. Adjusted the placement of the main container divs classed "page-information && page-information-color"

Please feel free to manipulate below in your own answer for more efficiency or for different adaptions.

<?php
function page_list( $atts ) {

    //extracting input parameters (attributes of shortcode)
    shortcode_atts( array(
                'pages' => ''
            ), $atts );

    /***************************************************Out***********************************************************/

    $i = '';

    // Get the page id by the page name

    $page_names = explode(",", $atts['pages']);

    $i .= '<div class="page-information">';

    $i .= '<div class="page-information-color">';

    foreach($page_names as $page_name) {


        $page = get_page_by_title( ''.$page_name.'', OBJECT, 'page' );

        //argument it

        $args = array(
            'post_type' => 'page',
            'posts_per_page' => 1,
            'page_id' => ''.$page->ID.''
        );

        // The Query
        $the_query = new WP_Query( $args );

        // The Loop
        if ( $the_query->have_posts() ) {

            while ( $the_query->have_posts() ) {

                $the_query->the_post();

                $i .= '<div class="page-information-widget" style="padding-left: 20px;">';

                $i .= '<div><a href="'.get_permalink( $the_query->ID).'"><img class="alignnone size-medium wp-image-81" src="'.wp_get_attachment_url( get_post_thumbnail_id($the_query->ID,'thumbnail') ).'" alt="" width="auto" height="70" /></a></div>';

                $i .= '<div><h4>'.get_the_title().'</h4></div>';

                $i .= '<div> '.wp_trim_words( get_the_excerpt(), 19, '...').' </div>';

                $i .= '<div><a href="'.get_permalink( $the_query->ID).'"><button class="news-widget-button" type="button">More info &#x3e;</button></a></div>';

                $i .= '</div>';


            }

        } else {

            echo 'No Pages Found! :(';

        }

        /* Restore original Post Data */
        wp_reset_postdata();

    }

    $i .= '</div>';

    $i .= '</div>';

    //returning the output to page
    return $i;
    }
     //instantiate the shortcode
    add_shortcode( 'list_pages', 'page_list' );

    ?>
4
Pieter Goosen On

This line is wrong:

'page__in' => ''.$page->ID.''
  • page__in does not exists in WP_Query

  • Whatever ''.$page->ID.'' is, no one knows. This is for you to explain how you got to this

The above line should be

 'page_id' => $page_id->ID,

EDIT

Your code is very sloppy and error prone, sorry to say

  • Never ever use extract(). It has been removed for very specific reasons from core almost two years ago. Please see the Shortcode API on how to properly use attributes without extract()

  • global $out is really poor coding. Creating globals is evil and should be avoided. Giving your globals easy variable names like $out is even a bigger evil and sin. Just remember, if you use $out outside context, you break your global

  • Your variable names are confusing, $page vs $Page. You should avoid this. This in a year's time will look like a spelling mistake, and you know what that leads to. This can be really crappy and frustrating to debug due to the similarity in variable names