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.
Related
I want to output the Post by category through an ajax filter. It works with the standard post type in the same setup but with the custom post type it's showing all post on page load but when I click on a category link it's only showing the right category id "Array ( [action] => filter_wb [category] => 17 )"
And when I output the $query it shows [found_posts":0,],
so I think it's not finding the category content.
I've already checked if the category is empty but when I filter it in the backend the posts are there...
All files are there. jquery works and the xhr is gets the admin-ajax.php 200.
I'm on that for way too long time and can't figure it out.
Maybe you have some tips for me.
//post template
</header><!-- .entry-header -->
<div class="post-filter-wrapper">
<ul class="post-filter">
<li><a class="js-filter-item-wb" href="<?= home_url(); ?>">All</a></li>
<?php
$cat_args = array(
'taxonomy' => 'category_wb',
'exclude' => array(1),
'option_all' => 'All'
);
$categories = get_categories($cat_args);
foreach($categories as $cat) : ?>
<li><a class="js-filter-item-wb" data-category="<?= ($cat->term_id); ?>" href="<?= get_category_link($cat->term_id); ?>"><?= $cat->name; ?></a></li>
<?php endforeach; ?>
</ul>
</div>
<div class="grid grid--3col-grid js-filter">
<?php
$args = array (
'post_type' => 'wuerzburg',
'posts_per_page' => -1,
);
$query = new WP_Query($args);
if($query->have_posts()) :
while($query->have_posts()) : $query->the_post();
echo '<div class="grid__post-wrapper" id="post' . get_the_ID() . '">'; // grid-post-wrapper
echo '<h2 class="grid__preview-title">' . get_the_title() . '</h2>';
echo '<div class="grid__preview-image-wrapper"><a class="grid__preview-image" href="' . get_permalink() . '">' . get_the_post_thumbnail() . '</a></div>';
echo '<div class="grid__the-excerpt">' . get_the_excerpt() . '</div>';
echo '<div class="btn-custom-wrapper"><a class="btn-custom-a btn-custom-a--dark" href="' . get_the_permalink() . '">Vorbeischauen</a></div>';
echo '</div>'; // end of grid-post-wrapper
endwhile;
endif;
wp_reset_postdata(); ?>
</div><!-- js-filter -->
//jQuery filtering
(function($) {
//$(document).ready(function(){
$(document).on('click', '.js-filter-item-wb', function(e){
e.preventDefault();
var category = $(this).data('category');
$.ajax({
url: wp_ajax.ajax_url,
data: { action: 'filter_wb', category: category },
type: 'post',
success: function(result) {
$('.js-filter').html(result);
console.log('i work')
},
error: function(result) {
console.warn(result);
}
});
});
//});
})(jQuery);
//php filter
add_action( 'wp_ajax_nopriv_filter_wb', 'filter_wb_ajax' );
add_action('wp_ajax_filter_wb', 'filter_wb_ajax' );
function filter_wb_ajax() {
$category = $_POST['category'];
$args = array (
'post_type' => 'wuerzburg',
'posts_per_page' => -1,
);
if(isset($category)) {
$args['category__in'] = array($category);
}
$query = new WP_Query($args);
if($query->have_posts()) :
while($query->have_posts()) : $query->the_post();
echo '<div class="grid__post-wrapper" id="post' . get_the_ID() . '">'; // grid-post-wrapper
echo '<h2 class="grid__preview-title">' . get_the_title() . '</h2>';
echo '<div class="grid__preview-image-wrapper"><a class="grid__preview-image" href="' . get_permalink() . '">' . get_the_post_thumbnail() . '</a></div>';
//output testing
echo 'i should print';
//output testing
echo '<div class="grid__the-excerpt">' . get_the_excerpt() . '</div>';
echo '<div class="btn-custom-wrapper"><a class="btn-custom-a btn-custom-a--dark" href="' . get_the_permalink() . '">Vorbeischauen</a></div>';
echo '</div>'; // end of grid-post-wrapper
endwhile;
endif;
wp_reset_postdata();
print_r($_POST);wp_die();
}
Has anyone been able to use fancyBox to open the post thumbnail in the WordPress loop? Here is my code so far:
<?php
$query = new WP_query( 'pagename=about');
//The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo '<aside class="clear flex-container">';
echo '<div class="about">';
echo '<figure class="about-thumb">';
echo '<a href="#" class="fancybox-thumb" rel="fancybox- thumb">';
the_post_thumbnail('medium');
echo '</a>';
echo '</figure>';
echo '<div class="about-entry-content">';
the_content();
echo '</div>';
echo '</div>';
echo '</aside>';
}
}
/* Restore original Post Data */
wp_reset_postdata();
?>
I have not been able to find an answer to this. Any help appreciated. Thanks.
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...
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 />';
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 .' ...