Wordpress posts not pulling content - php

This is my code (below) I am attempting to pull posts only from category id 4 and It is working however it is not pulling the post content. It is displaying everything else.
Can anyone help me with this?
<?php get_header(); ?>
<div id="content" class="fixed">
<?php
echo '<div class="row fixed">';
echo '<div class="col580 no-print">';
global $post;
$myposts = get_posts('category=4');
foreach($myposts as $post) :
?>
<div class="post-item">
<?php
$src = null;
$count = 0;
$readmorelabel = get_option(EWF_SETUP_THNAME."_blog_read_more", __('— Read More', EWF_SETUP_THEME_DOMAIN));
$count++;
//## Get post classes
//##
$post_class = get_post_class();
$post_class_fin = null;
foreach($post_class as $key=> $ctclass){
$post_class_fin.= ' '.$ctclass;
}
//## Get post categories
//##
get_the_category( $post->ID );
$post_categories = null;
foreach((get_the_category( $post->ID )) as $category) {
if ($post_categories == null){
$post_categories.= '<a href="'.get_category_link( $category->term_id ).'" >'.$category->cat_name.'</a>';
}else{
$post_categories.= ', <a href="'.get_category_link( $category->term_id ).'" >'.$category->cat_name.'</a>';
}
}
//## Get post featured image
//##
$image_id = get_post_thumbnail_id($post->ID);
$image_url = wp_get_attachment_image_src($image_id,'blog-featured-image');
$src .= '<div class="blog-post '.$post_class_fin.'">';
$src .= '<div class="blog-post-date">'.get_the_time('d').' <span>'.get_the_time('M Y').'</span></div>' ;
$src .= '<h3 class="blog-post-title">'.get_the_title($post->ID).'</h3>' ;
$src .= '<ul class="blog-post-info fixed">
<li class="categories">'.$post_categories.'</li>
<li class="comments">'.get_comments_number().' '.__('Comments', EWF_SETUP_THEME_DOMAIN).'</li>
</ul>';
if ($image_id){
$src .= '<div><img class="blog-post-thumb" src="'.$image_url[0].'" width="480" height="200" alt="" /></div>';
}
global $more;
$more = false;
$src .= '<p>'.do_shortcode(get_the_content(' ')).'</p>';
$more = true;
$src .= '<div class="fixed"><p class="blog-post-readmore">'.$readmorelabel.'</p></div>';
if ($wp_query_blog->post_count != $count ){
$src .= '<div class="hr"></div>';
}
$src .= '</div>';
if ($wp_query->found_posts > $wp_query->query_vars['posts_per_page']){
$src .= ewf_sc_blog_navigation_steps($wp_query->query_vars['posts_per_page'], $wp_query);
}
echo $src;
?>
</div>
<?php comments_template(); ?>
<?php endforeach; wp_reset_postdata();
echo '</div>';
echo '<div class="col280 last">';
if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('sidebar-page') );
echo '</div>';
echo '</div>';
?>
</div>
<?php get_footer(); ?>

You are using get_posts() so you can easily get the post content by using post_content.
I see you use are using this
global $post;
$myposts = get_posts('category=4');
foreach($myposts as $post) :
Please avoid using global variables as local variables, by doing so you might change its value and you might face some issue later, rather do something as below
global $post;
$myposts = get_posts('category=4');
foreach($myposts as $mypost) :
and after this you can easily get the details of each post inside the for each loop for example
global $post;
$myposts = get_posts('category=4');
foreach($myposts as $mypost) :
echo $mypost->post_title; // This gives you the post title
echo $mypost->post_content; // This gives you the post content
endforeach;
You can try and take a reference from the above and try with your code.
Base line: if you are using get_posts() you can get the content of the post from $variable->post_content;
Hope it helps!!!

Related

ACF gallery show first image and open lightbox on click

I'm trying to create an archive page to show some realisations, which all have a gallery with multiple images. I use ACF to create a gallery and the Simple Lightbox plugin to create the lightbox. I found an example on how to combine both plugins and it's close to what I need, but I can't figure the rest out myself.
Now all images from the gallery are showing, I only need the first image to show and when you click the image I want to open the image in lightbox and have the possibility to go through the gallery this way.
What I have so far:
<?php if ( have_posts() ) {
while ( have_posts() ) { the_post(); ?>
<article id="realisatie-<?php the_ID(); ?>" <?php post_class(); ?> style="background-image: url(<?php echo $images[0] ?>);">
<?php
$images = get_field('realisatie_beelden');
$image_1 = $images[0];
if( $images ) { ?>
<div class="realisatie__gallery" >
<?php foreach( $images as $image ) {
$content = '<a class="gallery_image" href="'. $image .'">';
$content .= '<img src="'. $image .'" alt="'. $image .'" />';
$content .= '</a>';
if ( function_exists('slb_activate') ) {
$content = slb_activate($content);
}
?>
<?php } ?>
</div>
<?php } ?>
</article>
<?php }
} ?>
Thanks to a comment, I found a solution that works for me.
I only display an image for the first element, the other links are left empty.
<?php if ( have_posts() ) {
while ( have_posts() ) { the_post(); ?>
<article id="realisatie-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php
$images = get_field('realisatie_beelden');
$image_1 = $images[0];
if( $images ) { ?>
<?php
$i = 0;
foreach( $images as $image ) {
if ($i >= 1) {
$content = '';
} else {
$content = '<a href="'. $image .'">';
$content .= '<img src="'. $image .'" />';
$content .= '</a>';
$i++;
}
if ( function_exists('slb_activate') ) {
$content = slb_activate($content);
}
echo $content;
?>
<?php } ?>
<?php } ?>
</article>
<?php }
} ?>

Calling array in shortcode issues

I am condensing some area of duplicate code over multiple template into one shortcode that I can reuse easily.
I am calling the featured image url as an inline background image style at it works great! Here's the code.
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<?php while (have_posts()) : the_post(); ?>
<div class="single-casestudy" style="background-image: url('<?php echo $image[0]; ?>'); background-size: 100%;">
<div class="case-study-content">
<h4><?php the_title(); ?></h4>
<p><?php echo excerpt(22); ?></p>
</div>
</div>
<?php endwhile ; ?>
This works fine when not in the shortcode, and I can see my featured image as the background on the live site, however when putting it into a shortcode function, it doesn't error, it just doesn't work! Heres the code I am using for the shortcode functionality. It seems like the $image[0] is not pulling through any data into the array.
function otherinfo_function() {
$args = array( 'post_type' => 'casestudy',
'posts_per_page' => -1,
'orderby' => menu_order,
'order' => RAND
);
query_posts($args);
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ),'single-post-thumbnail' );
while (have_posts()) : the_post();
echo $image[0];
$output = $output . '<div class="single-casestudy" style="background-image: url(' . $image[0] .'); background-size: 100%;">';
$output = $output . '<div class="case-study-content">';
$output = $output . '<h4>' . get_the_title() . '</h4>';
$output = $output . '<p>' . excerpt(22) . '</p>';
$output = $output . '</div>';
$output = $output . '</div>';
endwhile ;
$output = $output . '</div>';
return $output;
}
add_shortcode('otherinfo', 'otherinfo_function');
If anyone could help that would super great!
Sam

Recent post display using shortcode

I am trying to display recent post in staic page home-content.php so i have added this code in function.php
here it is a code
function my_recent_posts_shortcode($atts){
$q = new WP_Query(
array( 'orderby' => 'date')
);
$list ="";
while($q->have_posts()) : $q->the_post();
echo '<div class="item">';
$title=get_the_title();
if ( has_post_thumbnail() ) {
echo '<a class="single-image link-icon" href="' . get_permalink() . '">';
$list .=the_post_thumbnail(array(300,200),array('alt' =>$title));
echo '</a>';
}
echo '<h6 class="title"><span>'.$title.'</span></h6>';
echo '<div class="entry-body">';
$list .= wpe_excerpt('wpe_excerptlength_index', '');
echo '<a class="button default color" href="' . get_permalink() . '">Read More</a>';
echo '</div>';
echo '</div>';
endwhile;
wp_reset_postdata();
return $list ;
}
add_shortcode('recent-posts', 'my_recent_posts_shortcode');
[recent-posts] this is a shortcode for display recent post
and home-content.php for showing post
<?php
$post_id = 7;
$queried_post = get_post($post_id);
?>
<p><?php $check=$queried_post->post_content; ?></p>
<?php echo do_shortcode('["'.$check.'"]');?>
All the recent post display on home page of my custom theme http://templategraphy.com/wp-demo/businessguru/
but the problem is theme structure is not properly shown i
want this type of structure is shown http://templategraphy.com/demo/businessguru/
suggest some solution where i am doing wrong.

Display custom post type posts and meta data within the_content filter

I am using this in my mu-plugins.php file//
function new_default_content($content) {
global $post;
if ($post->post_type == 'textures') {
$content .='<li>
<figure>
<?php the_post_thumbnail('thummy'); ?>
<figcaption>
<h3><?php the_title(); ?></h3>
<span>Cool stuff brah.</span>
<?php
if ( has_post_thumbnail()) {
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large');
echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >View Full Image</a>'; }?>
</figcaption>
</figure>
</li>';
}
return $content;
}
add_filter('the_content', 'new_default_content');
and in my page template I used <?php the_content(); ?> to display everything.
UPDATE//
Full Page Template code
<div id="container" class="clearfix">
<div id="left-content">
<?php get_sidebar('two');?>
</div>
<div id="right-content">
<h1><?php wp_title(''); ?></h1>
<ul class="grid cs-style-3">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif; ?>
</ul>
</div><!--right-content-->
</div><!--container-->
<?php get_footer(); ?>
But I am recieving this error//
Parse error: syntax error, unexpected T_STRING in /home/xxx/public_html/domain.com/testing/wp-content/mu-plugins/must-use.php on line 15
I am trying this method of displaying content because I want to use the WP-Members plugin, and I realized that the plugin will only work for content within the_content().
So my question is how can I fix the code I posted above to display the title, thumbnail, links etc the correct way?
function new_default_content($content) {
global $post;
if ($post->post_type == 'textures') {
$content .='<li>';
$content .='<figure>';
$content .= the_post_thumbnail('thummy');
$content .= '<figcaption>';
$content .= '<h3>';
$content .= the_title();
$content .= '</h3>';
$content .= '<span>Cool stuff brah.</span>';
if ( has_post_thumbnail()) {
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large');
echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >View Full Image</a>';
}
$content .= '</figcaption>';
$content .= '</figure>';
$content .= '</li>';
}
return $content;
}
add_filter('the_content', 'new_default_content');
Just try this code. If you still get same error we'll check.
FOUND A SOLUTION
I remembered that you can add content inside of shortcodes.
So I created my own shortcode [textures_content]
Then used the code pasted below to display the content within <?php the_content(); ?> function//
add_shortcode( 'textures_content', 'textures_shortcode' );
function textures_shortcode( $atts ) {
ob_start();
$query = new WP_Query( array(
'post_type' => 'textures',
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'date',
) );
if ( $query->have_posts() ) { ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<li>
<figure>
<?php the_post_thumbnail('thummy'); ?>
<figcaption>
<h3><?php the_title(); ?></h3>
<?php if ( has_post_thumbnail()) {
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large');
echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >View Full Image</a>'; }?>
</figcaption>
</figure>
</li>
<?php endwhile;
wp_reset_postdata(); ?>
<?php $myvariable = ob_get_clean();
return $myvariable;
}
}
Now the Members only plugin works as expected and the blocked content is displayed once logged in :)

How to Achieve Something Like This In WordPress

Hey there, I'm trying to create a page showing specific pages (hope that makes sense), probably by calling their post ID's or something.
I want to pull in the page thumbnail/featured image, the page title, the page's description, and then a link to that page.
Something along the lines of this.
<ul>
<li>
<?php the_post_thumbnail(); ?>
<h2>Page Title</h2>
<p>Page Description</p>
Link to page
</li>
</ul>
Any help will be appreciated, thanks in advance.
UPDATE: At the moment I've got something like this. Using a custom field to bring in the description. I'm still trying to work out how I'd only show pages that are under a parent page called "Culture".
<?php query_posts('post_type=page'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_post_thumbnail(); ?>
<h2><?php the_title(); ?></h2>
<p>
<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'description', true);
?>
</p>
More info
<?php endwhile; endif; ?>
UPDATE 2: Solved it! Used the following if anyone's interested.
Pulled in all subpages from parent page (id=7).
Then the post thumbnail, followed by the page title, description using a custom field called description and finally the permalink.
Hope this helps anyone in a similar situation.
<?php query_posts('post_type=page&post_parent=7'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_post_thumbnail('culture-page-listing'); ?>
<h2><?php the_title(); ?></h2>
<p>
<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'description', true);
?>
</p>
More info
<?php endwhile; endif; ?>
I wrote a loop in WP some time ago which I'm sure isn't perfect, but it did basically something like that (delimited by categories).
http://www.kyleboddy.com/2010/10/14/wordpress-code-attachment-category-loop/
<?php
$areas = array(1 => 'Seattle','East Side & Mercer Island','North Side','South Side');
$slugs = array(1 => 'seattle-jobs','east-side-and-mercer-island-jobs','north-end-jobs','south-end-and-west-seattle-jobs');
$i = count($areas);
$n = 1;
while ($n <= $i)
{
global $post;
$myposts = get_posts('numberposts=-1&offset=0&category_name=' . $slugs[$n]);
echo '<div id="imageList">';
echo '<a name="' . $areas[$n] . '"></a><h2>' . $areas[$n] . '</h2>';
echo '<table id="ourwork"><tr>';
$x = 1;
foreach($myposts as $post)
{
setup_postdata($post);
echo '<td>';
$args = array(
'post_type' => 'attachment',
'numberposts' => '-1',
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts($args);
if ($attachments) {
$y = count($attachments);
$y--;
echo '<a href="' . $post->guid . '">';
echo wp_get_attachment_image($id = $attachments[$y]->ID, $size=array(200,133), $icon = false);
echo '<strong><br><br>';
echo apply_filters('the_title', $attachments[$y]->post_title);
echo '</strong></a>';
echo '</td>';
if ($x == 4)
{
echo '</tr><tr>';
$x = 0;
}
$x++;
}
}
echo '</tr></table>';
echo '</div><div class="blog"></div>';
$n++;
}

Categories