I'm using this function to split the content
function split_content() {
global $more;
$more = true;
$content = preg_split('/<span id="more-\d+"><\/span>/i', get_the_content('more'));
for($c = 0, $csize = count($content); $c < $csize; $c++) {
$content[$c] = apply_filters('the_content', $content[$c]);
}
return $content;
}
Now I'm using a shortcode to display an icon, the problem is that no mater where i put it in the content, it always is displayed before the split content function, is the're a way to do the shortcode after the split content function?
The shortcode function
add_shortcode( 'ikona', 'add_device_icon' );
function add_device_icon($attr){
global $post;
$icons = array(0=>'wybierz',1=>'piekarnik',2=>'frytkownica',3=>'mikrofalówka',4=>'patelnia',5=>'garnek',6=>'grill',7=>'opiekacz');
$icon_number = get_post_meta( $post->ID, 'product-icon-type', true );
extract( shortcode_atts( array( 'do' => ''), $atts ) );
$style='';
if($do!=''){
$style = 'style="float:left"';
}
echo "<span class='icon-$icon_number' title='{$icons[$icon_number]}' $style></span>";
}
The content is displayed in the wordpress loop
<div class='holder'>
<h3><?php the_title() ?></h3>
<?php $content = split_content() ?>
<?php echo $content[0] ?>
<?php if(count($content )> 1 ) :?>
zobacz więcej…
<?php endif ?>
</div>
<div class="more">
<?php echo $content[1] ?>
</div>
Related
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 );
Im using "insert pages" plugin for wordpress that allows me to insert pages and posts into other pages and posts from the backend. My problem being that when i i use "insert pages" plugin in my news section and then visit the site it shows the shortcode instead of the content. This seems to be a problem only when making a "post". If i use the plugin to display a page within another page it works fine (shows the content not the shortcode).
Any idea what could be the cause of this problem?
Cheers,
Emil
Update
// Shortcode hook: Replace the [insert ...] shortcode with the inserted page's content
function insertPages_handleShortcode_insert( $atts, $content = null ) {
global $wp_query, $post, $wp_current_filter;
extract( shortcode_atts( array(
'page' => '0',
'display' => 'all',
), $atts ) );
// Validation checks.
if ( $page === '0' ) {
return $content;
}
// Trying to embed same page in itself.
if ( $page == $post->ID || $page == $post->post_name ) {
return $content;
}
// Don't allow inserted pages to be added to the_content more than once (prevent infinite loops).
$done = false;
foreach ( $wp_current_filter as $filter ) {
if ( 'the_content' == $filter ) {
if ( $done ) {
return $content;
} else {
$done = true;
}
}
}
// Get page object from slug or id
$temp_query = clone $wp_query; // we're starting a new loop within the main loop, so save the main query
$temp_post = $wp_query->get_queried_object(); // see: http://codex.wordpress.org/The_Loop#Multiple_Loops_Example_2
// Convert slugs to page IDs to standardize query_posts() lookup below.
if ( ! is_numeric( $page ) ) {
$page_object = get_page_by_path( $page, OBJECT, get_post_types() );
$page = $page_object ? $page_object->ID : $page;
}
if ( is_numeric( $page ) ) {
$args = array(
'p' => intval( $page ),
'post_type' => get_post_types(),
);
} else {
$args = array(
'name' => esc_attr( $page ),
'post_type' => get_post_types(),
);
}
query_posts( $args );
// Start our new Loop
while ( have_posts() ) {
ob_start(); // Start output buffering so we can save the output to string
// Show either the title, link, content, everything, or everything via a custom template
// Note: if the sharing_display filter exists, it means Jetpack is installed and Sharing is enabled;
// This plugin conflicts with Sharing, because Sharing assumes the_content and the_excerpt filters
// are only getting called once. The fix here is to disable processing of filters on the_content in
// the inserted page. #see https://codex.wordpress.org/Function_Reference/the_content#Alternative_Usage
switch ( $display ) {
case "title":
the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php break;
case "link":
the_post(); ?>
<?php the_title(); ?>
<?php break;
case "excerpt":
the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php if ( has_filter( 'the_content', 'sharing_display' ) ) echo get_the_excerpt(); else the_excerpt(); ?>
<?php break;
case "excerpt-only":
the_post(); ?>
<?php if ( has_filter( 'the_content', 'sharing_display' ) ) echo get_the_excerpt(); else the_excerpt(); ?>
<?php break;
case "content":
the_post(); ?>
<?php if ( has_filter( 'the_content', 'sharing_display' ) ) echo get_the_content(); else the_content(); ?>
<?php break;
case "all":
the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php if ( has_filter( 'the_content', 'sharing_display' ) ) echo get_the_content(); else the_content(); ?>
<?php the_meta(); ?>
<?php break;
default: // display is either invalid, or contains a template file to use
$template = locate_template( $display );
if ( strlen( $template ) > 0 ) {
include $template; // execute the template code
} else { // Couldn't find template, so fall back to printing a link to the page.
the_post(); ?>
<?php the_title(); ?><?php
}
break;
}
$content = ob_get_contents(); // Save off output buffer
ob_end_clean(); // End output buffering
}
wp_reset_postdata();
$wp_query = clone $temp_query; // Restore main Loop's wp_query
$post = $temp_post;
$content = "<div data-post-id='$page' id='insertPages_Content'>$content</div>";
return $content;
//return do_shortcode($content); // careful: watch for infinite loops with nested inserts
}
This is could be work for you.
while ( have_posts() ) {
ob_start(); // Start output buffering so we can save the output to string
// Show either the title, link, content, everything, or everything via a custom template
// Note: if the sharing_display filter exists, it means Jetpack is installed and Sharing is enabled;
// This plugin conflicts with Sharing, because Sharing assumes the_content and the_excerpt filters
// are only getting called once. The fix here is to disable processing of filters on the_content in
// the inserted page. #see https://codex.wordpress.org/Function_Reference/the_content#Alternative_Usage
switch ( $display ) {
case "title":
the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php break;
case "link":
the_post(); ?>
<?php the_title(); ?>
<?php break;
case "excerpt":
the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php if ( has_filter( 'the_content', 'sharing_display' ) ) echo get_the_excerpt(); else the_excerpt(); ?>
<?php break;
case "excerpt-only":
the_post(); ?>
<?php if ( has_filter( 'the_content', 'sharing_display' ) ) echo get_the_excerpt(); else the_excerpt(); ?>
<?php break;
case "content":
the_post(); ?>
<?php if ( has_filter( 'the_content', 'sharing_display' ) ) echo do_shortcode( get_the_content() ); else the_content(); ?>
<?php break;
case "all":
the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php if ( has_filter( 'the_content', 'sharing_display' ) ) echo do_shortcode( get_the_content() ); else the_content(); ?>
<?php the_meta(); ?>
<?php break;
default: // display is either invalid, or contains a template file to use
$template = locate_template( $display );
if ( strlen( $template ) > 0 ) {
include $template; // execute the template code
} else { // Couldn't find template, so fall back to printing a link to the page.
the_post(); ?>
<?php the_title(); ?><?php
}
break;
}
I am using get_template_part() to dynamically insert wordpress pages. I have (m)qTranslate installed but I can not get any other language than the default one with get_template_part(). The __() and _e() are also not translated.
get_template_part() code:
$q_config['language'] = $_POST['lang'];
$post_id = $_POST['postId'];
$page_data = get_post( $post_id );
ob_start();
get_template_part( 'page-template/page', $page_data->post_name );
$output .= ob_get_clean();
ob_end_flush();
template code example:
<div>
<?php $the_query = new WP_Query( 'posts_per_page=50' ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<div id="isotope-list">
<?php while ( $the_query->have_posts() ) : $the_query->the_post();
if (in_category( get_category_by_slug( 'designs' )->term_id )) {
$termsArray = get_the_terms( $post->ID, "category" );
$termsString = "";
foreach ( $termsArray as $term ) {
$termsString .= $term->slug.' ';
}
?>
<div class="<?php echo $termsString; ?> item">
<span class="fs-<?php echo $post->ID; ?> ?>"></span>
<div><?php the_title();?></div>
<div class="thumbmail">
<?php
$thumb_url_array = wp_get_attachment_image_src(get_post_thumbnail_id(), 'thumbnail-size', true);
$thumb_url = $thumb_url_array[0];
echo '<img src="'.$thumb_url.'"/>';
?>
</div>
<span class="description name"><?php _e( 'Click to see more', 'sp' ); ?></span>
</a>
</div>
<?php }
endwhile; ?>
</div>
<?php endif; ?>
</div>
In function.php I have:
load_child_theme_textdomain('sp', get_stylesheet_directory().'/languages');
Does anybody know if get_template_part() can get the localization and how?
I have just replaced the mqtranslate (deprecated) plugin with qtranslate-X and the translation is working fine again with get_template_part().
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; ?>
Here's the situation:
In wordpress I'm trying to reset a post WP_Query so that I can rewrite the post link based on whether or not a custom field exists in the post. I'm trying to give the post a NEW link in the custom field.
All I've managed to do here is kill the link entirely. Any and all help is greatly appreciated, I'm pretty green to php.
Here's my WP_Query:
<?php
$recentPosts = new WP_Query();
$recentPosts->query('showposts=3');
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<?php
$attribute = the_title_attribute();
$title = the_title();
$key = 'NewPostLink';
$newLink = get_post_meta( $post->ID, $key, TRUE );
if ($newLink != '') {
$theLink = get_permalink ($post->ID );
if (has_post_thumbnail()) {
$image = get_the_post_thumbnail( $post->ID );
echo '<div class="thumbnailbox"><div class="thumbnail">'.$image.'</div></div>';
echo '<h2>'.$title.'</h2>';
} else {
echo '<h2>'.$title.'</h2>';
}
} else {
$theLink = $newLink;
if (has_post_thumbnail()) {
$image = get_the_post_thumbnail( $post->ID );
echo '<div class="thumbnailbox"><div class="thumbnail">'.$image.'</div></div>';
echo '<h2>'.$title.'</h2>';
} else {
echo '<h2>'.$title.'</h2>';
}
}
?>
<small><?php the_time('F jS, Y') ?></small>
<div class="entry">
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>
I think this is what you need. It's hard to tell. I suppose that the first part of the if statement is what runs if there is no custom post meta? I couldn't tell. Here's what the problem was. The if statement ran the first part if there IS a value returned for the custom post meta, otherwise it ran the second part, using the empty string as the href. (The first part runs if the custom value either doesn't exist or is anything but an empty string). Changing the if statement to check if it's empty is better because it will catch it if it doesn't exist (returns false), or if it does exist but is an empty string (declared but not defined).
I've marked what I edited with comments (just one line).
<?php
$recentPosts = new WP_Query();
$recentPosts->query('showposts=3');
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<?php
$attribute = the_title_attribute();
$title = the_title();
$key = 'NewPostLink';
$newLink = get_post_meta( $post->ID, $key, TRUE );
/* EDITED */ if (empty($newLink)) {
$theLink = get_permalink ($post->ID );
if (has_post_thumbnail()) {
$image = get_the_post_thumbnail( $post->ID );
echo '<div class="thumbnailbox"><div class="thumbnail">'.$image.'</div></div>';
echo '<h2>'.$title.'</h2>';
} else {
echo '<h2>'.$title.'</h2>';
}
} else {
$theLink = $newLink;
if (has_post_thumbnail()) {
$image = get_the_post_thumbnail( $post->ID );
echo '<div class="thumbnailbox"><div class="thumbnail">'.$image.'</div></div>';
echo '<h2>'.$title.'</h2>';
} else {
echo '<h2>'.$title.'</h2>';
}
}
?>
<small><?php the_time('F jS, Y') ?></small>
<div class="entry">
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>