Proper way to get page content - wordpress

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; ?>

Related

Show current taxonomy name - Wordpress [duplicate]

I am creating a new template that will get all the custom post type (Case Studies) content, including the taxonomies values associated with it.
So far I got the following:
<section>
<h1><?php _e( 'posts', 'casestudies' ); ?></h1>
<?php get_template_part('loop'); ?>
<?php
$args = array('post_type' => 'casestudies', 'posts_per_page' => 3);
$query = new WP_Query($args);
while($query -> have_posts()) : $query -> the_post();
?>
<h2><?php the_title(); ?></h2>
<p>Meta: <?php the_meta(); ?></p>
<p>Excerpt: <?php the_excerpt(); ?></p>
<p>what_to_put_here_to_get_taxonomies_values????</p>
<?php endwhile; ?>
<?php get_template_part('pagination'); ?>
</section>
How do I get the taxonomy of it? I have tried multiple things but all seemed to fail and just getting more confused.
Check this function: wp_get_post_terms()
Assuming your custom post type Case Study supports two taxonomies called country and subject, you can try something like this:
<?php $terms = wp_get_post_terms( $query->post->ID, array( 'country', 'subject' ) ); ?>
<?php foreach ( $terms as $term ) : ?>
<p><?php echo $term->taxonomy; ?>: <?php echo $term->name; ?></p>
<?php endforeach; ?>
Your output would be something like:
Country: United Kingdom
Subject: Biology
Subject: Chemistry
Subject: Neurology
assume: I register a taxonomy with custom post type name publication_category.
On your custom post type template write :
$terms = get_the_terms( $post->ID, 'publication_category' );
if ($terms) {
foreach($terms as $term) {
echo $term->name;
}
}
Have you tried using <?php get_taxonomies() ?> ?
If your looking for specific taxonomies that function has optional arguments you can pass in to control the output. See documentation here : http://codex.wordpress.org/Function_Reference/get_taxonomies
Just in case if it could help someone, I used "the_taxonomies()" function inside a loop of a custom post type.
<?php
while ( have_posts() ) : the_post();
$custom_post = get_post_meta( get_the_ID() );
//
?>
//html
//and stuff
<?php the_taxonomies(); ?>
<?php
endwhile;
?>
the result was:
Taxonomy-name: {Taxonomy-term}. <-- as a link

Display custom post type categories (terms) in loop

I am using the following code to load all the posts from a custom post type, in this loop I show a title but I also want to show the categories (terms) that are connected to this particular post but I can't seem to make it work
Loop:
<?php $args = array( 'post_type' => 'fotoalbum', 'showposts'=> '-1' ); $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php $i=1; ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if($i==1 || $i%3==1) echo '<div class="row">' ;?>
<div class="col-md-4">
<?php the_title();?><br/>
! HERE I WANT THIS POSTS CATEGORY !
</div>
<?php if($i%3==0) echo '</div>';?>
<?php $i++; endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
I tried:
<?php echo $term->name; ?>
You need to use get_the_terms() for getting category
you can get this by below code...
you need to put second argument a custom post-type's category slug
you can refer this link https://developer.wordpress.org/reference/functions/get_the_terms/
<?php $args = array( 'post_type' => 'fotoalbum', 'showposts'=> '-1' ); $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php $i=1; ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if($i==1 || $i%3==1) echo '<div class="row">' ;?>
<div class="col-md-4">
<?php the_title();?><br/>
! HERE I WANT THIS POSTS CATEGORY !
<?php
$terms = get_the_terms( get_the_ID(), 'category-slug' ); // second argument is category slug of custom post-type
if(!empty($terms)){
foreach($terms as $term){
echo $term->name.'<br>';
}
}
?>
</div>
<?php if($i%3==0) echo '</div>';?>
<?php $i++; endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>

WordPress excerpt character count not working

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 );

how to use Custom post type dynamically

I have created a new template page and I am displaying custom post type in that page as follows,
<div class="col-sm-4">
<?php $i = 1 ?>
<?php $posts = get_posts(array(
'post_type' => 'astroalbums',
'posts_per_page' => -1
));
foreach ($posts as $post) : start_wp(); ?>
<?php if ($i == 1): ?>
<?php $link = get_permalink($post->ID); ?>
<?php the_title( '<h3 class="entry-title">', '</h3>' );?>
<?php the_post_thumbnail(); ?>
<?php endif; ?>
<?php if($i == 3){$i = 1;} else {$i++;} ?>
<?php endforeach; ?>
My custom post type is "astroalbums" and I want to use it dynamically. I have 4 custom post types. I want to create new page in dashboard and assign the above page template i have created. and each page will call different custom post type.
It will be really great help
Thank you,
Trupti
You're retrieving the posts correctly, but it's seems that the problem is inside the foreach loop. As the default WordPress loop is not being used, you need to call functions which receive the post id as parameter or use the properties present in the $post object (which is an instance of the WP_POST class) in order to display the data.
One possible solution:
<?php
$posts = get_posts([
'post_type' => 'astroalbums',
'posts_per_page' => 1
]);
?>
<?php foreach( $posts as $post ): ?>
<?php $link = get_permalink( $post->ID ); ?>
<h3 class="entry-title">
<a href="<?php echo esc_url( $link ); ?>" rel="bookmark">
<?php echo get_the_title( $post->ID ); ?>
</a>
</h3>
<a href="<?php echo esc_url( $link ); ?>">
<?php echo get_the_post_thumbnail( $post->ID ); ?>
</a>
<?php endforeach; ?>

Is get_template_part() taking into account localization?

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().

Resources