WordPress.org

Support

Support » Plugins and Hacks » [p2 theme] making posts outside of frontpage

[p2 theme] making posts outside of frontpage

  • I made some tweaks in the P2 theme allowing users to make comments on other pages (not just the frontpage). What I’m having trouble with is when a new post is made, say on a category page, it doesn’t automatically load the post into the stream as it would on the frontpage, you have to refresh the page.

    Has anybody else tried tweaking P2 in this way with more luck?

    I’ve played around with the JS and functions files and tried making alterations, especially by changing some of the conditional parameters (functions that require the frontpage to be the active page), but I haven’t been able to solve it as of yet. Anybody had more success here?

Viewing 8 replies - 1 through 8 (of 8 total)
  • I got this working in a roundabout way (involves editing the theme files in some places). If anybody is interested leave a message.

    I WOULD LOVE TO KNOW!!! spent all day trying to find the answer!!!!

    update:
    I thought I had a nice fix (I let the loop refresh every time a post was made) but it stopped working. It’s very frustrating figuring out the code.

    An even less prettier way would be to go into p2.js and add a page reload function every time a post is made (only if you’re not on the homepage). This does make the entire page refresh (not ideal at all).

    hope somebody figures out how to do this properly, imo they should be built in.

    If I find a better way I’ll post it here

    Looking for this solution for a while. Ideally, posting on author page.

    Is it necessary to have the new post load in the ajax way that it does on the front page? If not, then you can still have authors post from the author page by just including the post form. Unless I’m missing something.

    <?php if( current_user_can( 'publish_posts' ) ||
    	    (get_option( 'p2_allow_users_publish' ) && $user_ID ) )
    	require_once dirname( __FILE__ ) . '/post-form.php';
    ?>

    Yup – don’t see why that would not work… Although I don’t know where else I would have it… unless I made a separate post page or something… But then you aren’t really looking for P2 at that point.

    I’ve changed my post-form.php file a LOT. I also changed the entry.php file (so it can just be called from functions.php – there’s so much redundancy in that thing!)

    Has anyone tried acousins’ solution yet?

    I made it work

    Although it requires a lot of file editing. I will add my changes below, it works for me.

    Make the script load post from correct category:

    js.php

    Find

    var isFirstFrontPage = <?php echo $page_options['is_first_front_page'] ?>;
    var isFrontPage = <?php echo $page_options['is_front_page'] ?>;

    Replace with

    var isFirstFrontPage = <?php echo 1 //$page_options['is_first_front_page'] ?>;
    var isFrontPage = <?php echo 1 //$page_options['is_front_page'] ?>;
    var thecategoryid = <?php 
    
        if($page_options['is_front_page']!=1){
    		$category11 = get_the_category();
    		echo $category11[0]->cat_ID;
    		}else{
    		echo "false";
    		}
    
    		?>;

    p2.js
    Correct js category query, around line 107

    var queryString = ajaxUrl +'&action=get_latest_posts&load_time=' + pageLoadTime + '&frontpage=' + isFirstFrontPage+ '&thecategoryid=' + thecategoryid;

    ajax.php
    Correct database query
    Replace function get_latest_posts()

    function get_latest_posts() {
    		$load_time = $_GET['load_time'];
    		$frontpage = $_GET['frontpage'];
    		$thecategoryidunsafe = $_GET['thecategoryid'];
    		$thecategoryid = filter_var($thecategoryidunsafe, FILTER_SANITIZE_NUMBER_INT);
    		if (is_numeric ($thecategoryid )){
    		$catquer = "&cat=$thecategoryid";
    		}
    
    		$num_posts = 10; // max amount of posts to load
    		$number_of_new_posts = 0;
    
    		query_posts('showposts=' . $num_posts . '&post_status=publish'.$catquer);
    		ob_start();
    		while (have_posts()) : the_post();
    		    $current_user_id = get_the_author_meta( 'ID' );
    			if ( get_gmt_from_date( get_the_time( 'Y-m-d H:i:s' ) ) <=  $load_time ) continue;
    			$number_of_new_posts++;
    			$post_request_ajax = true;
    			require dirname(__FILE__) . '/../entry.php';
    	    endwhile;
    	   	$posts_html = ob_get_clean();
    
    	    if ( $number_of_new_posts != 0 ) {
    			nocache_headers();
    	    	echo json_encode( array(
    				'numberofnewposts' => $number_of_new_posts,
    				'html' => $posts_html,
    				'lastposttime' => gmdate('Y-m-d H:i:s')
    			) );
    		} else {
    			header("HTTP/1.1 304 Not Modified");
    	    }
    	}

    I also need multiple categories per post, the p2-theme does not do this out of the box.

    Make the postform post “post_cat” in this format: “cat1,cat2,cat3” That’s easy so I wont write that here.

    Then you need to make the p2-theme support multiple categories per post.

    ajax.php

    My solution:

    //snip
    	/*	$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'; */
    
    		$post_cat = filter_input(INPUT_POST, 'post_cat' , FILTER_SANITIZE_SPECIAL_CHARS);
    
    		$post_cat = explode(",",$post_cat);
    
    		$catids = array();
    
    		foreach($post_cat as $singlecat){
    
      		if ( !category_exists( $singlecat ) )
    			wp_insert_category( array( 'cat_name' => $singlecat ) );
    
          $singlecat = get_category_by_slug( $singlecat );
    
          array_push($catids,$singlecat->cat_ID);
    		}
    
    		/* Add the quote citation to the content if it exists */
    		if ( !empty( $_POST['post_citation'] ) && 'quote' == $post_cat->slug ) {
    			$post_content = '<p>' . $post_content . '</p><cite>' . $_POST['post_citation'] . '</cite>';
    		}
    
    		$post_id = wp_insert_post( array(
    			'post_author'	=> $user_id,
    			'post_title'	=> $post_title,
    			'post_content'	=> $post_content,
    			'post_type'	=> $post_type,
    			'post_category' => $catids,
    //snip

    I think that’s all, good luck!

    Very Nice! I’m going to try out your code today. Is the site you implemented it on shareable here?

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘[p2 theme] making posts outside of frontpage’ is closed to new replies.
Skip to toolbar