• rafiqul233

    (@rafiqul233)


    I want to add my Category name to each Section of my post. Like the 2nd image from mdpi.com to my website [linked below]

    • This topic was modified 1 year ago by bcworkz. Reason: redundant link removed

    The page I need help with: [log in to see the link]

Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator bcworkz

    (@bcworkz)

    To output anything as the very first part of an individual post’s content you can use “the_post” action hook to generate the output.

    Your callback should verify it’s the right sort of request to output categories for, such as if you only want this for archive requests and not single posts. Or only for post post types, etc. At the very least verify ! is_admin() or else the admin post list table display will get messed up.

    The current WP_Post object is passed to your callback. With its ID property you can use wp_get_post_categories() to get assigned categories. Use the “fields” arg to only get category names, which will be returned as an array. You can use implode() to transform the array elements into a string, or loop through the array to output each element along with its surrounding HTML.

    Thread Starter rafiqul233

    (@rafiqul233)

    I don’t understand anything. I am not any kind of developer and I don’t have any development knowledge. So is there any kind of simple process then please help me with that.
    Thank You

    Moderator bcworkz

    (@bcworkz)

    When you ask in a “Developing” forum, you get a developer oriented answer ?? (no worries, the wrong forum is a very common mistake people make)

    You could start with this added to your theme’s functions.php:

    add_action('the_post', function( $post ) {
    	echo implode(', ', wp_get_post_categories( $post->ID, ['fields'=>'names',]));
    });

    This only outputs a plain, un-styled comma delimited list. It would need to also output appropriate HTML and have proper CSS applied to have it look like your example.

    Thread Starter rafiqul233

    (@rafiqul233)

    Thank You For the help. And Where I can Put The CSS to design the Field?

    Thread Starter rafiqul233

    (@rafiqul233)

    And another thing Is there is 2 catagory showing I want only on the top of the post Title.

    Moderator bcworkz

    (@bcworkz)

    Your added CSS can go in the Additional CSS section of the customizer or style book. Which one depends on the type of theme your have, classic or block respectively.

    I don’t know why the category terms reappear after the post listing. It’s likely a quirk of your theme. The code works as expected on the default classic theme Twenty Twenty-one. The code probably needs to check some condition prior to echoing out the terms. Perhaps your theme’s dedicated support forum could advise.

    Or maybe it could be hidden with CSS by using the :last-child pseudo-selector. You will want to place the terms in a div container anyway so they can be easily targeted with CSS selectors. Like this:

    echo '<div class="cat-terms">',
       implode(' ', wp_get_post_categories( $post->ID, ['fields'=>'names',])),
       '</div>';
    Thread Starter rafiqul233

    (@rafiqul233)

    Thank You For your help.
    I need an other solution. I have few users on my Website. when every indivisual user will login to their wp Dashboard, I want they won’t see others published post on their WP Dashboard. I want they only see their posts on the backend but in the front end of the site every posto should be visible by all of the users.
    Please help me with that too

    Moderator bcworkz

    (@bcworkz)

    The query for the posts list table uses the “pre_get_posts” action hook just like any other WP_Query method would. You can use this hook to modify what appears on the list table. Such as only listing posts where the author is the current user, regardless of which list view one is using.

    You’ll want to be sure the situation is right for applying this restriction since all posts queries utilize this action hook. For example, by first verifying this is an admin request and the current user is not an admin or editor role.

    I know you’re not a developer. If you’re unwilling to struggle with an unfamiliar paradigm you could hire professional help through a resource such as jobs.wordpress.net.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Ctagory Name Above on My Post Article’ is closed to new replies.