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><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" /></div>';
$i .= '<div><h4>'.get_the_title().'</h4></div>';
$i .= '<div> '.wp_trim_words( get_the_excerpt(), 19, '...').' </div>';
$i .= '<div><button class="news-widget-button" type="button">More info ></button></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' => ''.$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
Resolved
What I done:
Removed the second foreach ( creating twice as many duplicates)
Adjusted the global output ( Changed it to a less guessable output )
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.)
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><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" /></div>';
$i .= '<div><h4>'.get_the_title().'</h4></div>';
$i .= '<div> '.wp_trim_words( get_the_excerpt(), 19, '...').' </div>';
$i .= '<div><button class="news-widget-button" type="button">More info ></button></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' );
?>
Related
I am fairly new to WP functions so hoping someone could shed some light on this issue. I have a function that displays five posts from a specific category, then using a shortcode to display the results on certain WP posts/pages. I am having trouble to include wp_trim_words to get_the_title(). I want to limit the title to 5 words and end with "...". I saw many examples in here but none that would fit into my function. Can anyone help please?
function wpb_postsbycategory() {
// the query
$the_query = new WP_Query( array( 'category_name' => '3dgames', 'posts_per_page' => 5 ) );
// The Loop
if ( $the_query->have_posts() ) {
$string .= '<ul class="postsbycategory widget_recent_entries">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
if ( has_post_thumbnail() ) {
$string .= '<p>' . get_the_post_thumbnail($post_id, array( 100, 50) ) . get_the_title() .'<br></p>';
} else {
// if no featured image is found
$string .= '<li>' . get_the_title() .'</li>';
}
}
} else {
// no posts found
}
$string .= '</ul>';
return $string;
/* Restore original Post Data */
wp_reset_postdata();
}
// Add a shortcode
add_shortcode('3dgames', 'wpb_postsbycategory');
// Enable shortcodes in text widgets
add_filter('widget_text', 'do_shortcode');
You should use wp_trim_words() and inside place the title for the post:
wp_trim_words(get_the_title(), 5, '...')
In your case it should be something like this:
if ( has_post_thumbnail() ) {
$string .= '<p>' . get_the_post_thumbnail($post_id, array( 100, 50) ) . (wp_trim_words(get_the_title(), 5, '...')) .'<br></p>';
} else {
// if no featured image is found
$string .= '<li>' . (wp_trim_words(get_the_title(), 5, '...')) .'</li>';
}
For more details on how wp_trim_words() works you could visit the official documentation: https://developer.wordpress.org/reference/functions/wp_trim_words/
I need to separate posts from WP_Query into 2 columns: first post will go to right column, 3 other posts will go to left column so the structure will look like this - https://prnt.sc/vc044d
Please help me to improve my code.
Thanks a lot.
function mk_latestposts() {
// the query
$the_query = new WP_Query( array( 'posts_per_page' => 4 ) );
// The Loop
if ( $the_query->have_posts() ) {
$postcount = 0;
$string .= '<ul class="mk-recent-posts-list">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$postcount++;
// if this is the first post
if ( $postcount == 1 ) {
$string .= '<li class="first-post">';
$string .= '' . get_the_post_thumbnail($post_id, array( 50, 50) ) . get_the_title() . get_the_date() .'</li>';
} else {
// if this is the next post
$string .= '<li class="next-post">';
$string .= '' . get_the_post_thumbnail($post_id, array( 50, 50) ) . get_the_title() . get_the_date() .'</li>';
}
}
} else {
// no posts found
}
$string .= '</ul>';
return $string;
/* Restore original Post Data */
wp_reset_postdata();
}
// Add a shortcode
add_shortcode('mklatestposts', 'mk_latestposts');
Your code is fine You can do this with CSS. Just add a common class to every <li> Like <li class='single-post'> and then apply this CSS. You can change it from List structure to Div structure whatever you want.
li.single-post{ width: 47%; float: left;} ul.mk-recent-posts-list { width: 100%; }
My code had a typo!
But im not allowed to delete this so here you have some info on ways you could have done a better job then me ;)
The problem is as follows, My query is skipping a part of my loop.
i have a query that makes a anchor with the title and thumbnail of the post.
it runs the query fine for the first post except for the second post it does not load in the thumbnail, but it does show the title, and the title is only mentioned after the thumbnail, There is no small image that resembles it cant be found either. My first question posted on here so apolagies for misplacing items in the wrong sections.
<?php
// WP_Query arguments
$args = array (
'post_type' => array( 'klantcase' ),
'post_status' => array( 'publish' ),
'nopaging' => true,
'order' => 'ASC',
'orderby' => 'menu_order',
);
// The Query
$klantcases = new WP_Query( $args );
// The Loop
if ( $klantcases->have_posts() ) {
while ( $klantcases->have_posts() ) {
$klantcases->the_post();
echo "<a href=".get_the_permalink().">";
echo get_the_post_thumbnail( null, $size = 'post-thumbnail');?><br><?php
echo the_title();?><br><?php
echo "</a>";
}
} else {
echo "no posts found";
}
// Restore original Post Data
wp_reset_postdata();
Now there is probably many ways to improve this loop but as mentioned im very new to all of this. That said i'd love to hear how you guys would resolve this issue.
Define empty variable on top of while loop.
And in loop concatenate your html with that empty variable.
For Example.
$output = '';
if ( $klantcases->have_posts() ) {
while ( $klantcases->have_posts() ) {
$klantcases->the_post();
$output .= '<a href="'.the_permalink().'">';
$output .= the_post_thumbnail() .'<br>';
$output .= the_title();
$output .= '</a>';
}
} else {
echo "no posts found";
}
echo $output;
You don't need to echo the_title() - it already echoes out, so I'm guessing that's where part of the problem is coming in. You can also make your loop a little more simple.
You don't need the 'post_status' argument, as publish is default.
In your loop, let's use WP's built in echo functions: the_permalink(), the_title() and the_post_thumbnail(). You don't have to pass any arguments to the the_post_thumbnail() because you are just calling the default in your code.
<?php
// WP_Query arguments
$args = [
'post_type' => ['klantcase'],
'nopaging' => TRUE,
'order' => 'ASC',
'orderby' => 'menu_order',
];
// The Query
$klantcases = new WP_Query( $args );
// The Loop
if ( $klantcases->have_posts() ) {
while ( $klantcases->have_posts() ) {
$klantcases->the_post(); ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?><br>
<?php the_title(); ?>
</a>
<?php
}
} else {
echo 'no posts found';
}
// Restore original Post Data
wp_reset_postdata();
I use a crowdfunding-plugin for wordpress called personal fundraiser which you can see here. It uses one post-type called cause and another called campaign. The campaign posts uses the cause posts as a template. The reason why i ask here is that the plugin-developer answer questions every second month or so.
There are one shortcode which i want to change the content of. The first thing i want is to get the campaing-image. I probably need a code to get it from the asociated cause (the template). The second thing i want is to echo shortcodes in the $list_content you see below. How do i add shortcodes there? If i use $list_content .= '<?php echo do_shortcode( $content ) ?>' i get php-error. The code i want to change looks like this:
`function pfund_campaign_list() {
global $wp_query;
wp_enqueue_style( 'pfund-user', pfund_determine_file_location('user','css'),
array(), PFUND_VERSION );
$post_query = array(
'post_type' => 'pfund_campaign',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1
);
if ( isset( $wp_query->query_vars['pfund_cause_id'] ) ) {
$post_query['meta_query'] = array(
array(
'key' => '_pfund_cause_id',
'value' => $wp_query->query_vars['pfund_cause_id']
)
);
}
$campaigns = get_posts($post_query);
$list_content = '<ul class="pfund-list">';
foreach ($campaigns as $campaign) {
$list_content .= '<li>';
$list_content .= ' <h2>';
$list_content .= ' '.$campaign->post_title.'</h2>';
$list_content .= '</li>';
}
$list_content .= '</ul>';
return $list_content;
}`
In another function to list the causes, this code is used to get the cause-image, but when i use it in the campaign_list nothing happens:
`$cause_img = get_post_meta($cause->ID, '_pfund_cause_image', true);
if ( $cause_img ) {
$list_content .= '<img class="pfund-image" width="100%" src="'.wp_get_attachment_url( $cause_img ).'"/>';
}`
You can see the whole .php-file here
Any help is very much apreciated.
To get a shortcode in your theme do this
$list_content .= do_shortcode($content);
then you can change in your $list_content, you cant echo in a predefined variable.
In-place of
$list_content .= '<?php echo do_shortcode( $content ) ?>'
I need to change some code from the WP-Bootstrap Wordpress Files.
In their code they remove the standard gallery code and replace it with their own which works fine accept it doesn't properly account for Rows.
function gallery_shortcode_tbs($attr) {
global $post, $wp_locale;
$output = "";
$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID );
$attachments = get_posts($args);
if ($attachments) {
$output = '<div class="row-fluid"><ul class="thumbnails">';
foreach ( $attachments as $attachment ) {
$output .= '<li class="span3">';
$att_title = apply_filters( 'the_title' , $attachment->post_title );
$output .= wp_get_attachment_link( $attachment->ID , 'thumbnail', true );
$output .= '</li>';
}
$output .= '</ul></div>';
}
return $output;
}
could anyone advise on how to edit the above code to insert html code to close the row and start another after the 3rd item.. or any number i choose?
thank you in advance.
R
Take a look at the default WP gallery shortcode source, where a break is inserted after the specified number of columns, on lines 837-838:
if ( $columns > 0 && ++$i % $columns == 0 )
$output .= '<br style="clear: both" />';