Different "Read more" in different excerpts on the same page - php

I want two differents excerpts on my homepage, one for the posts inside the loop, the other one for the latest post published, outside the loop.
I managed to add the excerpt on the latest post outside of the loop like this
<?php $postslist = get_posts('numberposts=1&order=DESC&orderby=post_date');
foreach ($postslist as $post) :
setup_postdata($post); ?>
<?php wp_latest_excerpt('wp_latest', 'excerpt_more_latest'); ?>
<?php endforeach; ?>
While the excerpt for the posts inside the loop is
<?php wp_excerpt('wp_index'); ?>
For some reasons I am able to set the excerpt length differently for both of my excerpts, but the "more" stays the same. (both "more" have the class .view-article)
I thought I could create another excerpt with a different "more" function, but it does not work, here are my 2 different excerpts and more functions.
For the length
function wp_index($length)
{
return 21;
}
function wp_latest($length)
{
return 18;
}
For the "more"
function view_article($more)
{
return '... </br><a class="view-article" href="' . get_permalink($post->ID) . '">' . __('Continue Reading', '') . '</a>';
}
function latest_view_article($more)
{
return '... </br><a class="view-latest-article" href="' . get_permalink($post->ID) . '">' . __('Continue Reading', '') . '</a>';
}
add_filter('excerpt_more_latest', 'latest_view_article');
add_filter('excerpt_more', 'view_article');
And finally the excerpts
function wp_latest_excerpt($length_callback = '', $more_callback = '')
{
if (function_exists($length_callback)) {
add_filter('excerpt_length', $length_callback);
}
if (function_exists($more_callback)) {
add_filter('excerpt_more_latest', $more_callback);
}
$output = get_the_excerpt();
$output = apply_filters('wptexturize', $output);
$output = apply_filters('convert_chars', $output);
$output = '<p>' . $output . '</p>';
echo $output;
}
function wp_excerpt($length_callback = '', $more_callback = '')
{
if (function_exists($length_callback)) {
add_filter('excerpt_length', $length_callback);
}
if (function_exists($more_callback)) {
add_filter('excerpt_more', $more_callback);
}
$output = get_the_excerpt();
$output = apply_filters('wptexturize', $output);
$output = apply_filters('convert_chars', $output);
$output = '<p>' . $output . '</p>';
echo $output;
}
Is this a wrong way to achieve this or did I make some mistakes in my code ?

Related

Get category title with shortcode

i'd like to create a shortcode for wordpress to display the category name and i found this code :
function categories_list_func( $atts ){
$categories = get_the_category();
if($categories) {
foreach($categories as $category) {
$output .= '<li class="cat-' . $category->cat_ID . '">'.$category->cat_name.'</li>';
}
$second_output = trim($output);
}
$return_string = '<h4>'.__( "Categories :", "my_site").'</h4><div class="overflow"><ul class="post-categories">' . $second_output . '</ul></div>';
return $return_string;
} // END Categories
add_shortcode( 'categories-list', 'categories_list_func' );
But i know nothing in PHP and i'd like to remove everything (the link and "Catégorie :" in h4) except the category name.
I managed to do this with CSS but i'd like a clean php code if possible.
Could you help me please ?
Thanks.
Nicolas.
this is the code you want with link to categories :
<?php
function categories_list_func( $atts ){
$categories = get_the_category();
if($categories) {
foreach($categories as $category) {
$output .= '<li class="cat-' . $category->cat_ID . '">'.$category->cat_name.'</li>';
}
$second_output = trim($output);
}
$return_string = '<ul>' . $second_output . '</ul>';
return $return_string;
} // END Categories
add_shortcode( 'categories-list', 'categories_list_func' );
?>
and this is only the names without any listing and any links
function categories_list_func( $atts ){
$categories = get_the_category();
if($categories) {
foreach($categories as $category) {
$output .= '<li>'.$category->cat_name.'</li>';
}
$second_output = trim($output);
}
$return_string = '<ul>' . $second_output . '</ul>';
return $return_string;
} // END Categories
add_shortcode( 'categories-list', 'categories_list_func' );
or if you want it in another way dont hesitate to tell me :)

Wordpress shortcode shows as plain text in one view, works in other

I have this complex loop:
<?php
$args = array(
'cat' => 54,
'order' => 'ASC',
'posts_per_page' => -1
);
$query = new WP_Query($args);
$q = array();
while ( $query->have_posts() ) {
$query->the_post();
$a = '<h2>' . get_the_title() .'</h2>'
. get_the_post_thumbnail() .
'<p>' . get_the_content("...plačiau") . '</p>';
$categories = get_the_category();
foreach ( $categories as $key=>$category ) {
$b = '<h1 class="thetitle">' . $category->name . '<span>Išskleisti <i class="fa fa-arrow-circle-down"></i></span></h1>';
}
$q[$b][] = $a; // Create an array with the category names and post titles
}
/* Restore original Post Data */
wp_reset_postdata();
foreach ($q as $key=>$values) {
echo $key;
echo '<div class="straipsniai">';
foreach ($values as $value){
if (count($values) == 1) {
echo '<div class="vienas">' . $value . '</div>';
} else if (count($values) == 2) {
echo '<div class="du">' . $value . '</div>';
} else if (count($values) == 3) {
echo '<div class="trys">' . $value . '</div>';
} else {
echo '<div>' . $value . '</div>';
}
}
echo '</div>';
}
?>
Which worked for me, gave me this nice list/accordion:
http://bruzienesklinika.lt/web/gydytojai/
Now, each person in the category has some articles as posts and I want a list of their articles under their description. (basic Title + exerpt + read more link)
I've tried to do this by using "List category posts" plugin which allows me to use [catlist id=24] shortcode, but the problem is that browser prints it as plain text, source shows [catlist id=24](You can open the most bottom "GYDYTOJA REUMATOLOGĖ" tab to see that). The shortcode does work inside page, which is rendered by single.php, but it does not show when rendered in the loop I gave you in the beginning of the question.
SO, the question is, how do I make the shortcode work in the initial list, where all the categories are listed with posts inside the accordion.
Now it is not the problem of this certain plugin because no shortcodes work in that accordion list.
Or maybe you have an idea how to do this the other way?

The_excerpt showing before the div

i've created latest post container, which i'm trying to create in a shortcode, however when i add the excerpt into the div it is not being shown as it would normally. it is being pushed before the div. The test string after the excerpt is being placed the correct place inside in the div. How come the excerpt is being pushed to the top?
if($insta->have_posts()) :
while($insta->have_posts()) :
$insta->the_post();
$thumb_id = get_post_thumbnail_id();
$thumb_url = wp_get_attachment_image_src($thumb_id,'thumbnail-size', true);
$post_excerpt = the_excerpt();
$permalink = get_permalink();
$output = '<div class="betting-item">'
. '<div class="betting-thumb">'
. '<a href=" '. $permalink .' ">'
. '<img class="thumbnail" src="'. $thumb_url[0] .'" alt="0" />" </a>'
. '</div>'
. '<div class="betting-description">'
. '<h6>Instalocket</h6>'
. '<div class="meta">November 18, 2014 • No Comments</div>'
. '<div>' . $post_excerpt[0] . ' test</div>'
. '</div>'
. '</div>';
endwhile;
endif;
That is happening because the_excerpt() will print the except there and then (so before the div), you need to use get_the_excerpt () which will allow you to print it out at a later stage (in the div).
EDIT:
This should work and only print the excerpt on the first loop:
if($insta->have_posts()) :
$count = 0;
while($insta->have_posts()) :
$insta->the_post();
$thumb_id = get_post_thumbnail_id();
$thumb_url = wp_get_attachment_image_src($thumb_id,'thumbnail-size', true);
if($count == 0)
{
$post_excerpt = the_excerpt();
$count = 1;
}
else
{
$post_excerpt = '';
}
$permalink = get_permalink();

wp_list_pages Title attribute and Order

I found a post on this site to add a custom "mytheme_list_pages" to the functions.php file in order to add the title attribute to a link. While this works to add the title attribute to the "href" output, it no longer preserves the order of the menu as wp_list_pages does. Can someone tell me how to order the output of the custom code below?
I'm calling the function from my page.php file like this:
<?php mytheme_list_pages('exclude=819&title_li='); ?>
The custom function below:
<?php
function mytheme_list_pages($param) {
$pages = get_pages($param);
foreach ( $pages as $page ) {
$li = '<li><a href="' . get_page_link( $page->ID ) . '" title="';
$li .= esc_attr($page->post_title);
$li .= '">';
$li .= $page->post_title;
$li .= '</a></li>';
echo $li;
}
}
?>
Many thanks!
Here is updated code,
<?php mytheme_list_pages('exclude=819&title_li=&sort_column=menu_order'); ?>
<?php
function mytheme_list_pages($param) {
$pages = get_pages($param);
foreach ( $pages as $page ) {
$li = '<li><a href="' . get_page_link( $page->ID ) . '" title="';
$li .= esc_attr($page->post_title);
$li .= '">';
$li .= $page->post_title;
$li .= '</a></li>';
echo $li;
}
}
?>
for more information http://codex.wordpress.org/Function_Reference/get_pages

Adjusting Wordpress Function to display Parent Category

I am trying to display the classic breadcrumbs on a WP based site. I have the following function,
//breadcrumb function
function the_breadcrumb() {
if (!is_home()) {
echo '<a href="';
echo get_option('home');
echo '">';
//bloginfo('name');
echo 'Home';
echo "</a> > ";
if (is_category() || is_single()) {
the_category('title_li=');
if (is_single()) {
echo " » ";
the_title();
}
} elseif (is_page()) {
echo the_title();
}
}
}
But when the page is inside a parent category (i.e., About (Parent), Advisors (Child)) it only shows the child page. Any thoughts on how I can add a condition to show the parent page as well? Any help would be greatly appreciated.
/* EDITED */
I found a perfect working function for it:
function breadcrumbTrail($crumbs = true, $title = 'Browse', $separator = '/')
{
global $post;
?>
<div class="breadcrumbs">
<?php _e('Home','options'); ?> <?php echo $separator; ?>
<?php
if(is_single()) :
the_category(', '); echo ' ' . $separator . ' ';
elseif(is_page()) :
$parent_id = $post->post_parent;
$parents = array();
while($parent_id) :
$page = get_page($parent_id);
if($params["link_none"])
$parents[] = get_the_title($page->ID);
else
$parents[] = ''.get_the_title($page->ID).' ' . $separator . ' ';
$parent_id = $page->post_parent;
endwhile;
$parents = array_reverse($parents);
foreach($parents as $val) :
echo $val;
endforeach;
endif;
the_title();
?>
</div>
<?php
}
Hope this helps someone out.
I suggest using the Breadcrumb NavXT plugin, it works not only for categories but also the pages with parent.
If you want to write your own customised code for the breadcrumb, you can see how Breadcrumb NavXT did it.

Categories