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.
Related
I'm having trouble displaying the post taxonomy terms as classes. I know I've done it in the past, but I can't seem to find it and don't remember how to do it.
I have a custom taxonomy 'thema', now I want to add the corresponding theme term as classes to every post on the archive page.
I can list all the terms for a post, but when I want to output it as classes, the page stop loading from the point where the post loop starts.
This is what I have so far:
(EDIT: changed some code to show when the error occurs)
while ( $query->have_posts() ) {
$query->the_post();
$f = get_fields();
$link = ($f['bericht_doorverwijzen'] ?? '' ? $f['bericht_doorverwijzen'] : get_the_permalink());
$terms = wp_get_post_terms( get_the_ID(), 'thema');
echo "<div class='post ". foreach ($terms as $t) { echo $t->slug, ' '; } ."'>";
echo "<div class='postWrapper'>";
echo "<div class='content'>";
echo "<h2><a href='".$link."' title='Ga naar ".get_the_title()."'>".get_the_title()."</a></h2>";
echo the_excerpt_max_charlength($charlength = 130);
echo "<a href='".$link."' title='Ga naar ".get_the_title()."' class='link'>Lees meer</a>";
echo "</div>";
echo "</div>";
echo "</div>";
}
You can try this way:
$classes = '';
$terms = wp_get_post_terms( get_the_ID(), 'thema');
foreach($terms as $t) {
$classes .= $t->slug . ' ';
}
echo "<div class='post ". $classes ."'>";
It's should work. Hope help you.
I am still new to WordPress (and PHP) and I am trying to finalize my very first template.
Current request is to display all given (sub)categories, but if one category only has one post, it should display the post instead with an excerpt. I think I am only missing one tiny piece to complete this...
What I have so far:
<?php
if(!empty($categories)) { ?>
<!-- Display Sub-Categories and description -->
<div class="brick_list">
<?php
foreach ( $categories as $category ) {
echo '<div class="item"><h4 class="item-title">' . $category->name . '</h4><div class="item-text">' . $category->description . '</div><div class="item-link">Read more</div></div>';
}
?>
</div>
<?php }; ?>
I've searched the web for a solution of the "has only one post in category" task and found this:
if( 1 == $category[0]->count ) { ..... }
But I don't know how to include (or merge) this with my existing foreach loop.
Can anyone help?
I have edit your code a bit, I ahve added a condition to check the category count before diplying the div element.
<?php
$categories = get_categories();
if(!empty($categories)) { ?>
<!-- Display Sub-Categories and description -->
<div class="brick_list">
<?php
foreach ( $categories as $category ) {
if ( $category->count!= 1 ){
echo '<div class="item"><h4 class="item-title">' . $category->name . '</h4><div class="item-text">' . $category->description . '</div><div class="item-link">Read more</div></div>';
} else {
// display the post with excerpt
}
}
?>
</div>
<?php }; ?>
?>
Let me know if you got any issue.
I was able to solve it. And at the end it doesn't look that difficult :-D
I share it here in case someone else needs it
<?php
foreach ( $categories as $category ) {
// If there is only one post available, go directly to the post
if($category->count == 1) {
$all_posts = get_posts($category);
echo '<div class="item"><h4 class="item-title">' . get_the_title($all_posts[0]->ID) . '</h4><div class="item-text">' . wp_trim_words( get_the_content($all_posts[0]->ID), 30, '...') . '"</div></div>';
// otherwise display subcategories
} else {
echo '<div class="item"><h4 class="item-title">' . $category->name . '</h4><div class="item-text">' . wp_trim_words($category->description, 30, '...') . '</div></div>';
}
}
?>
I'm using ACF pro 5. I create a Repeater Field with two sub_field, called sub_field_item & sub_field_value. I need a code which will hide if sub_field_value is empty. Probably sub_field_item contains text but if sub_field_value empty, then it will hide both.
I tried with this code, but it's not working.
<?php
if( have_rows('myrepeater') )
{
$field_key = "field_5aa18d1bc322c"; //KEY for Repeater main field "myrepeater
$field = get_field_object($field_key);
foreach($field['value'] as $value)
{
if(!empty($value['sub_field_item']))
{
$not_empty = true;
break;
}
}
if($not_empty == true)
{
echo '<h2>' . $field['label'] . '</h2>';
}
while ( have_rows('sub_field_item') )
{
the_row();
$subfield = get_sub_field('sub_field_value');
if( !empty($subfield) )
{
echo '<b>' . $subfield . '</b>';
}
}
}
?>
here is the magic code :
<?php
if(get_field('field_name')):
while(has_sub_field('field_name')):
if(get_sub_field('subfield_name')): ?>
<h2><?php echo get_sub_field('subfield_name'); ?></h2>
<?php endif;
endwhile;
endif;
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 ?
On a wordress site I'm building, for the first time, I'm trying to add custom loops.
<?php
$custom_loop = new WP_Query('showposts=5&category_name=Featured&orderby=rand');
if ( $custom_loop->have_posts() ) :
echo '<ul>';
while ( $custom_loop->have_posts() ):
$custom_loop->the_post();
echo '<li>' . get_the_title() . '
</li>';
endwhile;
wp_reset_query();
echo '</ul>';
endif;
?>
How do I correctly add this line if ( has_post_thumbnail() ) {
the_post_thumbnail();} inbetween the <li></li> tags?
I've tried just placing it in, but it shows up like normal text on the site.
If you want to put the_post_thumbnail() value between li tag, use below code instead of your echo '<li>.....';
echo '<li>' . get_the_title() . '';
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
}
echo '</li>';
Try this:
<?php
$custom_loop = new WP_Query('showposts=5&category_name=Featured&orderby=rand');
if ($custom_loop->have_posts()) {
echo '<ul>';
while ($custom_loop->have_posts()) {
$custom_loop->the_post();
$thumb = (has_post_thumbnail()) ? the_post_thumbnail() : '';
printf('<li>%s%s</li>', get_permalink(), get_the_title(), $thumb);
}
wp_reset_query();
echo '</ul>';
}
?>