Give posts a particular template based on the assigned category

nikolaou, When I understand you question correct then you want to use different templates based on the categories you’re using within your posts, correct? If that’s the case, then you only need one template (single.php) and a combination of get_the_terms() and get_template_part(). <?php $categories = get_the_terms(get_the_ID(), ‘slug’); if ( in_array(‘cat1’, $categories) ) { get_template_part(‘partials/single’, ‘1’); … Read more

Add two custom fields as a default to the “new post” page.

The below code will add default custom fields to your post ( Insert the code in your themes function.php ) . add_action(‘wp_insert_post’, ‘set_default_custom_fields’); function set_default_custom_fields($post_id){ if ( $_GET[‘post_type’] == ‘post’ ) { add_post_meta($post_id, ‘Field Name’, ”, true); add_post_meta($post_id, ‘Another Field Name’, ”, true); } return true; } By default this will add an empty custom … Read more

WordPress post filter menu

1- To get a list of authors, you can use: <?php wp_list_authors( ); ?> This will return a list of authors and a link to their accounts and posts. To customize it using your desired arguments, take a look at Codex 2- To retrieve the categories, use: <?php wp_list_categories(); ?> This function will return a … Read more

Related Posts function not working

The ultimate answer will be in your server logs, I’m afraid. (You can also turn on debugging in wp-config – sometimes that helps too) But I did notice a few things that hopefully can help you track the issue: <?php // You should not require the $orig_post variable… global $post; // Get the tag ids … Read more

Add code to a specific post’s content

Your code is fine, but your logic isn’t working because of $cat. $cat is returning an Array with an Object which you’re trying to check against. But you can easily just use in_category(‘music’) like this: function add_go_back_btn($content) { global $post; $cat = get_the_category(); $post_type = get_post_type(); if(in_category(‘music’) && $post_type === ‘post’) { return ‘<div><a href=”https://wordpress.stackexchange.com/music”>This … Read more

How to remove text and retain in caption image using the_content function

If you are trying to remove the text i think the below code is what is adding this /// this is where your content is being inserted <div class=”strip_first_image”> <?php $content = get_the_content();$content = preg_replace(“/<img[^>]+\>/i”, ” “, $content); $content = preg_replace(“/\[caption.*\[\/caption\]/”, ”, $content);$content = apply_filters(‘the_content’, $content);$content = str_replace(‘]]>’, ‘]]>’, $content);echo $content; ?> </div> Someone with … Read more

Add div before the first paragraph the_content [closed]

Yogenisia’s is right that you should use the_content filter. If you want to insert something before the first paragraph, locate the first <p> in $content, then insert your div with substr_replace. add_filter( ‘the_content’, ‘namespace_insert_before_first_paragraph’ ); function namespace_insert_before_first_paragraph( $content ) { $my_div = ‘<div> … </div>’; $first_para_pos = strpos( $content, ‘<p’ ); $new_content = substr_replace( $content, … Read more