My loop looks like this:
<!-- loop for the posts here -->
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 3,
'category_name' => 'news'
);
$query = new WP_Query($args);
while($query->have_posts()) : $query->the_post();
?>
<div class="news_box_content">
<h5><?php the_title(); ?></h5>
<figure><?php the_post_thumbnail(); ?></figure>
<?php if($post->post_excerpt) { ?>
<p><?php echo get_the_excerpt(); ?></p>
Read more...
<?php } else {
the_content('Read More');
} ?>
</div>
<?php endwhile; wp_reset_postdata(); ?>
I used a function to count the excerpt length but it is not working.
function custom_excerpt_length(){
return 10;
}
add_filter('excerpt_length', 'custom_excerpt_length');
How can I limit the number of characters on any excerpt?
No need for an extra filter
echo substr(get_the_excerpt(), 0,10);
Try this one. I always use this one
<?php
$content = get_the_content();
$content = strip_tags($content);
$dots = strlen(substr($content,0,50)) < 50 ? " " : "...";
echo substr($content,0,50) . $dots;
?>
Add this to your functions.php file:
function trim_excerpt( $exc ) {
$exc_trimmed = substr( $exc, 0, 150 ) . '...';
return $exc_trimmed;
}
add_filter( 'the_excerpt', 'trim_excerpt', 999 );
Related
I'm trying to get all related posts on a single template. Posts should have at least one matching tag.
$post_id = $GLOBALS['wp_the_query']->get_queried_object_id();
$tags = get_the_tags( $post_id );
if ($tags && !is_wp_error($tags)) {
$tag_ids = array();
foreach ($tags as $tag) {
$tag_ids[] = $tag->term_id;
}
$args = array (
'tag__in' => $tag_ids,
'post__not_in' => [$post_id]
);
$related_tags_posts = new WP_Query( $args );
wp_reset_postdata();
}
Here is where I'm printing the posts.
<?php while ($related_tags_posts->have_posts()) : $related_tags_posts->the_post() ?>
<?php the_title(); ?>
<?php endwhile; wp_reset_postdata(); ?>
But it's printing just one post from each matching tag. How do I print ALL posts that have matching tags? Not sure what I'm missing.
Please have a look below:
<?php
$tags = wp_get_post_tags($post->ID);
if ($tags) {
echo 'Related Posts';
$first_tag = $tags[0]->term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'posts_per_page'=>5,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
<?php endwhile;
}
wp_reset_query();
}
I hope it would help you out.
i have a Wordpress page called Jobs (https://www.bap.cc/jobs/).
There i want to display two different posttypes (Jobs & Jobsangbote).
Everything works well, except the active function in my php. The first block “Wir suchen“ is okay, “Wir bieten“ active function doesn't work. Don't know why...
<?php
get_header(); ?>
<div class="overflow-y ios blur"><article class="article barba- container"><div id="agenturtext" class="text overflow firefox">
<?php
// Start the loop.
while ( have_posts() ) : the_post();
echo '<header>';
the_title('<h1 id="agenturheader">','</h1>');
echo '</header>';
the_content();
// Include the page content template.
//get_template_part( 'content', 'page' );
// End the loop.
endwhile;
?>
</div>
<div id="float" class="overflow jobs firefox">
<div class="agenturbild">
<?php
$id=get_the_ID();
$thumbnail_id=get_post_thumbnail_id($id);
$tablet=wp_get_attachment_image_src($thumbnail_id, 'tablet')[0];
$srcset=wp_get_attachment_image_srcset($thumbnail_id);
$alt=get_post_meta( $thumbnail_id, '_wp_attachment_image_alt', true );
echo '<img src="' . $tablet . '" srcset="' . $srcset . '" alt="' . ($alt?$alt:' ') . '" class="lazyload"></div>' /*sizes="(max-width: 920px) 33vw, (max-width: 1200px) 50vw, (max-width: 1500px) 60vw, (max-width:2000px) 70vw, 80vw"*/; ?>
<div id="jobmenu">
<?php $args = array( 'post_type' => 'jobs', 'posts_per_page' => -1, 'orderby'=>'date', 'order'=>'DESC' );
$loop = new WP_Query( $args );
$a=0;
if ($loop->found_posts>1){
echo '
<div class="jobueberschrift"><h2 class="jobtitel">Wir suchen:</h2></div>';
$ID=get_the_ID();
while ( $loop->have_posts() ) : $loop->the_post();
echo '<div>';the_title('<h2>','</h2>');echo '</div>';
// Include the page content template.
get_template_part( 'content', 'page' );
// End the loop.
endwhile;
}
wp_reset_query();
?>
<?php $argsangebote = array( 'post_type' => 'jobsangebote', 'posts_per_page' => -1, 'orderby'=>'date', 'order'=>'DESC' );
$loop = new WP_Query( $argssangebote );
$a=0;
if ($loop->found_posts>1){
echo '
<div class="jobueberschrift" style="margin-top:52px !important;"> <h2 class="jobtitel">Wir bieten:</h2></div>';
$ID=get_the_ID();
while ( $loop->have_posts() ) : $loop->the_post();
echo '<div>';the_title('<h2>','</h2>');echo '</div>';
// Include the page content template.
get_template_part( 'content', 'page' );
// End the loop.
endwhile;
}
?>
</div>
</article></div>
<?php get_footer(); ?>
In a wordpress website showing 3 post per row and almost unlimited rows due to ajax autoload for pagination.
What is the best way to add say for example a custom HTML code instead of post every 5th post?
You will need to add a count to the loop that is pulling the posts into the rows.
for example:
<?php $args = array(
'post_type' => 'posts',
'posts_per_page' => -1
);
$posts = get_posts( $args );
if ( $posts ) :
$temp_post = $post; ?>
<div class="row">
<?php $count = 1; foreach ( $posts as $post ) : setup_postdata( $post ); ?>
<h1><?php the_title(); ?></h1>
<?php //This count adds the custom html mark usful if its only a sinle line of markup.
echo ( $count % 5 == 0 ) ? '<h2 class="custom-html-markup">Custom Markup</h2>' : ''; ?>
<?php // If the markup is longer than a single line.
if ( $count % 5 == 0 ) : ?>
<div class="custom-html-markup">
<?php the_excerpt(); ?>
</div>
<?php endif; ?>
<?php // This count closes and re-opens the rows
echo ( $count % 3 == 0 ) ? '</div><div class="row">' : ''; ?>
<?php $count++; endforeach; ?>
</div>
<?php $post = $temp_post;
endif; ?>
I'm using below code to filter the query on page and sort the post in ascending order by price. I'm getting desire output but my concern is , i want all price with 0 at last.
<?php
global $wp_query;
$args = array_merge( $wp_query->query_vars, array( 'orderby' => 'meta_value_num','meta_key' => 'price','order' => 'ASC'));
query_posts( $args );
?>
please help me guys...Thanks in advance
full code here:
<?php
/**
* The template for displaying Archive pages.
*
* Learn more: http://codex.wordpress.org/Template_Hierarchy
*
* #package progression
*/
get_header('cars'); ?>
<?php get_template_part( 'page', 'vehicle-title' ); ?>
<div class="width-container">
<?php if ( have_posts() ) : ?>
<div id="page-title">
<h1 id="page-heading">
<?php if (is_tax('vehicle_type')) {
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$tax_term_breadcrumb_taxonomy_slug = $term->taxonomy;
echo '' . $term->name . '';
}
?>
</h1>
</div><!-- close #page-title -->
<div id="4-content-container" class="tax-vehicle-container">
<?php echo do_shortcode('[vehicle_searchform include="make,model"]') ?>
<?php
global $wp_query;
$args = array_merge( $wp_query->query_vars, array( 'orderby' => 'meta_value_num','meta_key' => 'price','order' => 'ASC'));
query_posts( $args );
?>
<?php
/* Start the Loop */
$count = 1;
$count_2 = 1;
?>
<?php while ( have_posts() ) : the_post();
if($count >= 5) { $count = 1; }
?>
<div class="grid4column-progression <?php if($count == 4): echo ' lastcolumn-progression'; endif; ?>">
<?php
get_template_part( 'content', 'vehicle');
?>
</div>
<?php if($count == 4): ?><div class="clearfix"></div><?php endif; ?>
<?php $count ++; $count_2++; endwhile; ?>
<div class="clearfix"></div>
<?php show_pagination_links( ); ?>
</div><!-- close #content-container -->
<?php else : ?>
<?php get_template_part( 'no-results', 'inventory' ); ?>
</div><!-- close #content-container -->
<?php endif; ?>
<?php get_sidebar( 'vehicle' ); ?>
</div><!-- close .width-container -->
<?php get_footer(); ?>
If you've got a small number of posts, the quick and dirty thing to do would be to traverse the loop twice, suppressing zeros on the first pass and suppressing everything except zeros on the second pass:
for ($i=0, $i++, $i<=1) :
while (have_posts()) : the_post();
$show_non_zeros = ($i === 0) && ( get_post_meta( get_the_ID(), 'price', true) > 0);
$show_zero_price = ($i === 1) && ( get_post_meta( get_the_ID(), 'price', true) == 0);
if ($show_non_zeros || $show_zero_price) {
//write output
}
end while;
end for;
If you're looking for a faster / more elegant solution, you can usort() $posts between your call to query_posts and the loop.
hope this will be useful to someone.. i done this way
<?php
/* ADDED by for sorting ASCENDING order but call for price(ie price with 0) last */
function my_sort_custom( $orderby, $query ){
global $wpdb;
$orderby = " case when CAST(meta_value AS SIGNED) = '0' then 1 else 0 end ,CAST(meta_value AS SIGNED) ";
return $orderby;
}
add_filter('posts_orderby','my_sort_custom',10,2);
global $wp_query;
$args = array_merge( $wp_query->query_vars, array('meta_key' => 'price'));
query_posts( $args );
?>
I have to get specific page content (like page(12))
I used that :
<?php $id=47; $post = get_page($id); echo $post->post_content; ?>
Work nice execpt for compatibility with translations, it returns both French and English text
But the loop is fine, return only the good language version
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<div id="post">
<?php the_content(); ?>
</div> <!-- .post -->
So the question.... HOW to get a specific page content inside the loop...
I've answered my own question. Call apply_filter and there you go.
<?php
$id=47;
$post = get_post($id);
$content = apply_filters('the_content', $post->post_content);
echo $content;
?>
get page content by page name:
<?php
$page = get_page_by_title( 'page-name' );
$content = apply_filters('the_content', $page->post_content);
echo $content;
?>
I used this:
<?php echo get_post_field('post_content', $post->ID); ?>
and this even more concise:
<?= get_post_field('post_content', $post->ID) ?>
A simple, fast way to get the content by id :
echo get_post_field('post_content', $id);
And if you want to get the content formatted :
echo apply_filters('the_content', get_post_field('post_content', $id));
Works with pages, posts & custom posts.
Just only copy and paste this code it will get your page content.
<?php
$pageid = get_the_id();
$content_post = get_post($pageid);
$content = $content_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
?>
The wp_trim_words function can limit the characters too by changing the $num_words variable. For anyone who might find useful.
<?php
$id=58;
$post = get_post($id);
$content = apply_filters('the_content', $post->post_content);
$customExcerpt = wp_trim_words( $content, $num_words = 26, $more = '' );
echo $customExcerpt;
?>
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'prev_text' >' Previous','post_type' => 'page', 'posts_per_page' => 5, 'paged' => $paged );
$wp_query = new WP_Query($args);
while ( have_posts() ) : the_post();
//get all pages
the_ID();
the_title();
//if you want specific page of content then write
if(get_the_ID=='11')//make sure to use get_the_ID instead the_ID
{
echo get_the_ID();
the_title();
the_content();
}
endwhile;
//if you want specific page of content then write in loop
if(get_the_ID=='11')//make sure to use get_the_ID instead the_ID
{
echo get_the_ID();
the_title();
the_content();
}
One liner:
<? if (have_posts()):while(have_posts()): the_post(); the_content(); endwhile; endif; ?>