Open POST in php file

0

By default I know I can list some wordpress articles on an external page in php using a "require ('../../ wp-blog-header.php');" .

However, by clicking on one of these articles, it goes to the default page that opens the entire post.

My question is:

How do I make the complete POSt open in a separate (PostComplete.php) file, instead of opening it in single.php? p>     

asked by anonymous 27.11.2016 / 20:49

1 answer

0

You can use the include require('../../wp-blog-header.php'); method to load any content. What changes is the way you call content inside your external file.

By default when you include wp-blog-header.php it will load the entire back-end of WordPress with the basic parameters, ie it loads the home page. Then he will obey whatever is in your file, so you can create a second query to get the content that interests you:

// Pra buscar um arquivo de posts do tipo "artigos"
$query = new WP_Query( array( 'post_type' => 'artigos' /* etc */ ) );
if ( $query->has_posts() ) : 
    while ( $query->has_posts() ) : 
        $query->the_post();
        // exibe as informações aqui
    endwhile;
endif;

-

// Pra buscar um post pelo ID
$single = get_post( $id );
echo $single->post_title; // imprime o título

Now, to link from within your posts file to external php you will probably have to filter the URLs that are being displayed. Something like this is a path, but there are others depending on your specific case:

// em functions.php
add_filter( 'post_link', 'altera_link' ); // posts
add_filter( 'page_link', 'altera_link' ); // pages
add_filter( 'post_type_link', 'altera_link' ); // custom post types
function altera_link( $link ) {
    // altera os links de 'example.com' para 'domain.com';
    return str_replace( 'example.com', 'domain.com', $link );
}

but: loading wp-blog-header.php is usually a bad idea.

I would think highly about the architecture of this system because there are better ways of not using the WP template system, if that is the case. There are wrappers for PHP frameworks (type Corcel ), or even for javascript front ends (type NodeifyWP ) and there's good old Ajax to pull only the content you want.

    
28.11.2016 / 14:07