Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems with custom post types #656

Closed
nanotime opened this issue Aug 26, 2015 · 11 comments
Closed

Problems with custom post types #656

nanotime opened this issue Aug 26, 2015 · 11 comments
Labels

Comments

@nanotime
Copy link

Hi! Im new with this and well, i have a little problem... Well, i create a new custom post type and capture it in the context of a page (to make a internal blog for one site).

For now i get all the posts and i present there in the template without problems but, when i try to get into a post (you know, clicking the permalink) it just retrieve me a 404.

Here is the code:

The custom post (fragment)t in functions.php (works perfectly)

<?php
add_action( 'init', 'create_post_type' );
function create_post_type() {
  register_post_type( 'blogposts',
    array(
      'labels' => array(
        'name' => __( 'Blogposts' ),
        'singular_name' => __( 'Blogpost' )
      ),
      'public' => true,
      'has_archive' => true,
      'supports' => array('title', 'editor', 'thumbnail', 'revisions'),
      'taxonomies' => array('category', 'post_tag')
    )
  );
}

The page.php getting the special post (works without problems)

<?php
$context = Timber::get_context();
$post = new TimberPost();
$context['post'] = $post;
$context['contact_form'] = Timber::get_widgets('form-contact');
$context['dir'] = simple_fields_value('dir');
$context['tlf'] = simple_fields_value('tlf');
$context['mail'] = simple_fields_value('mail');
$context['website'] = simple_fields_value('website');
$context['home_text'] = Timber::get_widgets('texto-inicio');
$context['blogposts'] = Timber::get_posts('post_type=blogposts'); //the unique change, it works

Timber::render( array( 'page-' . $post->post_name . '.twig', 'page.twig' ), $context );

The custom page.twig (it works perfectly)

{% extends "base.twig" %}

{% block content %}
    {% for post in blogposts %}
        <div id="bg" class="">
            <section id="articles_list">
                <article>
                    <div class="thumb"><a href="#"><img src='{{post.thumbnail | resize(300, 300)}}'></a></div>
                    <hgroup><h2><a href="{{post.link}}">{{post.title}}</a></h2></hgroup>
                    <div class="extract">
                        {{post.content}}
                    </div>
                </article>
            </section>
        </div>
    {% endfor %}
{% endblock %}

Now, the problem is with the single, i tried some things like: create a single-blogposts.php and the single-blogposts.twig

<?php 

$context = Timber::get_context();
$post = Timber::query_post();
$context['post'] = $post;

Timber::render('single-blogposts.twig');

?>
{% extends 'base.twig' %}

{% block content %}
    {% for post in blogposts %}
        {{post.title}}
    {% endfor %}
{% endblock %}

Try to use like the video tutorials in the index and making a tease-blogposts.twig and getting the posts in the index.php and so on...

Nothing works :/ ¿Any idea? I just need this to finish the proyect and i have no more hints or ideas.

@prisis
Copy link

prisis commented Aug 26, 2015

You should use $context['blogposts'] = Timber::get_posts('post_type=blogposts'); in your custom single-...php and not $context[post]

@prisis
Copy link

prisis commented Aug 26, 2015

Try to use twig dump on blogposts, you should get a array

@nanotime
Copy link
Author

Hm, i didn't understand at all (my english is not the best, sorry about that), but, you are talking about for something like this in my single-blogposts.php:

<?php 

$context = Timber::get_context();
$post = Timber::get_posts('post_type=blogposts');
$context['blogposts'] = $post;

Timber::render('single-blogposts.twig');

?>

I tried this but nothing happens when the permalink is clicked on the list of articles. I'm a noob, sorry u.u

Edit:

Added the second parameter $context at the single-blogposts.php but nothing happens yet:

Timber::render('single-blogposts.twig', $context);

@prisis
Copy link

prisis commented Aug 26, 2015

Your clicking the permalink in backend? or on a post? can you make a vardump on

var_dump(Timber::get_posts('post_type=blogposts'));

And check if you get a output? Because i don't know where you clicking the permalink.

@nanotime
Copy link
Author

Okay, im clicking it in the post, here: <hgroup><h2><a href="{{post.link}}">{{post.title}}</a></h2></hgroup>

And, the dump of bloposts is:

Array ( [0] => TimberPost Object ( [ImageClass] => TimberImage [PostClass] => TimberPost [object_type] => post [_custom_imported:protected] => [_content:protected] => [_get_terms:protected] => [_permalink:protected] => [_next:protected] => Array ( ) [_prev:protected] => Array ( ) [class] => post-453 blogposts type-blogposts status-publish has-post-thumbnail hentry category-blogpost [display_date] => 26 agosto, 2015 [id] => 453 [ID] => 453 [post_author] => 1 [post_content] => Me gustaria ver si esto funciona, estoy usando texto real para evitar los cortes raros de los lorem ipsum, cosa de ver que pasa. Más párrafos son necesarios, y la gente de sabadell jode como solo ellos, pero vale, pagan por esto y por esto gano dinero. ¿Algo más? Oh si, estoy hasta los huevos HASTA LOS HUEVOS [post_date] => 2015-08-26 16:02:50 [post_excerpt] => [post_parent] => 0 [post_title] => Hasta los huevos [post_type] => blogposts [slug] => hasta-los-huevos [_edit_last] => 1 [_edit_lock] => 1440604841:1 [_thumbnail_id] => 295 [post_date_gmt] => 2015-08-26 16:02:50 [post_status] => publish [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => hasta-los-huevos [to_ping] => [pinged] => [post_modified] => 2015-08-26 16:02:59 [post_modified_gmt] => 2015-08-26 16:02:59 [post_content_filtered] => [guid] => http://shirley:8080/wordpress/?post_type=blogposts&p=453 [menu_order] => 0 [post_mime_type] => [comment_count] => 0 [filter] => raw [status] => publish [custom] => Array ( [_edit_last] => 1 [_edit_lock] => 1440604841:1 [_thumbnail_id] => 295 ) ) [1] => TimberPost Object ( [ImageClass] => TimberImage [PostClass] => TimberPost [object_type] => post [_custom_imported:protected] => [_content:protected] => [_get_terms:protected] => [_permalink:protected] => [_next:protected] => Array ( ) [_prev:protected] => Array ( ) [class] => post-445 blogposts type-blogposts status-publish has-post-thumbnail hentry category-blogpost [display_date] => 26 agosto, 2015 [id] => 445 [ID] => 445 [post_author] => 1 [post_content] => Me gustaria ver si esto funciona, estoy usando texto real para evitar los cortes raros de los lorem ipsum, cosa de ver que pasa. Más párrafos son necesarios, y la gente de sabadell jode como solo ellos, pero vale, pagan por esto y por esto gano dinero. ¿Algo más? Oh si, estoy hasta los huevos [post_date] => 2015-08-26 14:33:51 [post_excerpt] => [post_parent] => 0 [post_title] => Este es un post de pruebas [post_type] => blogposts [slug] => hola-mundo-2 [_edit_last] => 1 [_edit_lock] => 1440604857:1 [_thumbnail_id] => 289 [post_date_gmt] => 2015-08-26 14:33:51 [post_status] => publish [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => hola-mundo-2 [to_ping] => [pinged] => [post_modified] => 2015-08-26 16:03:19 [post_modified_gmt] => 2015-08-26 16:03:19 [post_content_filtered] => [guid] => http://shirley:8080/wordpress/?post_type=blogposts&p=445 [menu_order] => 0 [post_mime_type] => [comment_count] => 0 [filter] => raw [status] => publish [custom] => Array ( [_edit_last] => 1 [_edit_lock] => 1440604857:1 [_thumbnail_id] => 289 ) ) [2] => TimberPost Object ( [ImageClass] => TimberImage [PostClass] => TimberPost [object_type] => post [_custom_imported:protected] => [_content:protected] => [_get_terms:protected] => [_permalink:protected] => [_next:protected] => Array ( ) [_prev:protected] => Array ( ) [class] => post-441 blogposts type-blogposts status-publish has-post-thumbnail hentry category-blogpost [display_date] => 26 agosto, 2015 [id] => 441 [ID] => 441 [post_author] => 1 [post_content] => No, en serio, me caen mal... ¿Alguien tiene idea de lo que es trabajar para esta gente? Joden demasiado y pagan poco, ellos lo saben, somos mano de obra barata pero quieren que les demos productos de calidad por lo poco que pagan. Look at dat descaro. Cojones [post_date] => 2015-08-26 14:09:13 [post_excerpt] => [post_parent] => 0 [post_title] => Estas putas me caen mal [post_type] => blogposts [slug] => esta-es-una-prueba [_edit_last] => 1 [_edit_lock] => 1440605065:1 [_thumbnail_id] => 219 [post_date_gmt] => 2015-08-26 14:09:13 [post_status] => publish [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => esta-es-una-prueba [to_ping] => [pinged] => [post_modified] => 2015-08-26 16:04:25 [post_modified_gmt] => 2015-08-26 16:04:25 [post_content_filtered] => [guid] => http://shirley:8080/wordpress/?post_type=blogposts&p=441 [menu_order] => 0 [post_mime_type] => [comment_count] => 0 [filter] => raw [status] => publish [custom] => Array ( [_edit_last] => 1 [_edit_lock] => 1440605065:1 [_thumbnail_id] => 219 ) ) )

@nanotime
Copy link
Author

The real problem is get the single content when i click the link in the lists of articles, like a normal blog behavior, this is the think that i cant do

@prisis
Copy link

prisis commented Aug 26, 2015

try to use

<hgroup><h2><a href="{{post.get_path}}">{{post.title}}</a></h2></hgroup>

@nanotime
Copy link
Author

I used {{post.title}}, the problem is when i click in this title (with {{post.link}}) i get a 404... :/

page-blogposts.twig:

{% extends "base.twig" %}

{% block content %}
    {% for post in blogposts %}
        <div id="bg" class="">
            <section id="articles_list">
                <article>
                    <div class="thumb"><a href="#"><img src='{{post.thumbnail | resize(300, 300)}}'></a></div>
                    <hgroup><h2><a href="{{post.link}}">{{post.title}}</a></h2></hgroup> {# link and title #}
                    <div class="extract">
                        {{post.content}}
                    </div>
                </article>
            </section>
        </div>
    {% endfor %}
{% endblock %}

@jarednova
Copy link
Member

@nanotime what page are you actually taken to when you get the 404? as in, what's outputted inside of the <a href="">?? Can you also post a screenshot of your permalink settings page in the WordPress admin?

@nanotime
Copy link
Author

Hmm, ok for some extrange reason it begins to "work" but not at all, let me brake this in parts:

First @jarednova my 404 is the same at timber starter theme, i dont touch it (im using the starter theme), so:

<?php
/**
 * The template for displaying 404 pages (Not Found)
 *
 * Methods for TimberHelper can be found in the /functions sub-directory
 *
 * @package  WordPress
 * @subpackage  Timber
 * @since    Timber 0.1
 */

$context = Timber::get_context();
Timber::render( '404.twig', $context );

Then, my Permalink config: %postname% (my instalation is in spanish, sorry)

permalinks

The href output is http://shirley:8080/wordpress/blogposts/hasta-los-huevos/, the correct link.

Again, i have NO IDEA why, but it begins works suddenly

P.D: I can't stop to apologize for my English, I know I have tons of errors

@jarednova
Copy link
Member

No prob., glad it kicked in for you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants