Creating a Custom Post Type Using Code

To create a custom post type, you'll use the register_post_type() function in WordPress. Here's an example:

Step 1: Add the Code

  1. Open your theme's functions.php file or create a custom plugin.
  2. Add the following code
function create_books_post_type() {
    $labels = array(
        'name'               => _x('Books', 'post type general name'),
        'singular_name'      => _x('Book', 'post type singular name'),
        'menu_name'          => __('Books'),
        'name_admin_bar'     => __('Book'),
        'add_new'            => __('Add New'),
        'add_new_item'       => __('Add New Book'),
        'edit_item'          => __('Edit Book'),
        'new_item'           => __('New Book'),
        'view_item'          => __('View Book'),
        'all_items'          => __('All Books'),
        'search_items'       => __('Search Books'),
        'not_found'          => __('No Books found.'),
        'not_found_in_trash' => __('No Books found in Trash.'),
    );

    $args = array(
        'labels'             => $labels,
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array('slug' => 'books'),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,
        'supports'           => array('title', 'editor', 'thumbnail', 'excerpt', 'comments'),
    );

    register_post_type('book', $args);
}
add_action('init', 'create_books_post_type');

Step 2: Save and Test

  1. Save the file and visit your WordPress admin panel.
  2. You should see a new "Books" menu item.
  3. Create a new book post and publish it.

Displaying Custom Post Types

Creating Custom Templates

To display custom post types on the front end, create these template files in your theme:

1. Single Template

  • File: single-book.php
  • Code Example:

2. Archive Template

  • File: archive-book.php
  • Code Example:

Books Archive

Troubleshooting Common Issues

1. Permalinks Not Working:

Go to Settings > Permalinks and click "Save Changes" to flush rewrite rules.

2. Custom Post Type Not Appearing:

Ensure public is set to true in the $args array

WordPress post excerpt section-wise (e.g., paragraphs, sentences, or words)

If you want to limit the WordPress post excerpt section-wise, you can divide it into specific sections (e.g., paragraphs, sentences, or words) and then display only a particular section or a limited number of sections.

Here's how you can achieve this:

Example 1: Limit Excerpt by Paragraphs



Example 2: Limit Excerpt by Sentences



Example 3: Limit Excerpt by Words



Explanation:

  1. Paragraphs (explode("\n")): Splits the excerpt by new lines.
  2. Sentences (preg_split): Uses a regular expression to split the excerpt into sentences based on punctuation.
  3. Words (explode(' ')): Breaks the excerpt into individual words.

Use Case:

  1. Use the paragraph-based approach for content with clear paragraph divisions.
  2. Use the sentence-based approach for summaries or descriptive excerpts.
  3. Use the word-based approach when you need precise control over the word count.

You can choose the method depending on how you want to display or limit the excerpt.

Add Social Media Sharing Buttons for a Specific Post in WordPress

Sometimes, you may want to display social media sharing buttons for a specific post rather than all posts on your WordPress site. This guide will show you how to implement sharing buttons for a specific post using PHP.

The Code

Here’s the PHP code snippet you can use:



    

    

How It Works

  1. Specify the Post: Replace 123 in $specific_post_id = 123; with the ID of the post where you want the sharing buttons to appear.
  2. Check Post ID: The if ( get_the_ID() == $specific_post_id ) ensures that the sharing buttons are only displayed for the specified post.
  3. Generate Links: Sharing links for platforms like Facebook, Twitter, LinkedIn, and WhatsApp are dynamically created using the post's URL and title.

Adding the Code

  1. Locate the File: Edit your theme’s single.php or a custom template file.
  2. Paste the Code: Add the snippet at the desired location within the file.
  3. Customize: Update the icon URLs (https://example.com/) and adjust styling as needed.

Enhancements

  • Dynamic Post Targeting: If you want to target multiple specific posts, replace the condition with an array, like so:
    $specific_posts = [123, 456]; // Replace with your post IDs
    if ( in_array( get_the_ID(), $specific_posts ) ) {
        // Display buttons
    }
    
  • Styling: Use CSS to align, style, and animate the buttons for a better user experience.
  • JavaScript for Copy Button:
    function copyToClipboard() {
        navigator.clipboard.writeText("");
        alert("Link copied to clipboard!");
    }
    

This approach ensures your social sharing buttons appear only where you need them. It's ideal for promoting a specific article or post that you want to make more shareable.