Customizing a Wordpress-generated breadcrumb link - php

Good Day
I am using Wordpress, and I am using a breadcrumbs plugin (Breadcrumb NavXT). Now it works great, exept that I would like to customize the way it links to a specific link:
Now here is the scenario:
I have a page, and on the page are links to posts. So when you click on one of the links on the page, it takes you to the post. Now this posts belongs to a category named Drill Rigs, and Drill Rigs is a child category of Products.
Now on the post page, which has category 'products>drill rigs' (parent/child), the breadcrumb shows the following way:
Home>products>drill rigs>postname
Problem is, that of you click on the products link above, it takes you to the category page of products:
siteurl/category/products
and not the actual wordpress page that is called products (that contains the links linking to the posts mentioned above:
siteurl/absolute link to the page, which is a unique link eg. siteurl/803-2
Now as far as I know, you cannot change the path from a category (which are unique to posts) to a page, neither in wordpress nor the plugin.
The best way I can think of is to add custom jquery or php to the posts php page (or index/header pages) that looks for the container div containing the anchor tags that hosts the title 'products'...and then replacing that anchor tag with my custom unique page url...
How would I do this...?

function the_breadcrumb() {
$sep = '/';
if (!is_front_page()) {
echo '<div class="breadcrumbs">';
echo '<a href="';
echo get_option('home');
echo '">';
bloginfo('name');
echo '</a>' . $sep;
if (is_category() || is_single() ){
the_category('title_li=');
} elseif (is_archive() || is_single()){
if ( is_day() ) {
printf( __( '%s', 'text_domain' ), get_the_date() );
} elseif ( is_month() ) {
printf( __( '%s', 'text_domain' ), get_the_date( _x( 'F Y', 'monthly archives date format', 'text_domain' ) ) );
} elseif ( is_year() ) {
printf( __( '%s', 'text_domain' ), get_the_date( _x( 'Y', 'yearly archives date format', 'text_domain' ) ) );
} else {
_e( 'Blog Archives', 'text_domain' );
}
}
if (is_single()) {
echo $sep;
the_title();
}
if (is_page()) {
echo the_title();
}
if (is_home()){
global $post;
$page_for_posts_id = get_option('page_for_posts');
if ( $page_for_posts_id ) {
$post = get_page($page_for_posts_id);
setup_postdata($post);
the_title();
rewind_posts();
}
}
echo '</div>';
}
}
try this hope this may help you

This code works with asynchron content for your posts: (to copy in functions.php)
function ariane() {
$cat_id = get_the_category()[0]->term_id;
$breadcrumb = '<li>' . get_the_title() . '</li>';
$if_parent = TRUE;
while($if_parent == TRUE) :
$cat_object = get_category($cat_id);
$cat = $cat_object->term_id;
$categoryURL = get_category_link($cat);
$name = $cat_object->name;
$cat_id = $cat_object->parent;
$add_link = '<li>' . $name . '</li>';
$breadcrumb = substr_replace($breadcrumb, $add_link, 0, 0);
if($cat_id == 0) :
$if_parent = FALSE;
endif;
endwhile;
echo '<ul>' . $breadcrumb . '</ul>';
}
In your archive.php or in the loop WP_QUERY
<div class="ariane"><?php ariane(); ?></div>
in css :
.ariane ul{
display: flex;
justify-content: flex-start;
align-items: center;
}
.ariane li:not(:last-child):after {
content: '>';
margin: 0 5px;
}

you can use the function to set your Breadcrumb by coding :
function the_breadcrumb() {
if (!is_home()) {
echo '<a href="';
echo get_option('home');
echo '">';
echo "Home";//bloginfo('name');
echo "</a> / ";
if (is_category() || is_single()) {
the_category('title_li=');
if (is_single()) {
echo " / ";
the_title();
}
} elseif (is_page()) {
echo the_title();
}
}
}
Put the above code in function.php file
you have to just call the function where you want to show the bredcrumb
<?php the_breadcrumb(); ?>
Hope this will help you ...

<script type="text/javascript">
$('#breadcrumbs .category[href="http://mysite.com/category/products"]').attr('href','http://mysite.com/803-2');
</script>
where:
http://mysite.com/803-2
is the url of the p[age I want to link to...

Related

WordPress Infinite Loop on Single Post

I've taken some code from https://gist.github.com/banago/5603826 so that I am able to do previous/next posts for a specific post on my WordPress site which is a custom post type of 'product'.
<?php
if( get_adjacent_post(false, '', true) ) {
previous_post_link('%link', '← Previous Post');
} else {
$first = new WP_Query('post_type=products&posts_per_page=1&order=DESC'); $first->the_post();
echo '← Previous Post';
wp_reset_query();
};
if( get_adjacent_post(false, '', false) ) {
next_post_link('%link', 'Next Post →');
} else {
$last = new WP_Query('post_type=products&posts_per_page=1&order=ASC'); $last->the_post();
echo 'Next Post →';
wp_reset_query();
};
?>
However it doesn't actually infinitely loop, if it's on the first or last post, the link outputs the page it's already on. An example can be found here - http://s860623395.websitehome.co.uk/products/bespoke-build/
Any solutions to this? Thanks!
Was just working on this myself the other day for a portfolio.
Here is what worked for me (adjusted for your product loop)
It should show a single next product indefinitely (infinite loop)
if( get_adjacent_post(false, '', true) ) {
$previous_link_url = get_permalink( get_previous_post() );
echo '← Next Product';
} else {
$first = new WP_Query('post_type=product&posts_per_page=1&order=DESC');
$first->the_post();
echo 'Next Product →';
wp_reset_query();
};
Just a note, next post is actually the previous post, It's a confusing thing, but when you view the latest post, be it portfolio, product or whatever, the next one is actually the one before, otherwise the next post after the most recent published post would then be the oldest one. This seems to be how most have done it within an infinite loop.
Like you, I tried many variations, none of which really worked, but this one did. Currently I'm using it in the Genesis framework for a portfolio CPT with a background image.
Here is the full working code below taken from a child theme:
add_action( 'genesis_after_content_sidebar_wrap', 'go_prev_next_post_nav_cpt', 999 );
/**
* Enable next link in portfolio.
*
* #since 1.0.0
*/
function go_prev_next_post_nav_cpt() {
echo '<div class="adjacent-entry-pagination">';
echo '<div class="wrap">';
if( get_adjacent_post(false, '', true) ) {
$prevThumb = get_the_post_thumbnail_url( get_previous_post(), 'full' );
$previous_link_url = get_permalink( get_previous_post() );
$prevTitle = get_the_title( get_previous_post() );
echo '<a class="post-link bg-image bg-overlay" href="' . $previous_link_url . '" style="background-image:url(' . $prevThumb . ')"><div class="next-description"><p class="mast">Next project</p><h3 class="display-2">' . $prevTitle . '</h3></div></a>';
} else {
$first = new WP_Query('post_type=portfolio&posts_per_page=1&order=DESC');
$first->the_post();
$bg_image = get_the_post_thumbnail_url();
echo '<a class="post-link bg-image bg-overlay" href="' . get_permalink() . '" style="background-image: url(' . $bg_image . ')"><div class="next-description"><p class="mast">Next project</p><h3 class="display-2">' . get_the_title() . '</h3></div></a>';
wp_reset_query();
};
echo '</div>';
echo '</div>';
}
From memory, I used this as a starting point (lot a trial and error!). https://gist.github.com/banago/5603826

How to separate posts from WP_Query into 2 columns

I need to separate posts from WP_Query into 2 columns: first post will go to right column, 3 other posts will go to left column so the structure will look like this - https://prnt.sc/vc044d
Please help me to improve my code.
Thanks a lot.
function mk_latestposts() {
// the query
$the_query = new WP_Query( array( 'posts_per_page' => 4 ) );
// The Loop
if ( $the_query->have_posts() ) {
$postcount = 0;
$string .= '<ul class="mk-recent-posts-list">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$postcount++;
// if this is the first post
if ( $postcount == 1 ) {
$string .= '<li class="first-post">';
$string .= '' . get_the_post_thumbnail($post_id, array( 50, 50) ) . get_the_title() . get_the_date() .'</li>';
} else {
// if this is the next post
$string .= '<li class="next-post">';
$string .= '' . get_the_post_thumbnail($post_id, array( 50, 50) ) . get_the_title() . get_the_date() .'</li>';
}
}
} else {
// no posts found
}
$string .= '</ul>';
return $string;
/* Restore original Post Data */
wp_reset_postdata();
}
// Add a shortcode
add_shortcode('mklatestposts', 'mk_latestposts');
Your code is fine You can do this with CSS. Just add a common class to every <li> Like <li class='single-post'> and then apply this CSS. You can change it from List structure to Div structure whatever you want.
li.single-post{ width: 47%; float: left;} ul.mk-recent-posts-list { width: 100%; }

Implementing a Wordpress custom function

Site I’m working on is a radio show. I have a bunch of custom fields, courtesy of Advanced Custom Fields, for links to musicians’ websites, social profiles, etc.. For the last few years, we’ve used a Genesis child theme to display them, and the code I adapted worked great. The radio show’s host/producer now wants to move to a non-Genesis theme, and I’m having some trouble making it work.
Here’s an example from the site we’re migrating FROM, which includes the functioning code. (The Artist Links box, floated to the right of the main copy.)
Here’s the same post at the new site, where the code doesn’t yet display. (It will display in the same place.) FWIW, the theme is GrandBlog.
Here’s the code:
<?php
add_action ('if_artist_links');
function if_artist_links() {
// These are the custom field keys defined in the dashboard:
$metas_links = array(
'website',
'album',
'tour',
'twitter',
'facebook',
'bandcamp',
'soundcloud',
'youtube',
'instagram',
'linkedin',
'myspace',
'image',
);
// If _any_ ACF field is filled in, it's a go.
$has_meta = false; // init
foreach ($metas_links as $test ) {
if ( get_field($test) ) {
$has_meta = true;
continue; // Just need one meta field filled to create the div.
}
}
if ( $has_meta ) {
echo '<div class="custom-data artist-links">';
echo '<h2 class="artistname">' ?> <?php the_field('name') ?></h2>
<center><strong> <?php the_field('tribe') ?></strong></center> <?php
foreach ( $metas_links as $meta_links ) {
$$meta_links = get_field($meta_links);
if ( $$meta_links ) {
$f_object = get_field_object($meta_links);
$label = $f_object['label'];
echo '<div class="' . $meta_links . '">' .
'<span class="label">' . $label . '</span>' .
'</div>';
}
}
echo '</div><!-- /.custom-data -->';
}
}
?>`
Where there’s an ACF value, a link is created. Where there’s no ACF value, there’s no link. Just one link is required to make the box appear.
As far as I can tell, I’m not telling the code to run, or run in the right way. If you can help me do that, I’ll sing your praises through the weekend.
If possible, I’d really like to wrap this up in a WP plugin, for simplicity and ease of maintenance, and to put minimal code in single.php.
Thanks so much for your help!
You need to use WordPress the_content hook. Replace this in your theme's 'functions.php'.
function single_add_to_content( $content ) {
if( is_single() ) {
// These are the custom field keys defined in the dashboard:
$metas_links = array(
'website',
'album',
'tour',
'twitter',
'facebook',
'bandcamp',
'soundcloud',
'youtube',
'instagram',
'linkedin',
'myspace',
'image',
);
// If _any_ ACF field is filled in, it's a go.
$has_meta = false; // init
foreach ($metas_links as $test ) {
if ( get_field($test) ) {
$has_meta = true;
continue; // Just need one meta field filled to create the div.
}
}
if ( $has_meta ) {
$content.= '<div class="custom-data artist-links">';
$content.= '<h2 class="artistname">'.get_field('name').'</h2>
<center><strong>'.get_field('tribe').'</strong></center>';
foreach ( $metas_links as $meta_links ) {
$$meta_links = get_field($meta_links);
if ( $$meta_links ) {
$f_object = get_field_object($meta_links);
$label = $f_object['label'];
$content.= '<div class="' . $meta_links . '">' .
'<span class="label">' . $label . '</span>' .
'</div>';
}
}
$content.= '</div><!-- /.custom-data -->';
}
}
return $content;
}
add_filter( 'the_content', 'single_add_to_content' );
Or you can copy 'single.php' to your child theme and add this code directly without function.

Facebook Share Link Not Correct

I have a WordPress site that when you click the Facebook like button it likes the wrong url.
If I inspect the web page, the url that is inside the like widget is always the same URL no matter what post you are on. It is like one of the posts url is being set somewhere.
I have checked and it ONLY does it for posts, not pages.
I feel like I have found the area (might be wrong) but I believe it is here in the loop
<?php if (have_posts()) :
$post = $posts[0]; // Hack. Set $post so that the_date() works. ?>
<?php /* If this is a category archive */ if (is_category()) { ?>
<h3><?php printf(__('Archive for the ‘%s’ Category', TEXTDOMAIN), single_cat_title('', false)); ?></h3>
<?php /* If this is a tag archive */ } elseif( is_tag() ) { ?>
<h3><?php printf(__('Posts Tagged ‘%s’', TEXTDOMAIN), single_tag_title('', false) ); ?></h3>
<?php /* If this is a daily archive */ } elseif (is_day()) { ?>
<h3><?php printf(__('Archive for %s | Daily archive page', TEXTDOMAIN), get_the_time(__('F jS, Y', TEXTDOMAIN))); ?></h3>
<?php /* If this is a monthly archive */ } elseif (is_month()) { ?>
<h3><?php printf(__('Archive for %s | Monthly archive page', TEXTDOMAIN), get_the_time(__('F Y', TEXTDOMAIN))); ?></h3>
<?php /* If this is a yearly archive */ } elseif (is_year()) { ?>
<h3><?php printf(__('Archive for %s | Yearly archive page', TEXTDOMAIN), get_the_time(__('Y', TEXTDOMAIN))); ?></h3>
<?php /* If this is a yearly archive */ } elseif (is_search()) { ?>
<h3><?php printf( __( 'Search Results for: %s', TEXTDOMAIN ), '<span>' . get_search_query() . '</span>' ); ?></h3>
<?php /* If this is an author archive */ } elseif (is_author()) { ?>
<h3><?php _e('Author Archive', TEXTDOMAIN); ?></h3>
<?php /* If this is a paged archive */ } elseif (isset($_GET['paged']) && !empty($_GET['paged'])) { ?>
<h3><?php _e('Blog Archives', TEXTDOMAIN); ?></h3>
<?php }
while (have_posts()) : the_post();
global $more;
if(!is_single()) $more = 0;
$type_blog = ( $GLOBALS['yiw_custom_blog_type'] != false ) ? $GLOBALS['yiw_custom_blog_type'] : get_option( $GLOBALS['shortname'] . '_blog_type', 'classic' );
?>
It is setting the $post but I am not sure how to fix it or check that this is in fact it. If I remove it, the page does not work.
Any help here will help a lot, if I am not correct what else should I be looking for?
The following are also areas in the functions.php that have the $post in it
function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$first_img = get_stylesheet_directory_uri()."/images/default.gif";
}
return $first_img;
}
and
function get_current_ID()
{
global $post;
return $post->ID;
}
function get_current_pagename()
{
global $post;
return $post->post_name;
}

Wordpress: disable links for get_the_category_list function?

Hey my theme uses this function to display the categories a post has, but it also creates links which I would like to get rid of. I prefer php rather than a javascript solution.
Here is the code:
<p class="postmeta">
<?php if ( 'post' == get_post_type() ) : // Hide category and tag text for pages on Search ?>
<?php
/* translators: used between list items, there is a space after the comma */
$categories_list = get_the_category_list( __( ', ', 'gridster' ) );
if ( $categories_list && gridster_categorized_blog() ) :
?>
<?php /*printf( __( '%1$s', 'gridster' ), $categories_list );*/ echo $categories_list; ?>
<?php endif; // End if categories ?>
<?php endif; // End if 'post' == get_post_type() ?>
</p>
Link to the codex reference
How can I void those links?
or get rid of links all together from DOM (but this might create more work as the actual text is between the <a> tags
HTML
<p class="postmeta">
<a target="_blank" href="http://whatever.com/category/default/" title="View all posts in Default" rel="category tag">Default</a>
</p>
Thanks!!
If you just want a text-string returned and use the native get_the_categories() function, you could simply wrap it in PHP's strip_tags() function to remove all HTML that is returned:
echo strip_tags( get_the_category_list(__( ', ', 'gridster' ));
You can try modifying this to fit your needs:
<?php
foreach((get_the_category()) as $category) {
echo $category->cat_name . ' ';
}
?>
(From http://premium.wpmudev.org/blog/how-to-get-a-wordpress-category-name-without-the-link/ )
One option is to copy the original function and adapt to your needs. Modified to remove the <a> tags but not tested, compare to the original, adapt if needed, and add it to the theme's functions.php:
function category_list_so_22771587( $separator = '' ) {
global $wp_rewrite;
$categories = get_the_category( $post_id );
$thelist = '';
$i = 0;
foreach ( $categories as $category ) {
if ( 0 < $i )
$thelist .= $separator;
switch ( strtolower( $parents ) ) {
case 'multiple':
if ( $category->parent )
$thelist .= get_category_parents( $category->parent, true, $separator );
$thelist .= $category->name;
break;
case 'single':
if ( $category->parent )
$thelist .= get_category_parents( $category->parent, false, $separator );
$thelist .= $category->name;
break;
case '':
default:
$thelist .= $category->name;
}
++$i;
}
return $thelist;
}
And modify your template to:
$categories_list = category_list_so_22771587( __( ', ', 'gridster' ) );
You can retrieve categories, extract the 'name' column of each items, and implode each values with a comma:
<?= implode(', ', (array_column(get_the_category(), 'name')))?>
Or you can retrieve categories list with link, and remove a tags :
<?= strip_tags(get_the_category_list(', ')) ; ?>
Do you just want to hide the links? If so use css:
.postmeta a {display: none}
Edit, you can also "disable" those links in css:
.postmeta a {
pointer-events: none;
cursor: default;
}

Categories