How To Add Dropdown Category Selection For Publishing Posts In Your WordPress P2 Theme ?

Add Dropdown Category Selection For Publishing Posts In Your WordPress P2 Theme

In your P2 theme’s p2.js file go to line 217 which should show:

var args = {action: ‘new_post’, _ajax_post:nonce, posttext: posttext, tags: tags, post_cat: post_cat, post_title: post_title, post_citation: post_citation };

Replace above line with:

var drop_cat = $(‘#drop_cat’).val();
var args = {action: ‘new_post’, _ajax_post:nonce, posttext: posttext, tags: tags, post_cat: post_cat, drop_cat: drop_cat, post_title: post_title, post_citation: post_citation };

Now open  ajax.php, find around line 155:

$accepted_post_cats = apply_filters( ‘p2_accepted_post_cats’, array( ‘post’, ‘quote’, ‘status’, ‘link’ ) );
$post_cat = ( in_array( $_POST[‘post_cat’], $accepted_post_cats ) ) ? $_POST[‘post_cat’] : ‘post’;

Make a backup, or comment these lines out and replace with:

$drop_cat= $_POST[‘drop_cat’];
$post_cat = $_POST[‘post_cat’];
if ($drop_cat) { $post_cat = $drop_cat;} //if a category was selected from the drop down menu, this will become the category.

Now add the actual dropdown menu, in post-form.php add this before line 78 (before the form closes)

<select name=”drop_cat” id=”drop_cat”>
<option value=””><?php echo attribute_escape(__(‘Select Category’)); ?></option>
<?php
$categories= get_categories();
foreach ($categories as $cat) {
$option = ‘<option value=”‘.$cat->category_nicename.'”>’;
$option .= $cat->cat_name;
$option .= ‘ (‘.$cat->category_count.’)’;
$option .= ‘</option>’;
echo $option;
}
?>
</select>

If you like to exclude certain categories you can control what categories are displayed by passing parameters to get_categories()

Source

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.