PHP - Combining recent posts with recent work - php

i'm trying to pull in 2 seperate feeds onto my wordpress homepage (recent portfolio work & recent blog posts). I want both sets to sit within the same ul. I've managed to pull in the portfolio feed, but i'm having trouble with the blog feed.
<ul>
<?php
$args = array( 'post_type' => 'work', 'posts_per_page' => 4 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >';
echo '<div class="recent-thumb">'; the_post_thumbnail(); echo '</div>';
echo '<div class="recent-title"><span>'; the_title(); echo '</span></div>';
echo '</a></li>';
endwhile;
?>
<?php
$args = array( 'post_type' => 'post', 'posts_per_page'=> 4 );
$the_query = new WP_Query( $args );
while ($the_query -> have_posts()) : $the_query -> the_post();
echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >';
echo '<div class="home-blog-category">'; user_the_categories(); echo '</div>';
echo '<div class="home-blog-title">'; the_title(); echo '</div>';
echo '<div class="home-blog-excerpt">'; echo excerpt(27); echo '</div>';
echo '<span class="home-blog-plus"><span class="home-blog-plus-wrap"><span class="horizontal"></span><span class="vertical"></span></span></span>';
echo '</a></li>';
endwhile;
?>
</ul>
Can anyone help with getting the post feed to pull? I'm sure this isn't the most efficient way of doing it, so if anyone has a better way of combining the two, that would also be appreciated! My knowledge of PHP is very limited :)
Cheers!

Use array_combine function in php to combine two results( $the_query & $loop ) and use a single while loop.
http://www.w3schools.com/php/func_array_combine.asp

Related

Getting Featured Image as a Background Image

I have a code in my page.php file that creates a list of child pages. I want every li to have a background-image added by Featured image function. Here is the entire code I have
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php endwhile; // end of the loop. ?>
<?php
if (is_page('eventsphotography')) {
$query = new WP_query('pagename=eventsphotography');
$eventsphotography_id = $query->queried_object->ID;
//The loop
if($query->have_posts() ) {
while($query->have_posts() ) {
$query->the_post();
the_content();
}
}
/* Get the children of the eventsphotography page */
$args = array (
'post_parent' => $thePostID,
'post_parent' => $eventsphotography_id,
'order' => 'ASC'
);
$eventsphotography_query = new WP_query($args);
//The Loop
if($eventsphotography_query->have_posts() ) {
echo '<ul class="events-list">';
while($eventsphotography_query->have_posts() ){
$eventsphotography_query->the_post();
$background = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
echo '<li style="background:url(' . $background[0] . '); background-repeat:no-repeat; background-size:cover;">';
echo '<div class="events-centered">';
echo '<a href="' . get_permalink() . '">';
echo '<h4>' . get_the_title() . '</h4>';
echo '</a>';
echo '<div class="view-events-details">';
echo '<a href="' . get_permalink() . '">';
echo '<h5>View Images</h5>';
echo '</a>';
echo '</div>';
echo '</div>'; /* end of events-centered */
echo '</li>';
}
echo'</ul>';
}
}
?>
I only need help for these lines:
$background = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
AND
echo '<li style="background:url(' . $background[0] . '); background-repeat:no-repeat; background-size:cover;">';
Here's the screenshot of the result of my code:
http://oi68.tinypic.com/10xzdhl.jpg
I marked the first <li> with a red rectangle. As I said before, I want URL of the featured image to be placed in <li style="background:url(URL of the featured image)">
I have found a solution. First, I created a new WP_Query:
$subs = new WP_Query( array( 'post_parent' => $post->ID, 'post_type' => 'page', 'meta_key' => '_thumbnail_id'));
Then in my loop I added this lines:
if($eventsphotography_query->have_posts() && $subs->have_posts()) {
echo '<ul class="events-list">';
while($eventsphotography_query->have_posts() && $subs->have_posts()){
$eventsphotography_query->the_post();
$subs->the_post();
echo '<li>';
echo get_the_post_thumbnail($post->ID);
...rest of the code...

how to echo/display the custom field value of a specific custom field name?

I have tried this code:
<?php
query_posts(array(
'meta_key' => 'custom_cat',
'meta_value' => 'creative',
'post_type' => 'page'
));
echo '<ul id="creative_services" class="clearfix row">';
if ( have_posts() ) while ( have_posts() ) : the_post();
echo '<li class="col-md-3">';
echo '<a class="popover-dismiss" data-toggle="popover" title="';
echo $post->post_title;
echo '" data-placement="bottom" data-content="';
echo $post->post_content;
echo '"><i class="';
get_post_meta( get_the_ID(), 'fa_icon' );
echo '"></i></a>';
echo '<h3>';
the_title();
echo '</h3>';
endwhile;
echo '</ul>';
wp_reset_query(); ?>
to display the custom field value as a class name, but the value is not displaying. Please help me find the problem or solution to display the custom field value as class name. I'm having hard time to debug this codes because i'm not a programmer, i'm a designer, still in the process of exploring wordpress.
Try adding echo before get_post_meta( get_the_ID(), 'fa_icon' );
so it would be:
<?php
query_posts(array(
'meta_key' => 'custom_cat',
'meta_value' => 'creative',
'post_type' => 'page'
));
echo '<ul id="creative_services" class="clearfix row">';
if ( have_posts() ) while ( have_posts() ) : the_post();
echo '<li class="col-md-3">';
echo '<a class="popover-dismiss" data-toggle="popover" title="';
echo $post->post_title;
echo '" data-placement="bottom" data-content="';
echo $post->post_content;
echo '"><i class="';
echo get_post_meta( get_the_ID(), 'fa_icon' );
echo '"></i></a>';
echo '<h3>';
the_title();
echo '</h3>';
endwhile;
echo '</ul>';
wp_reset_query(); ?>

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.

Why can't I use the_permalink() with echo in WordPress?

I have the following code:
<?php
query_posts(array(
'posts_per_page' => -1,
'post_type' => 'sample-letter',
'order' => 'ASC'
));
while(have_posts()){
the_post();
echo '<div class="col-md-9"><span class="title">title</span><br />';
}
wp_reset_query();
?>
It works great but the problem is, I can't use:
<?php the_permalink() ?>
INSIDE the echo statement. It is a simple link, and rather than render the link url, it outputs:
http://sitename.com/<?php the_permalink() ?>
Instead of:
http://sitename.com/thelink
How can I make this loop work without the echo? Is that actually the problem at all?
Use the echo part like;
echo '<div class="col-md-9"><span class="title">title</span><br />';
or you can use;
$current_post_id = get_the_ID(); // id of current post in the loop
$permalink = get_permalink( $current_post_id );
$title = get_the_title( $current_post_id );
echo '<div class="col-md-9"><span class="title">' . $title . '</span><br />';

Display Author Name (outside of loop)

I have an area of my index.php file that I'm customizing in a theme whereby I would like to display the most recent post and some additional meta. I was able to pull the featured image, post title (and link), & the date; but not the author. I've tried various examples from the WP boards with no luck. The latest attempt below:
<?php
$args = array( 'numberposts' => '1');
$recent_posts = wp_get_recent_posts( $args );
$feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
//$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
foreach( $recent_posts as $recent ){
$post_author = get_user_by( 'id', $recent->post_author );
echo '<div class="full-width" id="featured-post" style="background-image: url('. $feat_image .')">';
echo '<div class="row featured-post-meta"><div class="small-8 columns">';
echo '<h2><a href="' . get_permalink($recent["ID"]) . '" title="Read: '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a></h2>';
echo '<p>'. $post_author->display_name .' | '. get_the_time('F jS, Y') .'</p>';
echo '<a class="read-post" href="'. get_permalink($recent["ID"]) .'">Read the post</a>';
echo '</div></div></div>';
}
?>
Inside your foreach loop:
$post_author = get_user_by( 'id', $recent['post_author'] );
This gets you the user object which looks like what you were trying to do with $curauth.
You could then output $post_author->display_name in place of $curauth.
echo '<p>'. $post_author->display_name .' ...

Categories