Limit in Characters in Function of Wordpress - php

i am using below code to display the latest post from sub-domain to domain--Here is the code i am using ---
I need to show the limit characters of post_title like to 30
Here is the code..
function render_my_recent_posts( $numberposts = 5 ) { ?>
<ul>
<?php
$args = array( 'numberposts' => '5' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ) {
echo '<li><img src="/images/default-user-avatar-3.jpg"><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a> </li> ';
}
?>
</ul><?php
}
Any help ?
Thankyou

Would substr($recent["post_title"],0,30) do what you need?

Related

Trying to call post title, excerpt, and permalink from id for shorcode

I was able to get the title and excerpt to display correctly, but I can't figure out which permalink call to use.
function display_excerpt_shortcode( $atts ) {
extract(shortcode_atts(array(
'excerptid' => ''
), $atts));
if ($excerptid) {
$args=array(
'p' => $excerptid,
'post_type' => 'post',
'post_status' => 'publish'
);
$my_query = new WP_Query($args);
if ($my_query) {
$title = apply_filters( 'the_title', $my_query->posts[0]->post_title );
$excerpt = apply_filters( 'the_excerpt', $my_query->posts[0]->post_excerpt );
$link = apply_filters( 'the_permalink', $my_query->posts[0]->post_permalink );
return '<h3>' . $title . '</h3> <p>' . $excerpt . '</p> <a class="button small primary" href="' . $link . '" title="' . $title . '" >Read More </a>';
}
}
return;
}
add_shortcode('display_excerpt', 'display_excerpt_shortcode');
I've tried all kinds of combinations. the_permalink, get_permalink, post_permalink... I just can't figure out if it's the wrong combination or if I'm just completely off the mark. Thanks in advance.
Have you tried:
$link = get_permalink( $my_query->posts[0]->post_ID )
I think your problem is that the query object has no 'permalink' property.
Following the guide in the codex's class reference page, you'll find the pattern of setting a new post object during each iteration of the loop with $the_query->the_post();:
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
//Now reference WP post functions:
the_title();
the_permalink();
}
}
Sorry it's a totally different design, but this has never failed me.

Wordpress: get & store category info

I'm wondering: is there any way I can load all the categories and store each one in a different array?
So i've got this code:
<?php
$cat_args = array('orderby' => 'name','order' => 'ASC');
$categories = get_categories($cat_args);
foreach($categories as $category) {
$args = array(
'showposts' => -1,
'category__in' => array($category->term_id),
'caller_get_posts' => 1
);
$posts = get_posts($args);
if ($posts) {
echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
foreach($posts as $post) {
setup_postdata($post);
?> <p><?php the_title(); ?></p> <?php
} // foreach($posts
} // if ($posts
} // foreach($categories
?>
I want to make it so that I can use it later like that (outside the loop):
<?php echo $category[0] -> name . $category[1] -> name . $category[2] -> name; ?>
If you just need an array of category names, you can alter your code as follows; (PLEASE NOTE:caller_get_posts and showposts has been depreciated a couple of years ago. They where replaced by ignore_sticky_posts and posts_per_page respectively)
<?php
$cat_args = array('orderby' => 'name','order' => 'ASC');
$categories = get_categories($cat_args);
$category_names = array();
foreach($categories as $category) {
$category_names[] = $category->name;
$args = array(
'posts_per_page' => -1,
'category__in' => array($category->term_id),
'ignore_sticky_posts' => 1
);
$posts = get_posts($args);
if ($posts) {
echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
foreach($posts as $post) {
setup_postdata($post);
?> <p><?php the_title(); ?></p> <?php
} // foreach($posts
} // if ($posts
} // foreach($categories
?>
$category_names will now hold an array of category names.
If you need to display a list of categories with posts below them, you should check out my post here on WPSE. Your method is very resource intensive and slow. My method is superfast, use transients, and at optimum does only 2 db queries in under 0.002 seconds
Note: You already have the data in this format in $categories variable.
So you can use anywhere like this
<?php echo $categories[0] -> name . $categories[1] -> name . $categories[2] -> name; ?>

Wordpress how to echo Category ID as class name in Recent Posts

I'm trying to retrieve a list of posts in a custom widget in wordpress and I'd like to assign each li a class name based on the category name (or slug) of each item. I think I'm close but alas my classnames are showing empty. I'm not the greatest with PHP I'm sure it's a syntax issue...here's where I'm at so far:
<ul>
<?php
$args = array( 'numberposts' => '5' );
$recent_posts = wp_get_recent_posts( $args );
$categories = get_the_category($args);
foreach( $recent_posts as $recent ){
echo '<li class="' . $categories["Post_ID"]->slug . '"><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . get_the_post_thumbnail($recent["ID"]) . $recent["post_title"].'</a> </li> ';
}
?>
</ul>
get_the_category is expecting a post_id, not an array of posts.
Try something like
$args = array( 'numberposts' => '5' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
$categories = get_the_category($recent["ID"]);
echo '<li class="' . $categories[0]->slug . '"><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . get_the_post_thumbnail($recent["ID"]) . $recent["post_title"].'</a> </li> ';
}
I'm not sure if the key name is actually "ID" or "Post_ID" (you seem to be using both). Please check that too.

Grabbing post image - Wordpress PHP

I have a function here which will grab the two most recent post and grab the image posted within those two post. The issue is though, it keeps grabbing the first image of the first post and not also the second:
Code:
function getImageFeatured($num) {
global $more;
$more = 1;
$link = get_permalink();
$content = get_the_content();
$count = substr_count($content, '<img');
$start = 0;
for($i=1;$i<=$count;$i++) {
$imgBeg = strpos($content, '<img', $start);
$post = substr($content, $imgBeg);
$imgEnd = strpos($post, '>');
$postOutput = substr($post, 0, $imgEnd+1);
$postOutput = preg_replace('/width="([0-9]*)" height="([0-9]*)"/', '',$postOutput);;
$image[$i] = $postOutput;
$start=$imgEnd+1;
}
if(stristr($image[$num],'<img')) { echo ''.$image[$num].""; }
$more = 0;
}
$args = array( 'numberposts' => '2' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . getImageFeatured('1') .'</a></li> ';
}
Not to sure why It is only sending out the first image of the first post.
Suggestion and thoughts appreciated.
EDIT:
With Revision:
.....above function I posted....
global $post;
$args = array( 'numberposts' => '2' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $post ){
setup_postdata( $post );
echo '<li><a href="' . get_permalink() . '" title="Look '.get_the_title_attribute().'" >' . getImageFeatured('1') .'</a></li> ';
}
wp_reset_postdata();
This does not work either for me. It seems if I use the setup_postdat... it wont process but if I remove it it goes through but does not do what I ask, but does what I stated the problem to be.
get_the_content is getting the content of the post in the loop. You may run the function on both posts but it still gets the content of the post that's in the loop.
The function you've written needs some serious improvement but for the sake of getting it working in the quickest manner possible replace the existing foreach loop with:
global $post;
foreach( $recent_posts as $post ){
setup_postdata( $post );
echo '<li><a href="' . get_permalink() . '" title="Look '.get_the_title_attribute().'" >' . getImageFeatured('1') .'</a></li> ';
}
wp_reset_postdata();

Custom shortcode always appears at top of content

I have created a custom shortcode and can get the information to output, however it does not show up where I have placed it in the content hierarchy - it always prints at the top of the post/page. Any clue as to why this may be happening?
in my functions.php:
function sc_pdf($atts, $content = null) {
$pdfname = the_field('pdf_title');
$pdfimage = the_field('pdf_file');
$pdflink = the_field('pdf_thumbnail');
return '<p>'.$pdfname.'</p><p>'.$pdfimage.'</p><p>'.$pdflink.'</p>';
}
add_shortcode("peedeef", "sc_pdf");
Since you are using the_field method, I assume you use ACF plugin.
You should use get_field instead of the_field since the_field will output the specified field.
function sc_pdf($atts, $content = null) {
$pdfname = get_field('pdf_title');
... etc
Always use "return" instead echo.
You'll able to get data in proper location.
To move your shortcode around, don't use echo.
if you place the shortcode in your doc in the first example it will always float to the top.
in the second if I place it at the bottom it will appear at the bottom.
My shortcode is [showpod]
CODE THAT YOU CANT PLACE ANYWHERE
function makepod($atts) {
echo "<div class='podmysqlarray2 showpodholder'><h3 class='widget-title newposts'>Latest Snippets</h3>";
$args = array( 'numberposts' => '6' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<div class="pod"><li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a> </li> </div>';
}
echo "</div>";
}
add_shortcode('showpod', 'makepod');
AND NOW REVISED CODE YOU CAN PLACE ANYWHERE: -
function makepod($atts) {
$cat = "<div class='podmysqlarray2 showpodholder'><h3 class='widget-title newposts'>Latest Snippets</h3>";
$args = array( 'numberposts' => '6' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
$cat.= '<div class="pod"><li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a> </li> </div>';
}
$cat .= "</div>";
return $cat;
}
add_shortcode('showpod', 'makepod');

Categories