Cómo limitar caracteres en títulos de entradas de página principal

  • Autor Autor Menorah
  • Fecha de inicio Fecha de inicio
M

Menorah

Épsilon
Hola, quiero limitar los caracteres de los títulos de las entradas de la página principal y deseo que me ayuden a implementar correctamente los códigos que tengo para tal caso.

Deseo implementar este código:

Insertar CODE, HTML o PHP:
echo wp_trim_words ( the_title ( '', '', false ), 4 , '...');

Y en la plantilla tengo el siguiente código:

Insertar CODE, HTML o PHP:
the_title( '<h2 class="entry-title"><a href="' . get_permalink() . '">', '</a></h2>' );


En qué parte del segundo código pongo 'wp_trim_words... para filtrar la cantidad de caracteres de los títulos.

He intentado con:



Insertar CODE, HTML o PHP:
echo wp_trim_words ( the_title [B][COLOR=#ff0000]( '<h2 class="entry-title"><a href="' . get_permalink() . '">', '</a></h2>' )[/COLOR][/B] ( '', '', false ), 4 , '...');

Pero no funciona.

Aquí pongo el código completo de la plantilla:

Insertar CODE, HTML o PHP:
<?php/**
 * The template part for displaying post item for "list" listing type
 *
 * [U][URL="http://forobeta.com/member.php?u=156547"]pack[/URL][/U]age BoomBox_Theme
 */


// Prevent direct script access.
if ( ! defined( 'ABSPATH' ) ) {
    die( 'No direct script access allowed' );
}


$boombox_featured_image_size = 'boombox_image360x180';
$boombox_post_classes        = 'post';
$boombox_has_post_thumbnail  = true;
$boombox_template_options    = boombox_get_template_grid_elements_options();


if ( !$boombox_template_options['media'] || !has_post_thumbnail() ):
    $boombox_post_classes       .= ' no-thumbnail';
    $boombox_has_post_thumbnail  = false;
endif; ?>


<article id="post-<?php the_ID(); ?>" <?php post_class( $boombox_post_classes ); ?>>




        <!-- thumbnail -->
        <div class="post-thumbnail">
            <?php if ( $boombox_template_options['badges'] ) { ?>
                <?php boombox_get_post_badge_list();
            } ?>


            <?php if ( $boombox_has_post_thumbnail ) : ?>
            <a href="<?php echo esc_url( get_permalink() ); ?>"
               title="<?php echo esc_attr( the_title_attribute() ); ?>">
                <?php the_post_thumbnail( $boombox_featured_image_size ); ?>
            </a>
            <div class="post-meta">
                <?php boombox_post_view_vote_count( get_the_ID(), $boombox_template_options['views_count'], $boombox_template_options['votes_count'] ); ?>
                <?php if ( $boombox_template_options['share_count'] ) :
                    boombox_post_share_count();
                endif; ?>
            </div>
            <?php endif; ?>
        </div>
        <!-- thumbnail -->


    <div class="content">
        <!-- entry-header -->
        <header class="entry-header">
            <?php if ( $boombox_template_options['categories'] ) :
                boombox_categories_list();
            endif;
            if ( comments_open() && $boombox_template_options['comments_count'] ) :
                boombox_post_comments();
            endif;


            the_title( '<h2 class="entry-title"><a href="' . get_permalink() . '">', '</a></h2>' );


            if ( $boombox_template_options['subtitle'] ) :
                boombox_the_post_subtitle();
            endif;


            if ( $boombox_template_options['author'] ) :
                boombox_post_author();
            endif;
            if ( $boombox_template_options['date'] ) :
                boombox_post_date();
            endif; ?>


        </header>
        <!-- entry-header -->
        <?php if ( $boombox_template_options['excerpt'] ) : ?>
            <div class="entry-content"><?php echo wp_trim_excerpt(); ?></div>
        <?php endif; ?>


    </div>


<?php 
$url = get_permalink($post->ID);
$title = get_the_title($post->ID);
echo do_shortcode('[easy-social-share buttons="facebook,twitter,google,pinterest,whatsapp,meneame,messenger" style="icon" template="8" nospace="yes" point_type="simple" url="'.$url.'" text="'.$title.'" postviews="left"]'); 
?> 


</article>


A ver si Enlace eliminado me puede dar una mano. 🙂

- - - Actualizado - - -

¿Nadie? 😕

- - - Actualizado - - -

He intentado con .css pero no funciona en internet explorer, por lo que toca implementar código en PHP, alguien me dice cómo, ¿por favor?
 
Última edición:
[MENTION=114489]Menorah[/MENTION] prueba de esta forma! :encouragement:

PHP:
echo '<h2 class="entry-title"><a href="'.get_the_permalink().'">'.wp_trim_words(get_the_title(), 40, '...').'</a></h2>';
 
Menorah prueba de esta forma! :encouragement:

PHP:
echo '<h2 class="entry-title"><a href="'.get_the_permalink().'">'.wp_trim_words(get_the_title(), 40, '...').'</a></h2>';


Funciona, sos un groso. Gracias por la ayuda, saludos. 😛7:


P.D.: una última cosita, ¿sabes cómo hacer para que se limiten caracteres en vez de palabras en el código que me diste?. He intentado reemplazar wp_trim_words con wp_trim_excerpt, pero no hay caso.
 
Última edición:
[MENTION=114489]Menorah[/MENTION] prueba agregando esta funcion en tu functions.php

PHP:
function titulocorto($titulo, $length){
	$size = strlen($titulo);
	if($size > $length){
		$titulo = substr($titulo, 0, $length);
		$titulo = explode(' ', $titulo);
		array_pop($titulo);
		$titulo = implode(' ', $titulo).'...';
	}
	return $titulo;	
}

y usala de esta forma!

PHP:
echo '<h2 class="entry-title"><a href="'.get_the_permalink().'">'.titulocorto(get_the_title(), 100).'</a></h2>';
 
Menorah prueba agregando esta funcion en tu functions.php

PHP:
function titulocorto($titulo, $length){
    $size = strlen($titulo);
    if($size > $length){
        $titulo = substr($titulo, 0, $length);
        $titulo = explode(' ', $titulo);
        array_pop($titulo);
        $titulo = implode(' ', $titulo).'...';
    }
    return $titulo;    
}

y usala de esta forma!

PHP:
echo '<h2 class="entry-title"><a href="'.get_the_permalink().'">'.titulocorto(get_the_title(), 100).'</a></h2>';


Hola, gracias por responder, pero el código me "rompe" el sitio.
 
Oh que raro, agregate la funcion en functions.php de tu theme ?

Si claro, en un child theme, pero no te preocupes, ahora lo estoy haciendo todo por .css y va quedando así:

y8XJw5G.png


Al final opté por no añadir la elipsis al título, sino al contenido con ayuda de un poco de .css.

Ahora lo único que me falta por hacer es asignarle una posición fija a los iconos de las redes sociales para que queden así:

eZWVetc.png


Para esto último, crearé otro tema.

De nuevo, muchas gracias por tu ayuda colega. ¡Saludos! 😛8:
 
Atrás
Arriba