<!--more--> being ignored when WordPress content is displayed - wordpress

I'm trying to write a plugin that will take page id's and return a preview of the page. Here's my code:
function page_preview($atts,$pageid = null) {
extract(shortcode_atts(array(
"pageid" => '0'
), $atts));
$the_query = new WP_Query( 'page_id=' . $pageid . '' );
global $more;
$more = 0;
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$title=get_the_title();
$thumbnail=get_the_post_thumbnail( $pageid, 'preview' );
$content=get_the_content();
}
}
return '<div class="callout">' .
'<h4>' .
$title .
'</h4>' .
$thumbnail .
$content . '<a href="' .
get_the_permalink($pageid) .
'">Continue reading</a></div>';
}
add_shortcode( 'pagepreview', 'page_preview' );
and it gets called on wpadmin editor like so:
[pagepreview pageid=11][pagepreview pageid=13][pagepreview pageid=8054]
i.e., that would display a page preview for each of those page ids.
The "more" doesn't work.
global $more;
$more = 0;
usually fixes this issue, but it's not in my case. Can anyone see what I"m doing wrong? Thanks.

You're getting the full content because
$content=get_the_content();
is not applying the_content() filters, and $more=0 must be on a line after $the_query->the_post(); inside the while loop. Change your while loop to this:
while ( $the_query->have_posts() ) {
$the_query->the_post();
$more=0;
$title=get_the_title();
$thumbnail=get_the_post_thumbnail( $pageid, 'preview' );
$content = apply_filters( 'the_content', get_the_content() );
$content = str_replace( ']]>', ']]>', $content );
}
See get_the_content and read more in pages on the WordPress Codex for where I sourced this.

Related

Show featured image from shortcode - Currently only have title and excerpt

I currently have a function pulled from WPBeginner which allows me to create a shortcode which pulls the stickied posts on the site wherever I want.
I am in need of this to also show the featured image but not sure how to get this to work currently. The current shortcode is below:
function wpb_latest_sticky() {
/* Get all sticky posts */
$sticky = get_option( 'sticky_posts' );
/* Sort the stickies with the newest ones at the top */
rsort( $sticky );
/* Get the 5 newest stickies (change 5 for a different number) */
$sticky = array_slice( $sticky, 0, 1 );
/* Query sticky posts */
$the_query = new WP_Query( array( 'post__in' => $sticky, 'ignore_sticky_posts' => 1 ) );
// The Loop
if ( $the_query->have_posts() ) {
$return .= '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$return .= '<li>' . get_the_title() . '<br />' . get_the_excerpt(). '</li>';
}
$return .= '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
return $return;
}
add_shortcode('latest_stickies', 'wpb_latest_sticky');
This is how I pull it on post pages:
<?php if ( has_post_thumbnail() && !is_search() ) { ?>
<a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( sprintf( esc_html__( 'Permalink to ', 'quark' ) . '%s', the_title_attribute( 'echo=0' ) ) ); ?>">
<?php the_post_thumbnail( 'post_feature_full_width' ); ?>
</a>
Any help would be greatly appreciated!
while ( $the_query->have_posts() ) {
$the_query->the_post();
$return .= '<li>' . get_the_title() . '<br />' . get_the_excerpt() . '<br/>' . get_the_post_thumbnail() . '</li>';
}

Display Custom Post Type Metabox Data

I'm having a problem displaying metabox data in a page template wp_query. I get this error:
Notice: Array to string conversion
Here is my code:
<?php
// WP_Query arguments
$args = array (
'post_type' => array( 'portfolio' ),
'ignore_sticky_posts' => true,
'posts_per_page' => '10',
);
// The Query
$portfolio_query = new WP_Query( $args );
// The Loop
if ( $portfolio_query->have_posts() ) {
while ( $portfolio_query->have_posts() ) {
$portfolio_query->the_post();
echo '<div id="portfolio">';
echo '<div class="featured_img">';
echo '' . " " . get_the_post_thumbnail() . '';
echo '</div>';
echo '<div class="portfolio">';
echo the_title( '<h2>', '</h2>' );
echo '<p>' . the_excerpt() . '</p>';
echo '<p>' . get_post_meta( get_the_ID($post->id , 'project_metabox', false) ) . '</p>';
echo '</div>';
echo '</div>';
}
} else {
echo "<h1>There are no portfolio pieces to view.</h1>";
}
// Restore original Post Data
wp_reset_postdata(); ?>
I've tried everything. This is just the latest version of my attempts. What am I doing wrong?
UPDATE:
Alright, so I found out the metadata isn't saving. I'm not exactly sure why, but I tried fixing it, and it's not working. My fixes made it worse. Here is my code:
`
public function save_metabox( $post_id, $post ) {
// Check if it's not an autosave.
if ( wp_is_post_autosave( $post_id ) )
return;
// Sanitize user input.
$project_new_web_design = isset( $_POST[ 'project_web_design' ] ) ? 'checked' : '';
$project_new_web_development = isset( $_POST[ 'project_web_development' ] ) ? 'checked' : '';
$project_new_digital_art = isset( $_POST[ 'project_digital_art' ] ) ? 'checked' : '';
$project_new_graphic_design = isset( $_POST[ 'project_graphic_design' ] ) ? 'checked' : '';
// Update the meta field in the database.
update_post_meta( $post_id, 'project_web_design ', $project_new_web_design );
update_post_meta( $post_id, 'project_web_development ', $project_new_web_development );
update_post_meta( $post_id, 'project_digital_art ', $project_new_digital_art );
update_post_meta( $post_id, 'project_graphic_design ', $project_new_graphic_design );
}
}
I believe the issue you're having is on this line:
echo '<p>' . get_post_meta( get_the_ID($post->id , 'project_metabox', false) ) . '</p>';
You seem to have mixed get_the_ID and $post->ID together. You're also attempting to echo an array.
The third parameter of get_post_meta will determine whether the value returned is a string or an array. Use true to return a single value.
Change it to:
echo '<p>' . get_post_meta( get_the_ID(), 'project_metabox', true ) . '</p>';
On a minor note you're trying to echo functions like the_excerpt(). You'll typically find that functions starting the_ instead of get_the_ will output directly instead of returning a value. Furthermore the_excerpt() will automatically add paragraph tags.
Replace this:
echo '<p>' . the_excerpt() . '</p>';
With this:
the_excerpt();

Using WP_Query in functions.php only returning one result

I've written a function in my functions.php file that runs a new WP_Query class to fetch some child pages on my custom page template according to their meta key/value. It's kind of working but it's only returning one result - I know there are more as I had the query running properly on the specific page before I turned it into a function. It returned all the correct results, but as I may need this functionality in several pages I decided to turn it into a function.
Here is my function code…
function contact_profiles($args) {
global $post;
$output = "";
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post();
$output = '<div class="staff-member">'
.'' . get_the_post_thumbnail() . ''
.'<h2 class="name">' . get_the_title() . '</h2>'
.'<h3 class="job-role">' . get_post_meta( $post->ID, 'job_role', true ) . '</h3>'
.'</div>';
endwhile;
wp_reset_postdata();
return $output;
}
and here is how I am calling it in my custom page template…
$myarray = array('meta_key' => 'job_area', 'meta_value' => 'Online', 'post_type' => 'page',);
echo contact_profiles($myarray);
Am I doing something obvious that I shouldn't be? Is the global $post bit causing the issue as I'm not sure I should call it from the functions file.
Most probably you have your posts per page setting to 1 in the back end reading section. If no custom value is passed to a custom query, the default option get_option( 'posts_per_page' ) is used as value to the posts_per_page parameter.
You solution would be to explicitely set posts_per_page to a desired amount or to -1 to get all posts
EDIT
I have missed this before, your concatenation is wrong.
$output = '<div class="staff-member">'
should be
$output .= '<div class="staff-member">'
Here is an updated version of your code
function contact_profiles($args)
{
$output = "";
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post();
$output .= '<div class="staff-member">'
.'' . get_the_post_thumbnail() . ''
.'<h2 class="name">' . get_the_title() . '</h2>'
.'<h3 class="job-role">' . get_post_meta( $post->ID, 'job_role', true ) . '</h3>'
.'</div>';
endwhile;
wp_reset_postdata();
return $output;
}

How to display thumbnails in Wordpress for related posts by author?

I have a related posts by author function using this code:
function get_related_author_posts() {
global $authordata, $post;
$authors_posts = get_posts( array( 'author' => $authordata->ID, 'post__not_in' => array( $post->ID ), 'posts_per_page' => 10 ) );
$output = '<div class="block">';
foreach ( $authors_posts as $authors_post ) {
$output .= '' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '';
}
$output .= '</div>';
return $output; }
To output I use the following code inside the loop of my single.php file:
<?php echo get_related_author_posts(); ?>
Currently the function it displays only post titles as links.
How should look this wp code in order to display thumbnails for this related posts by author function?
Try:
foreach ( $authors_posts as $authors_post ) {
$output .= get_the_post_thumbnail($authors_post->ID);
$output .= '' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '';
}
$output .= '</div>';
check get_the_post_thumbnail for more information like image size and attributes like extra classes.
Let me know if you need more help.

loop won't work inside my plugin

Is it possible to loop in a wordpress plugin?
I've created this plugin that utilizes a wordpress loop to grab some info about posts in my custom post type of events:
function getEventsFeed() {
$args = array( 'post_type' => 'events' );
$loop = new WP_Query( $args );
$htmlOutput = '<h2>Events</h2>';
while ( $loop->have_posts() ) : $loop->the_post();
$date = get_post_meta($post->ID, 'events_0');
$location = get_post_meta($post->ID, 'events_9');
$htmlOutput .= '<tr><td>' . the_title() . '</td><td>' . $date[0] . '</td><td>' . $post->post_title .'</td><td>' . $location[0] . '</td></tr>';
endwhile;
$htmlOutput .= '</div>';
echo $htmlOutput;
}
Problem is only the the_title info is being returned. $post is not working within the loop so $post->ID and $post->post_title are not being returned. I'm using this exact code in another page template and it returns all the data correctly. I'm not sure why it won't return when I use it in a plugin.
Any ideas?
Try adding
global $post;
to the beginning of your function. $loop->the_post() will set the global $post variable, but it is not available inside your function scope.

Resources