The number of characters in the article title on the archive page cannot be set to 20.

Asked 2 years ago, Updated 2 years ago, 83 views

I have not received a reply from another site, so I will also ask you a question.
(Multipost)

I'm creating an archive page with WordPress, I want to use PHP to specify the number of characters in the title, but I can't specify the number of characters, so it's like what percentage of the total is.

Enter a description of the image here

PHP (function)

<?php
function my_enqueue_scripts(){
    wp_enqueue_script('jquery');
    wp_enqueue_script('bundle_js', get_template_directory_uri().'/assets/js/bundle.js', array();
    wp_enqueue_style('my_styles', get_template_directory_uri().'/style.css', array();
}
add_action('wp_enqueue_scripts', 'my_enqueue_scripts');

add_theme_support('post-thumbnails');

// create an archive page for a post
function post_has_archive($args,$post_type)
{
    if('post'==$post_type){
        $args ['rewrite'] = true; // Enable rewrite
        $args ['has_archive'] = 'blog'; // any slag name
    }
    return$args;
}
function twpp_change_excerpt_length($length){
    return 20; 
  }
  add_filter('excerpt_length', 'twpp_change_excerpt_length',999);
add_filter('register_post_type_args', 'post_has_archive', 10, 2);


PHP (Blog List Page (archive.php))

<?php get_header();?>
<section class="blog-archive-wrapper">
    <div class="blog-archive-wrapper-second">
        <div class="blog-archive-outer">
            <h2class="blog-archive-title">blog</h2>
          
            <?php//Show a list of blogs start?>
<?php if(have_posts()):while(have_posts()):the_post();?>
    <article class="blog-list__list-item">
        <a href="<?php the_permalink();?>"class="blog-item">
            <?php//Show eye catch start?>    
            <div class="blog-item__thumbnail">
                <?php if(has_post_thumbnail()):?>
                <img class="blog-item_thumbnail-image" src="<?php the_post_thumbnail_url('large');?>">
                <?php endif;?>
            </div>
            <?php//end?> to display eye catch
            <div class="blog-item__content">
                <?php//start to display title?>
                <h3class="blog-item__title">?php the_title();?>

// Specifying the Number of Characters in a Title
<?php
if(mb_strlen($post->post_title)>20){
  $title=mb_substr($post->post_title,0,20);
    echo$title.'...';
  } else{
    echo$post->post_title;
  }
?>
// Specify the number of characters in the title So far
                                    </h3>
                <?php//end?> to display the title
                <?php//start to display excerpts?>
                <h3class="blog-item__read"><?php the_excerpt();?>
               </h3>
                <?php//end?> to display excerpts
                <div class="blog-item__button">
                    <span class="blog-item__button-more">Read Articles</span>
                </div>
                <?php the_time('Y-m-d');?>
    </article>
<?php endwhile;endif;?>
<?php//View a list of blogs end?>

<?php the_posts_pagination(
    array(
        'mid_size' = > 2, // Number of page numbers to display on the left and right sides of the current page
        'prev_next' = > true, // True if you want to see the 'forward' or 'next' link
        'prev_text' = > __('forward'), // Text in the 'forward' link
        'next_text' = > __('Next'), // Text in the 'Next' link
        'type' = > 'list', // Specify return value (plain/list)
    )
);?>
           
</section>
<?php get_footer();?>

php wordpress

2022-09-30 19:49

1 Answers

I've tried a lot of things, but
<?phpechomb_substr($post->post_title,0,20).'...';?>
I'm glad it's this simple one. I couldn't do anything complicated, but I could do it with this one.


2022-09-30 19:49

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.