I have been trying to customize my site but I have met a problem... As I have stated in the title, what shall I add in order to make it possible? I will like the make the category with the latest post move to the first. I have tried for 5 hours and still failed to do it. Please teach me how to fix it.
<?php
//Get the desired categories and order by ID
$cat_args = array(
'orderby' => 'id'
);
//For each category show a random post
$categories = get_categories($cat_args);
foreach ($categories as $category) {
?>
<?php
$post_args = array(
'numberposts' => 1,
'category' => $category->term_id,
);
$posts = get_posts($post_args);
foreach ($posts as $post) {
?>
<article <?php post_class('post-list animated fadeIn'); ?> role="article">
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<figure class="eyecatch<?php if (!has_post_thumbnail()) : ?> noimg<?php endif; ?>">
<?php the_post_thumbnail('home-thum'); ?>
<?php archivecatname(); ?>
</figure>
<section class="entry-content cf">
<h1 class="h2 entry-title"><?php the_title(); ?></h1>
<div class="byline entry-meta vcard">
<?php if (get_option('post_options_authordisplay', 'author_off') == 'author_on') : ?><span class="writer name author"><?php echo get_avatar(get_the_author_meta('ID'), 30); ?><span class="fn"><?php the_author(); ?></span></span><?php endif; ?>
</div>
<div class="description"><?php the_excerpt(); ?></div>
</section>
</a>
</article>
<?php get_template_part('loop'); ?>
<?php
}
}
?>
Query Arguments
$args = array(
'cat' => $category->term_id,
'post_type' => 'post',
'posts_per_page' => '1',
);
Running the Query
$query = new WP_Query( $args );
if ( $query->have_posts() ) { ?>
<section class="<?php echo $category->name; ?> listing">
<h2>Latest in <?php echo $category->name; ?>:</h2>
<?php while ( $query->have_posts() ) {
$query->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'category-listing' ); ?>>
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'thumbnail' ); ?>
</a>
<?php } ?>
<h3 class="entry-title">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h3>
</article>
<?php } // end while ?>
</section>
<?php } // end if
// Use reset to restore original query.
wp_reset_postdata();
I've 2 post types and I want to output them alternately but without using many loops so I found this solution which does that.
However, it is not ideal as I need to output the_post_thumbnail which I find I am unable to do using this method (echo $smallPosts->posts[$i]->post_thumbnail; does nothing). Additonally I've read post_content is not the same as the_content(); - with the latter what I want to use.
Any suggestions on how I can loop through the alternating post types and have more control over the output so I can use the_post_thumnail etc.?
Below is my code that does work but just doesn't quite do what I require.
<?php $args = array(
'post_type' => 'small_post',
'posts_per_page' => 3
);
$smallPosts = new WP_Query($args);
$args = array(
'post_type' => 'full_post',
'posts_per_page' => 3
);
$fullPosts = new WP_Query($args);
for ($i = 0; $i < 3; $i++) {
if ($smallPosts->post_count > $i)
echo $smallPosts->posts[$i]->post_title;
echo '<br />';
echo $smallPosts->posts[$i]->post_content;
echo '<br />';
if ($fullPosts->post_count > $i)
echo $fullPosts->posts[$i]->post_title;
echo '<br />';
echo $fullPosts->posts[$i]->post_content;
echo '<br />';
}
?>
This is my solution which outputs both post types and using the time published they can be alternated and I can use the_thumbnail( ); and other functions I need. Additionally I've used if statements to add classes as the different post types need to be styled differently.
<ul>
<?php
$args = array(
'posts_per_page' => 10,
'post_type' => (array('small_post','full_post')),
);
query_posts($args); ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php if ( get_post_type( get_the_ID() ) == 'small_post' ) { ?>
<li class="article small-post" style="">
<?php if(has_post_thumbnail()) :?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(''); ?>
</a>
<?php endif;?>
<a href="<?php the_permalink(); ?>">
<h3>
<?php the_title(); ?>
</h3>
</a>
<p><?php the_excerpt(); ?></p>
</li>
<?php } ?>
<?php if ( get_post_type( get_the_ID() ) == 'full_post' ) { ?>
<li class="article full-post" style="">
<?php if(has_post_thumbnail()) :?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(''); ?>
</a>
<?php endif;?>
<a href="<?php the_permalink(); ?>">
<h3>
<?php the_title(); ?>
</h3>
</a>
<p><?php the_excerpt(); ?></p>
</li>
<?php } ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
</ul>
<?php
$args=array(
'post_type'=>'post',
'cat' => '',
'posts_per_page' => 4,
'paged'=>$paged
);
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query($args);
if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<div id="box1">
<h1 class="blog1"><?php the_title(); ?></h1>
<h2 class="blog2">Posted By :- <?php the_author_meta('display_name'); ?> <span class="blog3">
<?php echo get_the_date(); ?></span></h2>
<?php $a=wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
<span class="blog4"><img src="<?php echo get_template_directory_uri(); ?>/timthumb.php?src=<?php echo $a; ?>&w=200&h=150&q=100" alt=""></span>
<p class="blogcontent"><?php $a=get_the_content();
$b=strlen($a);
if($b>770)
{
$con = substr($a, 0, strrpos(substr($a,0,770),' ')). '....';
echo $con;
?>
<p style=" float:right;"><a style ="margin-top:15px;" href="<?php echo get_permalink($post->ID);?> ">Read More</a></p>
<?php
}
else
{
echo $a;
}
?>
</p>
</div>
<?php //endforeach;
endwhile; ?>
<?php endif; ?>
<span style="padding:30px;"><?php if(function_exists('wp_paginate')) {wp_paginate();}
$wp_query = null;
$wp_query = $temp;
wp_reset_query(); ?>
</span>
I working on wordpress blog and make a pagination in blog there were issue in pagination.when click on page 2 the url change but page not changed.i am using the following code for that
Thanks & Regards
Try this code:
<?php
$args=array(
'post_type'=>'post',
'cat' => '',
'posts_per_page' => 4,
'paged'=>$paged
);
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query($args);
if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<div id="box1">
<h1 class="blog1"><?php the_title(); ?></h1>
<h2 class="blog2">Posted By :- <?php the_author_meta('display_name'); ?> <span class="blog3">
<?php echo get_the_date(); ?></span></h2>
<?php $a=wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
<span class="blog4"><img src="<?php echo get_template_directory_uri(); ?>/timthumb.php?src=<?php echo $a; ?>&w=200&h=150&q=100" alt=""></span>
<p class="blogcontent"><?php $a=get_the_content();
$b=strlen($a);
if($b>770)
{
$con = substr($a, 0, strrpos(substr($a,0,770),' ')). '....';
echo $con;
?>
<p style=" float:right;"><a style ="margin-top:15px;" href="<?php echo get_permalink($post->ID);?> ">Read More</a></p>
<?php
}
else
{
echo $a;
}
?>
</p>
</div>
<?php //endforeach;
endwhile; ?>
<?php endif; ?>
<span style="padding:30px;">
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
$wp_query = null;
$wp_query = $temp;
wp_reset_query(); ?>
</span>
Alternatively, you can see this tutorial if you want to have numbered pagunation.
I need to use the same page for different taxonomies and terms.
How can I retrieve the taxonomies and the terms from the URL and run the same page for those I that I want?
The query should be run through a URL since I am not using forms.
When the user clicks on the link, a new page should be opened that is an archive of posts having custom post type, taxonomy and taxonomy terms specified in the URL.
This is the code that I have right now:
<? /*
* Template Name: Activities template
* Description: Template for activties like restaurants, pubs, etc.
*/
?>
<?php $options = get_option('mh_options'); ?>
<?php get_header(); ?>
<?php
if ( get_query_var('paged') ) {$paged = get_query_var('paged');}
if ( get_query_var('page') ) {$paged = get_query_var('page');}
$args = array(
'post_type' => 'activties',
'tax_query' => array(
array(
'taxonomy' => 'restaurants',
'field' => 'slug',
'terms' => 'italian'
),
'paged' => $paged
)
);
query_posts( $args ); ?>
<div class="wrapper clearfix">
<div class="main">
<div class="content <?php mh_content_class(); ?>">
<?php mh_before_page_content(); ?>
<?php dynamic_sidebar('pages-1'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<!-- <?php the_content(); ?> -->
<article <?php post_class(); ?>>
<div class="loop-wrap loop-layout2">
<div class="clearfix">
<div class="loop-thumb">
<a href="<?php the_permalink(); ?>">
<?php if( get_field('business_logo') ): ?>
<?php $image = wp_get_attachment_image_src(get_field('business_logo'), 'loop'); ?>
<img src="<?php echo $image[0]; ?>" alt="<?php the_field('business_logo');?>" data-thumb="<?php echo $thumb[0]; ?>" />
<?php else: echo '<img src="' . get_template_directory_uri() . '/images/noimage_300x225.png' . '" alt="No Picture" />'; ?>
<?php endif; ?>
</a>
</div>
<div class="loop-content">
<header>
<h3 class="loop-title"><?php the_title(); ?></h3>
</header>
<?php if (get_field('business_description')): ?>
<?php $text_to_trim = get_field('business_description');
echo '<div class="mh-excerpt">'. davide_excerpt($text_to_trim) . '</div>' . "\n" ; ?>
<?php endif; ?>
<?php if (get_field('business_paying_desc')): ?>
<?php $text_to_trim = get_field('business_paying_desc');
echo '<div class="mh-excerpt">'. davide_excerpt($text_to_trim) . '</div>' . "\n" ; ?>
<?php endif; ?>
</div>
</div>
</div>
</article>
<?php dynamic_sidebar('pages-2'); ?>
<?php endwhile; ?>
<!-- <?php wp_reset_postdata(); ?> -->
<?php wp_reset_query(); ?>
<?php if (isset($options['comments_pages']) ? $options['comments_pages'] : false) : ?>
<section>
<?php comments_template(); ?>
</section>
<?php endif; ?>
<?php endif; ?>
</div>
<?php get_sidebar(); ?>
</div>
<?php mh_second_sb(); ?>
</div>
<?php get_footer(); ?>
EDIT
After a few days, I found the solution that I am going to post, however it has got an issue: I cannot see the list of the posts if a post has got multiple terms (more than 1 term basically) selected by the checkboxes when it is created or edited.
This is my taxonomy.php file
<?php
$post = $wp_query->post;
/*$catMerc = array('restaurants','pubs', 'the-pizzerias', 'bars', 'cafes', 'nightlife-clubs', 'shopping', 'the-coffeeshops');*/
$catMerc = array('the-restaurants', 'the-coffeeshops', 'the-pizzerias', 'shopping', 'nightlife-clubs', 'cafes', 'bars', 'pubs');
$termsObjects = wp_get_object_terms($post->ID, $catMerc);
//Assuming your post only has one category, if theres more it will return multiple objects in it's return array:
$currentCustomCat = $termsObjects[0]->slug;
$currentCatMerc = get_query_var('taxonomy');
//you can have 'name' instead of 'slug' if that helps too
$customcatarray = array('american-pubs-exclusive5', 'american-pubs-in-town', 'beer-houses-exclusive5', 'beer-houses-in-town', 'free-joints-exclusive5', 'free-joints-in-town', 'local-atmosphere-exclusive5', 'local-atmosphere-in-town', 'spanish-atmosphere-exclusive5', 'spanish-atmosphere-in-town', 'take-away-exclusive5', 'take-away-in-town', 'traditional-dutch-exclusive5', 'traditional-dutch-in-town', 'african-exclusive-5', 'african-in-town', 'argentinian-restaurants-exclusive5', 'argentinian-restaurants-in-town',' asian-restaurants-exclusive5', 'asian-restaurants-in-town', 'dutch-restaurants-exclusive5', 'dutch-restaurants-in-town', 'french-restaurants-exclusive5', 'french-restaurants-in-town', 'italian-restaurants-exclusive5', 'italian-restaurants-in-town', 'seafood-restaurants-exclusive5', 'seafood-restaurants-in-town', 'spanish-restaurants-exclusive5', 'spanish-restaurants-in-town', 'cocktail-bars-exclusive5', 'cocktail-bars-in-town', 'disco-bars-exclusive5', 'disco-bars-in-town', 'dutch-bars-exclusive5', 'dutch-bars-in-town', 'internet-cafes-exclusive5', 'internet-cafes-in-town', 'lounge-bars-exclusive5', 'lounge-bars-in-town', 'art-cafes-exclusive5', 'art-cafes-in-town', 'breakfast-lunch-exclusive5','breakfast-lunch-in-town', 'famous-cafes-exclusive5', 'famous-cafes-in-town', 'fashion-cafes-exclusive5', 'fashion-cafes-in-town', 'timeout-exclusive5', 'timeout-in-town', 'best-boutiques-exclusive5','best-boutiques-in-town', 'famous-brands-exclusive5', 'famous-brands-in-town', 'sportswear-exclusive5', 'sportswear-in-town', 'the-pizzerias-with-table-service-in-town', 'the-pizzerias-with-table-service-exclusive5', 'the-pizzerias-takeway-in-town', 'the-pizzerias-takeaway-exclusive5', 'the-coffeeshops-in-town', 'the-coffeeshops-exclusive5');
if (in_array($currentCatMerc, $catMerc) && in_array($currentCustomCat, $customcatarray) ) {include(TEMPLATEPATH.'/page_activities.php'); }
/*if( $currentCustomCat == "italian" || $currentCustomCat == "local-atmosphere"){
//It matched, do stuff here
{include(TEMPLATEPATH.'/single_activities.php'); }
}*/
else { /*include(TEMPLATEPATH.'/page.php'); */
$pagelink=get_page_link (get_page_by_title( 'Homepage' ));
header("Location: $pagelink",TRUE,301);
}
?>
And this is my page_activities.php file
<?php
if ( get_query_var('paged') ) {$paged = get_query_var('paged');}
if ( get_query_var('page') ) {$paged = get_query_var('page');}
$args = array(
'post_type' => 'activities',
'tax_query' => array(
array(
'taxonomy' => $currentCatMerc,
'field' => 'slug',
'terms' => $currentCustomCat
),
'paged' => $paged,
'posts_per_page'=>'10',
)
);
$args2 = array(
'post_type' => 'activities',
'paged' => $paged
);
query_posts( $args ); ?>
<div class="wrapper clearfix">
<div class="main">
<div class="content <?php mh_content_class(); ?>">
<?php mh_before_page_content(); ?>
<?php dynamic_sidebar('pages-1'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<!-- <?php the_content(); ?> -->
<article <?php post_class(); ?>>
<div class="loop-wrap loop-layout2">
<div class="clearfix">
<div class="loop-thumb">
<a href="<?php the_permalink(); ?>">
<?php if( get_field('business_logo') ): ?>
<?php $image = wp_get_attachment_image_src(get_field('business_logo'), 'loop'); ?>
<img src="<?php echo $image[0]; ?>" alt="<?php the_field('business_logo');?>" data-thumb="<?php echo $thumb[0]; ?>" />
<?php else: echo '<img src="' . get_template_directory_uri() . '/images/noimage_300x225.png' . '" alt="No Picture" />'; ?>
<?php endif; ?>
</a>
</div>
<div class="loop-content">
<header>
<h3 class="loop-title"><?php the_title(); ?></h3>
</header>
<?php if (get_field('business_description')): ?>
<?php $text_to_trim = get_field('business_description');
echo '<div class="mh-excerpt">'. davide_excerpt($text_to_trim) . '</div>' . "\n" ; ?>
<?php endif; ?>
<?php if (get_field('business_paying_desc')): ?>
<?php $text_to_trim = get_field('business_paying_desc');
echo '<div class="mh-excerpt">'. davide_excerpt($text_to_trim) . '</div>' . "\n" ; ?>
<?php endif; ?>
</div>
</div>
</div>
</article>
<?php dynamic_sidebar('pages-2'); ?>
<?php endwhile; ?>
<!-- <?php wp_reset_postdata(); ?> -->
<?php wp_reset_query(); ?>
<?php endif; ?>
<div class="sb-widget home-2 home-wide">
<h4 class="widget-title">Latest Updates</h4>
<ul class="cp-widget clearfix">
<?php query_posts( $args2 ); ?>
<?php if (have_posts()) : $i=1; while (have_posts() && ($i<=10)) : the_post(); ?>
<li class="cp-wrap cp-small clearfix">
<div class="cp-thumb">
<a href="<?php the_permalink(); ?>">
<?php if( get_field('business_logo') ): ?>
<?php $image = wp_get_attachment_image_src(get_field('business_logo'), 'cp_small'); ?>
<img width="70" height="53" src="<?php echo $image[0]; ?>" alt="<?php the_field('business_logo');?>" data-thumb="<?php echo $thumb[0]; ?>" class="attachment-cp_small wp-post-image" />
<?php endif; ?>
</a></div>
<div class="cp-data">
<p class="cp-widget-title"><?php the_title(); ?></p>
</div>
</li>
<?php $i++; ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<?php endif;?>
</ul>
</div>
<div class="sb-widget home-2 home-wide">
<?php echo do_shortcode('[rev_slider 620_100]'); ?>
</div>
<?php if (isset($options['comments_pages']) ? $options['comments_pages'] : false) : ?>
<section>
<?php comments_template(); ?>
</section>
<?php endif; ?>
</div>
<?php get_sidebar(); ?>
</div>
<?php mh_second_sb(); ?>
</div>
<?php get_footer(); ?>
Pls help me with this because I am really clueless.
One good news is that if I check the array with print_r($termsObjects[0]); I can see properly the output.
I guess I should use a foreach somehow, but I don't know where to put my hands on.
Thank you to anyone who can help me with this.
Regards
..your question is a bit broad...but the gist of what you need to do:
to pull the variables from the url
$query = explode('&', $_SERVER['QUERY_STRING']);
$params = array();
foreach( $query as $param ){
list($name, $value) = explode('=', $param);
$params[urldecode($name)][] = urldecode($value);
}
now you will have a params array i've no idea what your urls will look like, but say its '
postterm1 = italian, postterm2 = chinese, etc
$array['taxquery'] = array();
$array['taxquery'][relation]= 'OR';
foreach ($param as $key=>$value) {
$array['taxquery'][]=array(
'taxonomy' => 'restaurants',
'field' => 'slug',
'terms' => $value
);
}
this will build the query you see below, just delete 'tax_query' array and insert the var $array instead. It should work but i haven't tested it so play around with the format if needed.
your custom query (i think you want multi taxonomies but not all?)
Use WP_query:
$args= array(
'post_type' => 'activties',
'tax_query' => array(
'relation'=>'OR',
array(
'taxonomy' => 'restaurants',
'field' => 'slug',
'terms' => 'italian'
),
array(
'taxonomy' => 'restaurants',
'field' => 'slug',
'terms' => 'chinese'
)
),
);
ref: http://codex.wordpress.org/Class_Reference/WP_Query
Here is, index.php file from my template.
<?php get_header(); ?>
<div id="content">
<div id="content_inside">
<?php
wp_reset_query();
$get_blog_id = '-' . get_category_id('blog');
if(get_category_id('blog') != '') {
$args = array(
'post_type' => 'post',
'cat' => $get_blog_id,
'posts_per_page' => 6,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1)
);
} else {
$args = array(
'post_type' => 'post',
'posts_per_page' => 6,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1)
);
}
query_posts($args);
$x = 0;
while (have_posts()) : the_post(); ?>
<?php if($x % 2 == 0) { ?>
<div class="post_box">
<?php } else { ?>
<div class="post_box post_box_right">
<?php } ?>
<?php if ($x == 0) { ?>
<h1><?php the_title(); ?></h1>
<?php } elseif ($x == 1 || $x == 2 || $x == 3) { ?>
<h2><?php the_title(); ?></h2>
<?php } elseif ($x == 4 || $x == 5) { ?>
<h3><?php the_title(); ?></h3>
<?php } else { ?>
<h3><?php the_title(); ?></h3>
<?php } ?>
<div class="post_meta"><?php the_author(); ?> / <?php the_time('d m , Y') ?> - <?php the_time('H:i'); ?> / <?php comments_popup_link('Yorum Yok', '1 Yorum', '% Yorum'); ?></div>
<div class="img_link">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('featured-home'); ?>
<?php
//$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'featured-home' );$url = $thumb['0'];
//$titleGet = get_the_title($post->ID);
//echo '<img src="'.CDN.'/assets/images/_load.png" alt="'.$titleGet.'" data-modafesto="'.$url.'" width="301" height="398" />';
?>
</a>
</div>
<div class="post_cat">
<?php
$category = get_the_category();
echo ''.$category[0]->cat_name.'';
?> konu başlığı ile yayınlandı.
</div>
</div><!--//post_box-->
<?php if($x % 2 == 1) { ?>
<div class="clear"></div>
<?php } ?>
<?php $x++; ?>
<?php endwhile; ?>
<div class="clear"></div>
</div><!--//content_inside-->
<div class="clear"></div>
<div class="load_more_cont">
<div style="margin:0px auto;"><div class="load_more_text"><?php next_posts_link('DİĞER YAZILARI YÜKLE') ?></div></div>
</div><!--//load_more_cont-->
<?php wp_reset_query(); ?>
</div><!--//content-->
<script type="text/javascript">$(".load_more_cont a").live("click",function(a){a.preventDefault();$(".load_more_text a").html("Yazılar Yükleniyor...");$.ajax({type:"GET",url:$(this).attr("href")+"#content",dataType:"html",success:function(b){result=$(b).find("#content_inside .post_box");nextlink=$(b).find(".load_more_cont a").attr("href");$("#content_inside").append(result);$(".load_more_text a").html("Daha fazla yazı yükle");if(nextlink!=undefined){$(".load_more_cont a").attr("href",nextlink)}else{$(".load_more_cont").remove();$("#content_inside").append('<div class="clear"></div>')}}})});</script>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
I have tried to use, lazy load script for my images. Because it is a great choice for SEO Engines. That lines,
<div class="img_link">
<a href="<?php the_permalink(); ?>">
<?php
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'featured-home' );$url = $thumb['0'];
$titleGet = get_the_title($post->ID);
echo '<img src="'.CDN.'/assets/images/_load.png" alt="'.$titleGet.'" data-modafesto="'.$url.'" width="301" height="398" />';
?>
</a>
</div>
Also, here is my preloader script http://j.mp/12QkkTW .
When you load the page, first posts comes corectly with lazyload / preload but if you click load more posts button, when you get more posts the preloader doesnt work.
Can anyone help me ? Thank you.
try to add
$("XXXXXXX img").lazyload();
in ajax success callback funcion, after append html,
XXXXX is append html DOM, eq: class name or id