I'm using the Avada theme in wordpress. I'm trying to add some post custom fields into the scrollover (shown by mouse hover). I tried to edit the template file that controls the scrollover but without success. I got an error message invalid ">".
I tried to use this code to show the custom field:
<?php echo get_post_meta( $post_id,'the_price',); ?>
The Avada function template is as follows:
if ( ! function_exists( 'avada_render_rollover' ) ) {
function avada_render_rollover( $post_id, $post_permalink = '', $display_woo_price = false, $display_woo_buttons = false, $display_post_categories = 'default', $display_post_title = 'default', $gallery_id = '' ) {
// Retrieve the permalink if it is not set
if ( ! $post_permalink ) {
$post_permalink = get_permalink( $post_id );
}
// Check if theme options are used as base or if there is an override for post categories
if ( 'enable' == $display_post_categories ) {
$display_post_categories = true;
} elseif ( 'disable' == $display_post_categories ) {
$display_post_categories = false;
} else {
$display_post_categories = ! Avada()->settings->get( 'cats_image_rollover' );
}
// Check if theme options are used as base or if there is an override for post title
if ( 'enable' == $display_post_title ) {
$display_post_title = true;
} elseif ( 'disable' == $display_post_title ) {
$display_post_title = false;
} else {
$display_post_title = ! Avada()->settings->get( 'title_image_rollover' );
}
// Set the link on the link icon to a custom url if set in page options
$icon_permalink = ( fusion_get_page_option( 'link_icon_url', $post_id ) != null ) ? fusion_get_page_option( 'link_icon_url', $post_id ) : $post_permalink;
if ( '' == fusion_get_page_option( 'image_rollover_icons', $post_id ) || 'default' == fusion_get_page_option( 'image_rollover_icons', $post_id ) ) {
if( ! Avada()->settings->get( 'link_image_rollover' ) && ! Avada()->settings->get( 'zoom_image_rollover' ) ) { // link + zoom
$image_rollover_icons = 'linkzoom';
} elseif( ! Avada()->settings->get( 'link_image_rollover' ) && Avada()->settings->get( 'zoom_image_rollover' ) ) { // link
$image_rollover_icons = 'link';
} elseif( Avada()->settings->get( 'link_image_rollover' ) && ! Avada()->settings->get( 'zoom_image_rollover' ) ) { // zoom
$image_rollover_icons = 'zoom';
} elseif( Avada()->settings->get( 'link_image_rollover' ) && Avada()->settings->get( 'zoom_image_rollover' ) ) { // link
$image_rollover_icons = 'no';
} else {
$image_rollover_icons = 'linkzoom';
}
} else {
$image_rollover_icons = fusion_get_page_option( 'image_rollover_icons', $post_id );
}
// Set the link target to blank if the option is set
$link_target = ( 'yes' == fusion_get_page_option( 'link_icon_target', $post_id ) || 'yes' == fusion_get_page_option( 'post_links_target', $post_id ) ) ? ' target="_blank"' : '';
?>
<div class="fusion-rollover">
<div class="fusion-rollover-content">
<?php if ( 'no' != $image_rollover_icons && 'product' != get_post_type( $post_id ) ) : // Check if rollover icons should be displayed ?>
<?php if ( 'zoom' != $image_rollover_icons ) : // If set, render the rollover link icon ?>
<a class="fusion-rollover-link" href="<?php echo $icon_permalink; ?>"<?php echo $link_target; ?>>Permalink</a>
<?php endif; ?>
<?php if ( 'link' != $image_rollover_icons ) : // If set, render the rollover zoom icon ?>
<?php
// Get the image data
$full_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), 'full' );
if ( ! is_array( $full_image ) ) {
$full_image = array();
$full_image[0] = '';
}
// If a video url is set in the post options, use it inside the lightbox
if ( fusion_get_page_option( 'video_url', $post_id ) ) {
$full_image[0] = fusion_get_page_option( 'video_url', $post_id );
}
?>
<?php if ( 'linkzoom' == $image_rollover_icons || '' === $image_rollover_icons ) : // If both icons will be shown, add a separator ?>
<div class="fusion-rollover-sep"></div>
<?php endif; ?>
<?php if ( $full_image[0] ) : // Render the rollover zoom icon if we have an image ?>
<?php
// Only show images of the clicked post
if ( 'individual' == Avada()->settings->get( 'lightbox_behavior' ) ) {
$lightbox_content = avada_featured_images_lightbox( $post_id );
$data_rel = sprintf( 'iLightbox[gallery%s]', $post_id );
// Show the first image of every post on the archive page
} else {
$lightbox_content = '';
$data_rel = sprintf( 'iLightbox[gallery%s]', $gallery_id );
}
?>
<a class="fusion-rollover-gallery" href="<?php echo $full_image[0]; ?>" data-id="<?php echo $post_id; ?>" data-rel="<?php echo $data_rel; ?>" data-title="<?php echo get_post_field( 'post_title', get_post_thumbnail_id( $post_id ) ); ?>" data-caption="<?php echo get_post_field( 'post_excerpt', get_post_thumbnail_id( $post_id ) ); ?>">Gallery</a><?php echo $lightbox_content; ?>
<?php endif; ?>
<?php endif; ?>
<?php endif; ?>
<?php if ( $display_post_title ) : // Check if we should render the post title on the rollover ?>
<h4 class="fusion-rollover-title"><a href="<?php echo $icon_permalink; ?>"<?php echo $link_target; ?>><?php echo get_the_title( $post_id ); ?></a></h4>
<?php endif; ?>
<?php
// Check if we should render the post categories on the rollover
if ( $display_post_categories ) {
// Determine the correct taxonomy
if ( 'post' == get_post_type( $post_id ) ) {
$post_taxonomy = 'category';
} elseif ( 'avada_portfolio' == get_post_type( $post_id ) ) {
$post_taxonomy = 'portfolio_category';
} elseif ( 'product' == get_post_type( $post_id ) ) {
$post_taxonomy = 'product_cat';
}
echo get_the_term_list( $post_id, $post_taxonomy, '<div class="fusion-rollover-categories">', ', ', '</div>' );
}
?>
<?php if ( $display_woo_price ) : // Check if we should render the woo product price ?>
<?php woocommerce_get_template( 'loop/price.php' ); ?>
<?php endif; ?>
<?php if ( $display_woo_buttons ) : // Check if we should render the woo "add to cart" and "details" buttons ?>
<div class="fusion-product-buttons">
<?php woocommerce_get_template( 'loop/add-to-cart.php' ); ?>
<span class="fusion-rollover-linebreak"></span>
<a class="fusion-show-details-button" href="<?php echo post_permalink(); ?>"><?php _e( 'Details', 'Avada' ); ?></a>
</div>
<?php endif; ?>
</div>
</div>
<?php
}
}
Usually to use $post_id you need first to declare global $post
Related
Sorry I'm newbie with Wordpress, How to display 10 users only and the others inside "see more".
I searched more times before asking but with nothing!
Thanks in advance.
<?php
$post_id = get_the_ID();
$users_liked = get_post_meta( $post_id, '_user_liked', true );
if ( '' !== $users_liked && ! empty( $users_liked ) ) {
?>
<span>Liked:</span></br>
<?php foreach ( array_values( $users_liked ) as $user_id ) :
$user = get_user_by( 'id', $user_id );
if ( false === $user ) {
continue;
}
?>
<div class="sort-list-name">
<a href="<?php echo get_author_posts_url($user->ID);?>">
<?php echo get_avatar( $user->ID, 35 ); ?>
</a>
</div>
<?php
endforeach; ?>
<?php } ?>
first off all try to add an index in your loop
<?php
$post_id = get_the_ID();
$i = 0;
$users_liked = get_post_meta( $post_id, '_user_liked', true );
if ( '' !== $users_liked && ! empty( $users_liked ) ) {
?>
<span>Liked:</span></br>
<?php foreach ( array_values( $users_liked ) as $user_id ) :
$user = get_user_by( 'id', $user_id );
if ( false === $user ) {
continue;
}
?>
<div class="sort-list-name">
<a href="<?php echo get_author_posts_url($user->ID);?>">
<?php echo get_avatar( $user->ID, 35 ); ?>
</a>
</div>
<?php
if ($i++ == 9){
echo "<a href='#'>see more</a>";
break;
}
endforeach; ?>
<?php } ?>
and then if $i get 9 the loop will be break
Ask to everyone, i have problem. Here i try to use multiple chechbox to my custom post metabox.
<?php
function prodetail() {
add_meta_box('pro_metabox', 'Detail Property', 'pro_metabox', 'property', 'normal', 'default');
}
function pro_metabox() {
global $post;
echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
$postmeta = maybe_unserialize( get_post_meta( $post->ID, 'elements', true ) );
$elements = array(
'pool' => 'Pool',
'garage' => 'Garage',
'balcon' => 'Balcon',
'yard' => 'Yard',
'internet' => 'Internet'
);
foreach ( $elements as $id => $element) {
if ( is_array( $postmeta ) && in_array( $id, $postmeta ) ) {
$checked = 'checked="checked"';
} else {
$checked = null;
}
?>
<div class="pro-inn">
<div class="procols">
<div class="pro-inn">
<input type="checkbox" name="multval[]" value="<?php echo $id; ?>" <?php echo $checked; ?> />
<?php echo $element;?>
</div>
</div>
</div>
<?php
}
}
function pro_meta($post_id, $post) {
if ( !wp_verify_nonce( $_POST['eventmeta_noncename'], plugin_basename(__FILE__) )) {
return $post->ID;
}
if ( !current_user_can( 'edit_post', $post->ID ))
return $post->ID;
if ( ! empty( $_POST['multval'] ) ) {
update_post_meta( $post_id, 'elements', $_POST['multval'] );
} else {
delete_post_meta( $post_id, 'elements' );
}
}
add_action('save_post', 'pro_meta', 1, 2);
?>
help me to add code to show this checked result to single.php because my code use foreach just show Array text not show text like Pool Garage Balcon ect.
Thanks
Use this code in your single.php file for your custom post
$meta_value = get_post_meta( $post->ID, 'elements', true );
foreach($meta_value as $key=>$value){
echo $value . ' ';
}
It will show results same as you mentioned in the question ie:
(Pool Garage Balcon ect.)
I want to take a full path in bread crumbs in the wordpress template
Now displays only the home page and category
I want the home - category - the title of the article with the link to this page, with a working schema markup
now the code looks like this
version Wordpress 4.8.1
function barcelona_breadcrumb() {
if ( barcelona_get_option( 'show_breadcrumb' ) != 'on' ) {
return;
}
$barcelona_post_type = is_single() ? get_post_type() : NULL;
$barcelona_sep_icon = '';
$barcelona_items = '';
if ( ( is_single() && $barcelona_post_type == 'post' && ! is_attachment() ) || is_category() ) {
$barcelona_categories = is_category() ? array() : get_the_category();
$barcelona_current_cat = $barcelona_last_cat = is_category() ? get_queried_object() : $barcelona_categories[0];
$barcelona_counter = 3;
while ( $barcelona_current_cat->category_parent != '0' ) {
$barcelona_current_cat = get_category( $barcelona_current_cat->category_parent );
$barcelona_items = $barcelona_sep_icon .'<li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a itemprop="item" href="'. esc_url( get_category_link( $barcelona_current_cat ) ) .'"><span itemprop="name">'. esc_html( $barcelona_current_cat->name ) .'</span></a><meta itemprop="position" content="%'. $barcelona_counter .'%" /></li>'. $barcelona_items;
$barcelona_counter++;
}
$barcelona_items .= $barcelona_sep_icon .'<li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><'. ( is_category() ? 'span' : 'a href="'. esc_url( get_category_link( $barcelona_last_cat ) ) .'"' ) .' itemprop="item"><span itemprop="name">'. esc_html( $barcelona_last_cat->name ) .'</span></'. ( is_category() ? 'span' : 'a' ) .'><meta itemprop="position" content="%2%" /></li>';
if ( $barcelona_counter > 3 ) {
$barcelona_arr = array_reverse( range( 2, $barcelona_counter - 1 ) );
foreach( $barcelona_arr as $k => $v ) {
$barcelona_items = str_replace( 'itemprop="position" content="%'. intval( $k + 2 ) .'%"', 'itemprop="position" content="'. intval( $v ) .'"', $barcelona_items );
}
} else {
$barcelona_items = str_replace( 'content="%2%"', 'content="2"', $barcelona_items );
}
} else if ( is_archive() || is_search() ) {
$barcelona_title = is_search() ? esc_html__( 'Search Results', 'barcelona' ) : esc_html( get_the_archive_title() );
if ( is_author() ) {
$barcelona_title = esc_html__( 'Author Archive', 'barcelona' );
} else if ( is_year() ) {
$barcelona_title = esc_html__( 'Yearly Archive', 'barcelona' );
} else if ( is_month() ) {
$barcelona_title = esc_html__( 'Monthly Archive', 'barcelona' );
} else if ( is_day() ) {
$barcelona_title = esc_html__( 'Daily Archive', 'barcelona' );
} else if ( is_tag() ) {
$barcelona_title = esc_html__( 'Tag Archive', 'barcelona' );
}
$barcelona_items .= $barcelona_sep_icon .'<li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><span itemprop="item"><span itemprop="name">'. $barcelona_title .'</span></span><meta itemprop="position" content="2" /></li>';
}
$barcelona_items = '<li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a itemprop="item" href="'. esc_url( home_url( '/' ) ) .'">'. esc_html__( 'Home', 'barcelona' ) .'</a><meta itemprop="position" content="1" /></li>'. $barcelona_items;
echo '<div class="breadcrumb-wrapper"><div class="container"><ol itemscope itemtype="http://schema.org/BreadcrumbList" class="breadcrumb">'. $barcelona_items .'</ol></div></div>';
}
Thanks!
Try with below code put below code in functions.php
function the_breadcrumbs() {
global $post;
if (!is_home()) {
echo "<a href='";
echo get_option('home');
echo "'>";
echo "Site's name here";
echo "</a>";
if (is_category() || is_single()) {
echo " > ";
$cats = get_the_category( $post->ID );
foreach ( $cats as $cat ){
echo $cat->cat_name;
echo " > ";
}
if (is_single()) {
the_title();
}
} elseif (is_page()) {
if($post->post_parent){
$anc = get_post_ancestors( $post->ID );
$anc_link = get_page_link( $post->post_parent );
foreach ( $anc as $ancestor ) {
$output = " > ".get_the_title($ancestor)." > ";
}
echo $output;
the_title();
} else {
echo ' > ';
echo the_title();
}
}
}
elseif (is_tag()) {single_tag_title();}
elseif (is_day()) {echo"Archive: "; the_time('F jS, Y'); echo'</li>';}
elseif (is_month()) {echo"Archive: "; the_time('F, Y'); echo'</li>';}
elseif (is_year()) {echo"Archive: "; the_time('Y'); echo'</li>';}
elseif (is_author()) {echo"Author's archive: "; echo'</li>';}
elseif (isset($_GET['paged']) && !empty($_GET['paged'])) {echo "Blogarchive: "; echo'';}
elseif (is_search()) {echo"Search results: "; }
}
Put below code in header.php
if(function_exists('the_breadcrumbs')) the_breadcrumbs();
I want to have three dropdown list that allow user select Author, Post and Testimonial. But wordpress only save last dropdown.
This is first dropdown:
//allow authors list on posts
add_action( 'add_meta_boxes', 'author_list_add' );
function author_list_add() {
add_meta_box( 'my-meta-box-id', 'Post Author', 'author_list', 'post', 'normal', 'high' );
}
function author_list( $post ) {
$values = get_post_custom( $post->ID );
$selected = isset( $values['custom_author'] ) ? esc_attr( $values['custom_author'][0] ) : '';
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce ');
?>
<p>
<label for="custom_author">Select author</label>
<br>
<select name="custom_author" id="custom_author">
<option value="0" <?php selected( $selected, '0' ); ?>>No author</option>
<?php
$args = array('post_type' => 'member');
$loop = get_posts($args);
foreach ($loop as $post) : setup_postdata($post);
$name = $post->post_title;
$link = $post->ID;
?>
<option value="<?php echo $link; ?>" <?php selected( $selected, $link ); ?>><?php echo $name; ?></option>
<?php
endforeach;
?>
</select>
</p>
<?php
}
add_action( 'save_post', 'author_list_save' );
function author_list_save( $post_id ) {
// Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// if our nonce isn't there, or we can't verify it, bail
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;
// now we can actually save the data
$allowed = array(
'a' => array( // on allow a tags
'href' => array() // and those anchords can only have href attribute
)
);
// Probably a good idea to make sure your data is set
if( isset( $_POST['custom_author'] ) )
update_post_meta( $post_id, 'custom_author', esc_attr( $_POST['custom_author'] ) );
}
The second Part:
//allow select testimonial list on posts
add_action( 'add_meta_boxes', 'testimonial_select_box_add' );
$types = array( 'post', 'page' );
function testimonial_select_box_add() {
add_meta_box( 'testimonial-box', 'Testimonial in bottom row', 'testimonial_select_box', $types, 'normal', 'high' );
}
function testimonial_select_box( $post ) {
$values = get_post_custom( $post->ID );
$selected = isset( $values['testimonial_select'] ) ? esc_attr( $values['testimonial_select'][0] ) : '';
wp_nonce_field( 'testimonial_meta_box_nonce', 'meta_box_nonce' );
?>
<p>
<label for="testimonial_select">Select Testimonial</label>
<br>
<select name="testimonial_select" id="testimonial_select">
<option value="0" <?php selected( $selected, '0' ); ?>>None</option>
<?php
$args = array('post_type' => 'testimonial','posts_per_page' => -1);
$loop = get_posts($args);
foreach ($loop as $post) : setup_postdata($post);
$name = $post->post_title;
$link = $post->ID;
?>
<option value="<?php echo $link; ?>" <?php selected( $selected, $link ); ?>><?php echo $name; ?></option>
<?php
endforeach;
?>
</select>
</p>
<?php
}
add_action( 'save_post', 'testimonial_select_box_save' );
function testimonial_select_box_save( $post_id ) {
// Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// if our nonce isn't there, or we can't verify it, bail
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'testimonial_meta_box_nonce' ) ) return;
// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;
// now we can actually save the data
$allowed = array(
'a' => array( // on allow a tags
'href' => array() // and those anchords can only have href attribute
)
);
// Probably a good idea to make sure your data is set
if( isset( $_POST['testimonial_select'] ) )
update_post_meta( $post_id, 'testimonial_select', esc_attr( $_POST['testimonial_select'] ) );
}
And last one:
//allow select Post list on posts
add_action( 'add_meta_boxes', 'post_select_box_add' );
function post_select_box_add() {
add_meta_box( 'post-box', 'Post in bottom row', 'post_select_box',$types, 'normal', 'high' );
}
function post_select_box( $post ) {
$values = get_post_custom( $post->ID );
$selected = isset( $values['post_select'] ) ? esc_attr( $values['post_select'][0] ) : '';
wp_nonce_field( 'post_meta_box_nonce', 'meta_box_nonce' );
?>
<p>
<label for="post_select">Select Post</label>
<br>
<select name="post_select" id="post_select">
<option value="0" <?php selected( $selected, '0' ); ?>>None</option>
<?php
$args = array('post_type' => 'post','posts_per_page' => -1);
$loop = get_posts($args);
foreach ($loop as $post) : setup_postdata($post);
$name = $post->post_title;
$link = $post->ID;
?>
<option value="<?php echo $link; ?>" <?php selected( $selected, $link ); ?>><?php echo $name; ?></option>
<?php
endforeach;
?>
</select>
</p>
<?php
}
add_action( 'save_post', 'post_select_box_save' );
function post_select_box_save( $post_id ) {
// Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// if our nonce isn't there, or we can't verify it, bail
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'post_meta_box_nonce' ) ) return;
// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;
// now we can actually save the data
$allowed = array(
'a' => array( // on allow a tags
'href' => array() // and those anchords can only have href attribute
)
);
// Probably a good idea to make sure your data is set
if( isset( $_POST['post_select'] ) )
update_post_meta( $post_id, 'post_select', esc_attr( $_POST['post_select'] ) );
}
as you can see, function and ID names are unique, but only last one saved.
this is what happened when I save :
As per my comment, each of your nonces seems to have the same name (meta_box_nonce), so there may be a clash if all three fields are submitted in the same form. Trying giving them unique names (in both your wp_nonce_field and wp_verify_nonce calls).
I need to shorten the title in the previous and next buttons in a WP theme.
Here's the code:
<?php
/* ----------------------------------------------------------------------------------
Display navigation to next/previous pages when applicable.
---------------------------------------------------------------------------------- */
if ( ! function_exists( 'thinkup_input_nav' ) ) :
function thinkup_input_nav( $nav_id ) {
global $wp_query, $post;
// Don't print empty markup on single pages if there's nowhere to navigate.
if ( is_single() ) {
$previous = ( is_attachment() ) ? get_post( $post->post_parent ) : get_adjacent_post( false, '', true );
$next = get_adjacent_post( false, '', false );
if ( ! $next && ! $previous )
return;
}
// Don't print empty markup in archives if there's only one page.
if ( $wp_query->max_num_pages < 2 && ( is_home() || is_archive() || is_search() ) )
return;
?>
<nav role="navigation" id="<?php echo $nav_id; ?>">
<?php if ( is_single() ) : ?>
<?php previous_post_link( '<div class="nav-previous">%link</div>', '<span class="meta-icon"><i class="icon-angle-left icon-large"></i></span><span class="meta-nav">%title</span>' ); ?>
<?php next_post_link( '<div class="nav-next">%link</div>', '<span class="meta-nav">%title</span><span class="meta-icon"><i class="icon-angle-right icon-large"></i></span>' ); ?>
<?php elseif ( $wp_query->max_num_pages > 1 && ( is_home() || is_archive() || is_search() ) ) : // navigation links for home, archive, and search pages ?>
<?php if ( get_next_posts_link() ) : ?>
<div class="nav-previous"><?php next_posts_link( __( 'Older posts', 'lan-thinkupthemes') ); ?></div>
<?php endif; ?>
<?php if ( get_previous_posts_link() ) : ?>
<div class="nav-next"><?php previous_posts_link( __( 'Newer posts', 'lan-thinkupthemes') ); ?></div>
<?php endif; ?>
<?php endif; ?>
</nav><!-- #<?php echo $nav_id; ?> -->
<?php
}
endif;
/* ----------------------------------------------------------------------------------
Display navigation to next/previous image when applicable.
---------------------------------------------------------------------------------- */
if ( ! function_exists( 'thinkup_input_imagesnav' ) ) :
function thinkup_input_imagesnav() {
global $wp_query, $post;
?>
<nav role="navigation" id="nav-below">
<div class="nav-previous"><?php previous_image_link( 'false', '<div class="nav-previous"><span class="meta-icon"><i class="icon-angle-left icon-large"></i></span><span class="meta-nav">' . __( 'Previous', 'lan-thinkupthemes') . ' </span></div>' ); ?></div>
<div class="nav-next"><?php next_image_link( 'false', '<span class="meta-nav">' . __( 'Next', 'lan-thinkupthemes') . '</span><span class="meta-icon"><i class="icon-angle-right icon-large"></i></span>' ); ?></div>
</nav><!-- #image-navigation -->
<?php
}
endif;
/* Display comments at bottom of post, page and project pages. */
function thinkup_input_allowcomments() {
if ( comments_open() || '0' != get_comments_number() )
comments_template( '/comments.php', true );
}
/* ----------------------------------------------------------------------------------
Returns true if a blog has more than 1 category.
---------------------------------------------------------------------------------- */
function thinkup_input_categorizedblog() {
if ( false === ( $all_the_cool_cats = get_transient( 'all_the_cool_cats' ) ) ) {
// Create an array of all the categories that are attached to posts
$all_the_cool_cats = get_categories( array(
'hide_empty' => 1,
) );
// Count the number of categories that are attached to the posts
$all_the_cool_cats = count( $all_the_cool_cats );
set_transient( 'all_the_cool_cats', $all_the_cool_cats );
}
if ( '1' != $all_the_cool_cats ) {
return true;
} else {
return false;
}
}
/* Flush out the transients used in thinkup_input_categorizedblog. */
function thinkup_input_transient_flusher() {
delete_transient( 'all_the_cool_cats' );
}
add_action( 'edit_category', 'thinkup_input_transient_flusher' );
add_action( 'save_post', 'thinkup_input_transient_flusher' );
What I tried was to add substring to this line:
<?php previous_post_link( '<div class="nav-previous">%link</div>', '<span class="meta-icon"><i class="icon-angle-left icon-large"></i></span><span class="meta-nav">%title</span>' ); ?>
to:
<?php previous_post_link( '<div class="nav-previous">%link</div>', '<span class="meta-icon"><i class="icon-angle-left icon-large"></i></span><span class="meta-nav"><?php echo substr(%title,0,10)?></span>' ); ?>
but upon reloading the page it displayed an empty text instead.
What am I doing wrong?
You can override the previous_post_link() function in your functions.php file of your theme as like this :
function previous_post_link($format='« %link', $link='%title', $in_same_cat = false, $excluded_categories = '',$limit = -1) {
if ( is_attachment() )
$post = & get_post($GLOBALS['post']->post_parent);
else
$post = get_previous_post($in_same_cat, $excluded_categories);
if ( !$post )
return;
$title = apply_filters('the_title', $post->post_title, $post);
if ($limit>-1) {$title = substr($title,0,$limit).'…';}
$string = '<a href="'.get_permalink($post->ID).'">';
$link = str_replace('%title', $title, $link);
$link = $pre . $string . $link . '</a>';
$format = str_replace('%link', $link, $format);
echo $format;
And then you can call the function with the additional argument to define your desired trim length:
<li><?php previous_post_link('%link','« %title',FALSE,'',20); ?></li>
Hope it helps you.
Taken from https://wordpress.org/support/topic/using-substr-to-limit-characters-in-post-title