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.
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 ></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' );
?>
This continues further down the page another 5/6 times.
I wondering why:
- I have multiple entry’s
- They’re not the pages I requested
Could anybody possibly help, fix the probelms?
This line is wrong:
page__in
does not exists inWP_Query
Whatever
''.$page->ID.''
is, no one knows. This is for you to explain how you got to thisThe above line should be
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 withoutextract()
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 globalYour 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 namesResolved
What I done:
Please feel free to manipulate below in your own answer for more efficiency or for different adaptions.