ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Prev/Next links that support custom fields?

March 10, 2008 8:06pm

Subscribe [5]
  • #1 / Mar 10, 2008 8:06pm

    sahadeva

    14 posts

    I’m working on a /comments page (individual post) and I’d like to be able to link to the previous and next posts AND include some of my custom fields in those links, e.g. “Previous post: Title, by So-and-so”, all wrapped in an <a> tag. I know this has been batted around in the forums a few times, and that using {exp:weblog:previous_entry} won’t won’t because it doesn’t support custom fields. I tried using {exp:weblog:entries weblog="{my_weblog}”  orderby=“date”  limit=“1” offset=“1”} (notice of the OFFSET=“1”) but that doesn’t seem to grab the right entry relative to the current post, and it doesn’t seem to handle the case where there is no previous entry (just shows the current entry if I’m doing this right). Am I missing something? Also, using offset=”-1” for the NEXT post gives me a MYSQL error…

    I also tried using {pagination} tags inside the {exp:weblog:entries} tags but those don’t seem to support custom fields either…

    Any ideas on how to build simple previous / next links in EE that support custom fields? So I’m wondering if there’s a way of working it outside the pagination/previous_entry methods, e.g. somehow using offset, so as to allow the use of custom fields?

    Thanks : )

  • #2 / Mar 10, 2008 10:30pm

    Sue Crocker

    26054 posts

    You have access to the entry_id in each of these.. you could use the query module to bring back what you need to display additional information.

  • #3 / Mar 11, 2008 12:01am

    sahadeva

    14 posts

    Hmmm, I think I see what you mean—is this the kind of expression I should be writing (below)? I get a MYSQL error, I imagine because I have the FROM and WHERE elements of the query wrong (any suggestions on what I should be putting in those elements or where I can find that info? The EE docs on Query Module Tags were mum on the subject…).

    {exp:weblog:next_entry}

    {title}

    {exp:query sql="SELECT post_author FROM exp_weblog_titles WHERE entry_id = '1'"}
    {post_author}
    {/exp:query}

    {/exp:weblog:next_entry}

    O did you mean using the pagination or Offset way of pulling in the post info?

    Thanks for the response.

    Here’s the SQL error I get:

    MySQL ERROR:

    Error Number: 1054
    Description: Unknown column ‘post_author’ in ‘field list’

    Query: SELECT post_author FROM exp_weblog_titles WHERE entry_id = ‘19’

  • #4 / Mar 11, 2008 1:22am

    Matt Weinberg

    489 posts

    I assume you’re trying to get the author of the entry you’re linking to? exp_weblog_titles only has author_id—you need to do a join:

    select t2.username
    from exp_weblog_titles t1 inner join exp_members t2
    on t1.author_id = t2.member_id
    where t1.entry_id = {entry_id}

    I’m assuming you can use {entry_id} within the query module but am not 100% sure.

    Does that help?

  • #5 / Mar 11, 2008 10:44am

    Robin Sowell

    13255 posts

    Is post_author a custom field?  If so- you need to figure out the id- then query the exp_weblog_data table for that id:

    SELECT field_id_x FROM exp_weblog_data WHERE entry_id='{entry_id}'

    ‘x’ is the id of the custom field.  Though if we’re getting into custom queries, I’m going to shift this over to ‘How to’.

  • #6 / Mar 11, 2008 1:16pm

    sahadeva

    14 posts

    Thanks Robin. Yes, {post_author} is a custom field, and I found the field ID in the database and fixed up the query (I think), but I’m still not sure where to put the query. Can I put it inside {exp:weblog:next_entry} tags, or do I have to use some other weblog tag?

    Here’s the new code:

    {exp:weblog:next_entry weblog="{my_weblog}"}
    <a href="http://{path="><span class="previous">Previous</span><span class="title">{title}</span><br>_<span>by _{exp:query sql="SELECT field_id_9 FROM exp_weblog_data WHERE entry_id='{entry_id}'"}_{post_author}_{/exp:query} _</span></a>
    {/exp:weblog:next_entry}

    And this is the output I get on the rendered webpage (the EE tags are showing)

    Test Post Title
    by {abm_post_author} {/exp:query}


    _____

    Thanks for your response slapshotw but it’s a bit over my head… : )

  • #7 / Mar 11, 2008 1:33pm

    Matt Weinberg

    489 posts

    My apologies—I thought post_author was you looking for the username of the author of the entry, not a custom field called that. I should have read better.

    If you’re selecting field_id_9, you need to use the variable name {field_id_9} to display it.

  • #8 / Mar 11, 2008 1:43pm

    Robin Sowell

    13255 posts

    Bugger- looks like it’s running into a conflict.  I replicate your result- you were doing it right- though I think there’s a bad single quote in there.  Anybody else replicate?

    Did get it going w/pure php pared on output- which is messier.

    {exp:weblog:next_entry}
    {title} = {entry_id}
    
    <?php global $DB;
    $result = $DB->query("SELECT field_id_13 AS post_author FROM exp_weblog_data WHERE entry_id='{entry_id}'");
    if ($result->num_rows > 0)
    {
    echo '<span>by '.$result->row['post_author'].'</span>';
    }
    ?>
    {/exp:weblog:next_entry}

    Adjust field id numbers as needed- wanted to test it on my own data, so tweaked it a bit.

  • #9 / Mar 12, 2008 1:22pm

    sahadeva

    14 posts

    Thanks again Robin. Still getting code in the output using the php method you wrote above (here’s the output I get)

    Test 2 March 4 = 19
    query(“SELECT field_id_9 AS post_author FROM exp_weblog_data WHERE entry_id=‘19’”); if ($result->num_rows > 0) { echo 'by '.$result->row['post_author'].''; } ?>

    ___

    Test 2 March 4 is the correct title, and 19 is the correct post id, though it shouldn’t be displayed. Any ideas?

    Here again is my full code, filled in with correct data:

    <li>
    {exp:weblog:next_entry weblog="{my_weblog}"}
    <a href="http://{path="><span class="label previous">Previous</span><span class="title">{title} = {entry_id}</span><br>_<?php global $DB;_$result = $DB->query("SELECT field_id_9 AS post_author FROM exp_weblog_data WHERE entry_id='{entry_id}'");_if ($result->num_rows > 0)_{_echo '<span class="author">by '.$result->row['post_author'].'</span>';_}_?></a>
    {/exp:weblog:next_entry}
    </li>
  • #10 / Mar 12, 2008 1:30pm

    Robin Sowell

    13255 posts

    Hm- is php turned on for that template- and parsing set to output?  Also look for any curly quotes that may have worked their way in.  Had one in my initial test.  But in general- it worked for me.

  • #11 / Mar 12, 2008 4:59pm

    sahadeva

    14 posts

    Ah, that seems to have done it! Thanks a bazillion—you’re the best!

    Also, it doesn’t look like I needed “{title} = {entry_id}” in there, just “{title}”, otherwise it output “TitleName = 19” (where 19 is the post number). 

    I think I’ll go add a feature request to support custom fields in the {exp:weblog:next_entry} tags now, too ; )

  • #12 / Aug 07, 2008 12:12pm

    Pigtail Dencil

    117 posts

    Nice. Worked for me too. Now let me look for that Feature Request and second it.

  • #13 / Mar 19, 2009 4:49am

    J. Hull

    132 posts

    I just tried this but for some reason it’s not working for me.

    On a single entry page, I’ve copied the code above but for some reason it is only returning me the weblog_data from the current page (the current entry_id), NOT the next or previous ones.

    Any ideas of why this could be happening?

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases